SyslogHandler::write()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Logger
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Logger\Handler;
16
17
use Phossa2\Logger\LogLevel;
18
use Phossa2\Logger\Message\Message;
19
use Phossa2\Logger\Entry\LogEntryInterface;
20
use Phossa2\Logger\Exception\LogicException;
21
use Phossa2\Logger\Formatter\FormatterInterface;
22
23
/**
24
 * SyslogHandler
25
 *
26
 * Log to syslog
27
 *
28
 * @package Phossa2\Logger
29
 * @author  Hong Zhang <[email protected]>
30
 * @see     HandlerAbstract
31
 * @version 2.0.0
32
 * @since   2.0.0 added
33
 * @since   2.0.1 updated constructor
34
 */
35
class SyslogHandler extends HandlerAbstract
36
{
37
    /**
38
     * syslog facility
39
     *
40
     * @var    int
41
     * @access protected
42
     */
43
    protected $facility;
44
45
    /**
46
     * syslog options
47
     *
48
     * @var    int
49
     * @access protected
50
     */
51
    protected $logopts;
52
53
    /**
54
     * syslog priorities
55
     *
56
     * @var    array
57
     * @access protected
58
     */
59
    protected $priorities = [
60
        LogLevel::DEBUG     => LOG_DEBUG,
61
        LogLevel::INFO      => LOG_INFO,
62
        LogLevel::NOTICE    => LOG_NOTICE,
63
        LogLevel::WARNING   => LOG_WARNING,
64
        LogLevel::ERROR     => LOG_ERR,
65
        LogLevel::CRITICAL  => LOG_CRIT,
66
        LogLevel::ALERT     => LOG_ALERT,
67
        LogLevel::EMERGENCY => LOG_EMERG,
68
    ];
69
70
    /**
71
     * @param  int $facility
72
     * @param  int $logOpts
73
     * @param  FormatterInterface $formatter
74
     * @param  bool $stopPropagation
75
     * @access public
76
     * @since  2.0.1 removed level param
77
     */
78
    public function __construct(
79
        /*# int */ $facility = LOG_USER,
80
        /*# int */ $logOpts = LOG_PID,
81
        FormatterInterface $formatter = null,
82
        /*# bool */ $stopPropagation = false
83
    ) {
84
        $this->facility = $facility;
85
        $this->logopts  = $logOpts;
86
        parent::__construct($formatter, $stopPropagation);
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    protected function write(LogEntryInterface $logEntry)
93
    {
94
        $ident = $logEntry->getChannel();
95
96
        if (!openlog($ident, $this->logopts, $this->facility)) {
97
            throw new LogicException(
98
                Message::get(Message::LOG_SYSLOG_FAIL, $ident, $this->facility),
99
                Message::LOG_SYSLOG_FAIL
100
            );
101
        }
102
103
        syslog(
104
            $this->priorities[$logEntry->getLevel()],
105
            $logEntry->getFormatted()
106
        );
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    protected function close()
113
    {
114
        closelog();
115
    }
116
}
117