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 Psr\Log\LogLevel; |
11
|
|
|
use Yii; |
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 array syslog levels |
31
|
|
|
*/ |
32
|
|
|
private $_syslogLevels = [ |
33
|
|
|
LogLevel::EMERGENCY => LOG_EMERG, |
34
|
|
|
LogLevel::ALERT => LOG_ALERT, |
35
|
|
|
LogLevel::CRITICAL => LOG_CRIT, |
36
|
|
|
LogLevel::ERROR => LOG_ERR, |
37
|
|
|
LogLevel::WARNING => LOG_WARNING, |
38
|
|
|
LogLevel::NOTICE => LOG_NOTICE, |
39
|
|
|
LogLevel::INFO => LOG_INFO, |
40
|
|
|
LogLevel::DEBUG => LOG_DEBUG, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int openlog options. This is a bitfield passed as the `$option` parameter to [openlog()](http://php.net/openlog). |
45
|
|
|
* Defaults to `null` which means to use the default options `LOG_ODELAY | LOG_PID`. |
46
|
|
|
* @see http://php.net/openlog for available options. |
47
|
|
|
* @since 2.0.11 |
48
|
|
|
*/ |
49
|
|
|
public $options; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function init() |
56
|
|
|
{ |
57
|
|
|
parent::init(); |
58
|
|
|
if ($this->options === null) { |
59
|
|
|
$this->options = LOG_ODELAY | LOG_PID; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Writes log messages to syslog |
65
|
|
|
*/ |
66
|
1 |
|
public function export() |
67
|
|
|
{ |
68
|
1 |
|
openlog($this->identity, $this->options, $this->facility); |
69
|
1 |
|
foreach ($this->messages as $message) { |
70
|
1 |
|
syslog($this->_syslogLevels[$message[0]], $this->formatMessage($message)); |
71
|
|
|
} |
72
|
1 |
|
closelog(); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
2 |
|
public function formatMessage($message) |
79
|
|
|
{ |
80
|
2 |
|
[$level, $text, $context] = $message; |
|
|
|
|
81
|
2 |
|
$level = Logger::getLevelName($level); |
|
|
|
|
82
|
2 |
|
$prefix = $this->getMessagePrefix($message); |
83
|
2 |
|
return $prefix. '[' . $level . '][' . ($context['category'] ?? '') . '] ' .$text; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.