Passed
Push — fix-php-74 ( f6eb65...a1ad6b )
by Alexander
38:08 queued 12:19
created

SyslogTarget::export()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\log;
9
10
use Yii;
11
use yii\helpers\VarDumper;
12
13
/**
14
 * SyslogTarget writes log to syslog.
15
 *
16
 * @author miramir <[email protected]>
17
 * @since 2.0
18
 */
19
class SyslogTarget extends Target
20
{
21
    /**
22
     * @var string syslog identity
23
     */
24
    public $identity;
25
    /**
26
     * @var int syslog facility.
27
     */
28
    public $facility = LOG_USER;
29
    /**
30
     * @var int openlog options. This is a bitfield passed as the `$option` parameter to [openlog()](https://secure.php.net/openlog).
31
     * Defaults to `null` which means to use the default options `LOG_ODELAY | LOG_PID`.
32
     * @see https://secure.php.net/openlog for available options.
33
     * @since 2.0.11
34
     */
35
    public $options;
36
37
    /**
38
     * @var array syslog levels
39
     */
40
    private $_syslogLevels = [
41
        Logger::LEVEL_TRACE => LOG_DEBUG,
42
        Logger::LEVEL_PROFILE_BEGIN => LOG_DEBUG,
43
        Logger::LEVEL_PROFILE_END => LOG_DEBUG,
44
        Logger::LEVEL_PROFILE => LOG_DEBUG,
45
        Logger::LEVEL_INFO => LOG_INFO,
46
        Logger::LEVEL_WARNING => LOG_WARNING,
47
        Logger::LEVEL_ERROR => LOG_ERR,
48
    ];
49
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function init()
55
    {
56
        parent::init();
57
        if ($this->options === null) {
58
            $this->options = LOG_ODELAY | LOG_PID;
59
        }
60
    }
61
62
    /**
63
     * Writes log messages to syslog.
64
     * Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.
65
     * @throws LogRuntimeException
66
     */
67 2
    public function export()
68
    {
69 2
        openlog($this->identity, $this->options, $this->facility);
70 2
        foreach ($this->messages as $message) {
71 2
            if (syslog($this->_syslogLevels[$message[1]], $this->formatMessage($message)) === false) {
72 2
                throw new LogRuntimeException('Unable to export log through system log!');
73
            }
74
        }
75 1
        closelog();
76 1
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 3
    public function formatMessage($message)
82
    {
83 3
        list($text, $level, $category, $timestamp) = $message;
84 3
        $level = Logger::getLevelName($level);
85 3
        if (!is_string($text)) {
86
            // exceptions may not be serializable if in the call stack somewhere is a Closure
87 2
            if ($text instanceof \Throwable || $text instanceof \Exception) {
88 1
                $text = (string) $text;
89
            } else {
90 1
                $text = VarDumper::export($text);
91
            }
92
        }
93
94 3
        $prefix = $this->getMessagePrefix($message);
95 3
        return "{$prefix}[$level][$category] $text";
96
    }
97
}
98