Completed
Push — bash-completion ( 164352...1454d3 )
by Carsten
86:55 queued 83:59
created

SyslogTarget::formatMessage()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 10
nc 3
nop 1
crap 4
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()](http://php.net/openlog).
31
     * Defaults to `null` which means to use the default options `LOG_ODELAY | LOG_PID`.
32
     * @see http://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
     * @inheritdoc
52
     */
53
    public function init()
54
    {
55
        parent::init();
56
        if ($this->options === null) {
57
            $this->options = LOG_ODELAY | LOG_PID;
58
        }
59
    }
60
61
    /**
62
     * Writes log messages to syslog
63
     */
64 1
    public function export()
65
    {
66 1
        openlog($this->identity, $this->options, $this->facility);
67 1
        foreach ($this->messages as $message) {
68 1
            syslog($this->_syslogLevels[$message[1]], $this->formatMessage($message));
69 1
        }
70 1
        closelog();
71 1
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 3
    public function formatMessage($message)
77
    {
78 3
        list($text, $level, $category, $timestamp) = $message;
0 ignored issues
show
Unused Code introduced by
The assignment to $timestamp is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
79 3
        $level = Logger::getLevelName($level);
80 3
        if (!is_string($text)) {
81
            // exceptions may not be serializable if in the call stack somewhere is a Closure
82 2
            if ($text instanceof \Throwable || $text instanceof \Exception) {
83 1
                $text = (string) $text;
84 1
            } else {
85 1
                $text = VarDumper::export($text);
86
            }
87 2
        }
88
89 3
        $prefix = $this->getMessagePrefix($message);
90 3
        return "{$prefix}[$level][$category] $text";
91
    }
92
}
93