Passed
Push — master ( 858802...245853 )
by Alexander
02:40 queued 47s
created

SyslogTarget::export()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 string that is prefixed to each message.
31
     *
32
     * @see https://www.php.net/openlog
33
     */
34
    private string $identity;
35
36
    /**
37
     * @var int Bit options to be used when generating a log message.
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 Used to specify what type of program is logging the message. This allows you to specify (in your
47
     * machine's syslog configuration) how messages coming from different facilities will be handled.
48
     *
49
     * Defaults to `LOG_USER`.
50
     *
51
     * @see https://www.php.net/openlog
52
     */
53
    private int $facility;
54
55
    /**
56
     * @var array Syslog levels.
57
     */
58
    private array $syslogLevels = [
59
        LogLevel::EMERGENCY => LOG_EMERG,
60
        LogLevel::ALERT => LOG_ALERT,
61
        LogLevel::CRITICAL => LOG_CRIT,
62
        LogLevel::ERROR => LOG_ERR,
63
        LogLevel::WARNING => LOG_WARNING,
64
        LogLevel::NOTICE => LOG_NOTICE,
65
        LogLevel::INFO => LOG_INFO,
66
        LogLevel::DEBUG => LOG_DEBUG,
67
    ];
68
69
    /**
70
     * @param string $identity The string that is prefixed to each message.
71
     * @param int $options Bit options to be used when generating a log message.
72
     * @param int $facility Used to specify what type of program is logging the message. This allows you to specify (in your
73
     * machine's syslog configuration) how messages coming from different facilities will be handled.
74
     */
75 3
    public function __construct(string $identity, int $options = LOG_ODELAY | LOG_PID, int $facility = LOG_USER)
76
    {
77 3
        $this->identity = $identity;
78 3
        $this->options = $options;
79 3
        $this->facility = $facility;
80 3
        parent::__construct();
81
82 3
        $this->setFormat(static function (Message $message) {
83 2
            return "[{$message->level()}][{$message->context('category', '')}] {$message->message()}";
84 3
        });
85 3
    }
86
87
    /**
88
     * Writes log messages to syslog.
89
     *
90
     * @see https://www.php.net/openlog
91
     * @see https://www.php.net/syslog
92
     * @see https://www.php.net/closelog
93
     *
94
     * @throws RuntimeException If unable to export log through system log.
95
     */
96 3
    protected function export(): void
97
    {
98 3
        $formattedMessages = $this->getFormattedMessages();
99 3
        openlog($this->identity, $this->options, $this->facility);
100
101 3
        foreach ($this->getMessages() as $key => $message) {
102 3
            if (syslog($this->syslogLevels[$message->level()], $formattedMessages[$key]) === false) {
103 1
                throw new RuntimeException('Unable to export log through system log.');
104
            }
105
        }
106
107 2
        closelog();
108 2
    }
109
}
110