1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Log\Target\Syslog; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LogLevel; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Yiisoft\Log\Target; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* SyslogTarget writes log to syslog. |
13
|
|
|
*/ |
14
|
|
|
class SyslogTarget extends Target |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string syslog identity |
18
|
|
|
*/ |
19
|
|
|
private string $identity; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int syslog facility. |
23
|
|
|
*/ |
24
|
|
|
private int $facility = LOG_USER; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int openlog options. This is a bitfield passed as the `$option` parameter to [openlog()](http://php.net/openlog). |
28
|
|
|
* Defaults to `LOG_ODELAY | LOG_PID`. |
29
|
|
|
* |
30
|
|
|
* @see http://php.net/openlog for available options. |
31
|
|
|
*/ |
32
|
|
|
private int $options = LOG_ODELAY | LOG_PID; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool Whether the message format was previously set. |
36
|
|
|
*/ |
37
|
|
|
private bool $isMessageFormatSet = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array syslog levels |
41
|
|
|
*/ |
42
|
|
|
private array $syslogLevels = [ |
43
|
|
|
LogLevel::EMERGENCY => LOG_EMERG, |
44
|
|
|
LogLevel::ALERT => LOG_ALERT, |
45
|
|
|
LogLevel::CRITICAL => LOG_CRIT, |
46
|
|
|
LogLevel::ERROR => LOG_ERR, |
47
|
|
|
LogLevel::WARNING => LOG_WARNING, |
48
|
|
|
LogLevel::NOTICE => LOG_NOTICE, |
49
|
|
|
LogLevel::INFO => LOG_INFO, |
50
|
|
|
LogLevel::DEBUG => LOG_DEBUG, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
public function setIdentity(string $identity): self |
54
|
|
|
{ |
55
|
|
|
$this->identity = $identity; |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setFacility(int $facility): self |
60
|
|
|
{ |
61
|
|
|
$this->facility = $facility; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setOptions(int $options): self |
66
|
|
|
{ |
67
|
|
|
$this->options = $options; |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setFormat(callable $format): Target |
72
|
|
|
{ |
73
|
|
|
$this->isMessageFormatSet = true; |
74
|
|
|
return parent::setFormat($format); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Writes log messages to syslog. |
79
|
|
|
* Starting from version 2.0.14, this method throws RuntimeException in case the log can not be exported. |
80
|
|
|
* |
81
|
|
|
* @throws RuntimeException |
82
|
|
|
*/ |
83
|
2 |
|
public function export(): void |
84
|
|
|
{ |
85
|
2 |
|
$messages = $this->getMessages(); |
86
|
2 |
|
openlog($this->identity, $this->options, $this->facility); |
87
|
|
|
|
88
|
2 |
|
if (!$this->isMessageFormatSet) { |
89
|
2 |
|
$this->setFormat(static fn (array $message) => "[{$message[0]}][{$message[2]['category']}] {$message[1]}"); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
foreach ($this->getFormattedMessages() as $key => $message) { |
93
|
2 |
|
if (syslog($this->syslogLevels[$messages[$key][0]], $message) === false) { |
94
|
1 |
|
throw new RuntimeException('Unable to export log through system log.'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
closelog(); |
99
|
1 |
|
} |
100
|
|
|
} |
101
|
|
|
|