Passed
Pull Request — master (#14)
by Evgeniy
08:54 queued 10s
created

SyslogTarget   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 79
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A export() 0 12 3
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\Message;
10
use Yiisoft\Log\Target;
11
12
use const LOG_ALERT;
13
use const LOG_CRIT;
14
use const LOG_DEBUG;
15
use const LOG_EMERG;
16
use const LOG_ERR;
17
use const LOG_INFO;
18
use const LOG_NOTICE;
19
use const LOG_ODELAY;
20
use const LOG_PID;
21
use const LOG_USER;
22
use const LOG_WARNING;
23
24
/**
25
 * SyslogTarget writes log to syslog.
26
 */
27
final class SyslogTarget extends Target
28
{
29
    /**
30
     * @var string The openlog identity. This is a bitfield passed as the `$ident` parameter to `openlog()`.
31
     *
32
     * @see https://www.php.net/openlog
33
     */
34
    private string $identity;
35
36
    /**
37
     * @var int The openlog options. This is a bitfield passed as the `$option` parameter to `openlog()`.
38
     *
39
     * Defaults to `LOG_ODELAY | LOG_PID`.
40
     *
41
     * @see https://www.php.net/openlog
42
     */
43
    private int $options;
44
45
    /**
46
     * @var int The openlog facility. his is a bitfield passed as the `$facility` parameter to `openlog()`.
47
     *
48
     * Defaults to `LOG_USER`.
49
     *
50
     * @see https://www.php.net/openlog
51
     */
52
    private int $facility;
53
54
    /**
55
     * @var array The syslog levels.
56
     */
57
    private array $syslogLevels = [
58
        LogLevel::EMERGENCY => LOG_EMERG,
59
        LogLevel::ALERT => LOG_ALERT,
60
        LogLevel::CRITICAL => LOG_CRIT,
61
        LogLevel::ERROR => LOG_ERR,
62
        LogLevel::WARNING => LOG_WARNING,
63
        LogLevel::NOTICE => LOG_NOTICE,
64
        LogLevel::INFO => LOG_INFO,
65
        LogLevel::DEBUG => LOG_DEBUG,
66
    ];
67
68
    /**
69
     * @param string $identity The openlog identity. This is a bitfield passed as the `$ident` parameter to `openlog()`.
70
     * @param int $options The openlog options. This is a bitfield passed as the `$option` parameter to `openlog()`.
71
     * @param int $facility The openlog facility. his is a bitfield passed as the `$facility` parameter to `openlog()`.
72
     */
73 3
    public function __construct(string $identity, int $options = LOG_ODELAY | LOG_PID, int $facility = LOG_USER)
74
    {
75 3
        $this->identity = $identity;
76 3
        $this->options = $options;
77 3
        $this->facility = $facility;
78 3
        parent::__construct();
79
80 3
        $this->setFormat(static function (Message $message) {
81 2
            return "[{$message->level()}][{$message->context('category', '')}] {$message->message()}";
82 3
        });
83 3
    }
84
85
    /**
86
     * Writes log messages to syslog.
87
     *
88
     * @see https://www.php.net/openlog
89
     * @see https://www.php.net/syslog
90
     * @see https://www.php.net/closelog
91
     *
92
     * @throws RuntimeException If unable to export log through system log.
93
     */
94 3
    protected function export(): void
95
    {
96 3
        $formattedMessages = $this->getFormattedMessages();
97 3
        openlog($this->identity, $this->options, $this->facility);
98
99 3
        foreach ($this->getMessages() as $key => $message) {
100 3
            if (syslog($this->syslogLevels[$message->level()], $formattedMessages[$key]) === false) {
101 1
                throw new RuntimeException('Unable to export log through system log.');
102
            }
103
        }
104
105 2
        closelog();
106 2
    }
107
}
108