Passed
Push — master ( c0e684...0f43c3 )
by Alexander
02:12
created

SyslogTarget::setFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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