|
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\RuntimeException; |
|
21
|
|
|
use Phossa2\Logger\Formatter\FormatterInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* SyslogHandler |
|
25
|
|
|
* |
|
26
|
|
|
* @package Phossa2\Logger |
|
27
|
|
|
* @author Hong Zhang <[email protected]> |
|
28
|
|
|
* @see HandlerAbstract |
|
29
|
|
|
* @version 2.0.0 |
|
30
|
|
|
* @since 2.0.0 added |
|
31
|
|
|
*/ |
|
32
|
|
|
class SyslogHandler extends HandlerAbstract |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* syslog facility |
|
36
|
|
|
* |
|
37
|
|
|
* @var int |
|
38
|
|
|
* @access protected |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $facility; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* syslog options |
|
44
|
|
|
* |
|
45
|
|
|
* @var int |
|
46
|
|
|
* @access protected |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $logopts; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* syslog priorities |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
* @access protected |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $priorities = [ |
|
57
|
|
|
LogLevel::DEBUG => LOG_DEBUG, |
|
58
|
|
|
LogLevel::INFO => LOG_INFO, |
|
59
|
|
|
LogLevel::NOTICE => LOG_NOTICE, |
|
60
|
|
|
LogLevel::WARNING => LOG_WARNING, |
|
61
|
|
|
LogLevel::ERROR => LOG_ERR, |
|
62
|
|
|
LogLevel::CRITICAL => LOG_CRIT, |
|
63
|
|
|
LogLevel::ALERT => LOG_ALERT, |
|
64
|
|
|
LogLevel::EMERGENCY => LOG_EMERG, |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param int $facility |
|
69
|
|
|
* @param int $logOpts |
|
70
|
|
|
* @param string $level |
|
71
|
|
|
* @param FormatterInterface $formatter |
|
72
|
|
|
* @param bool $stopPropagation |
|
73
|
|
|
* @access public |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct( |
|
76
|
|
|
/*# int */ $facility = LOG_USER, |
|
77
|
|
|
/*# int */ $logOpts = LOG_PID, |
|
78
|
|
|
/*# string */ $level = LogLevel::WARNING, |
|
79
|
|
|
FormatterInterface $formatter = null, |
|
80
|
|
|
/*# bool */ $stopPropagation = false |
|
81
|
|
|
) { |
|
82
|
|
|
$this->facility = $facility; |
|
83
|
|
|
$this->logopts = $logOpts; |
|
84
|
|
|
parent::__construct($level, $formatter, $stopPropagation); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritDoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function write(LogEntryInterface $logEntry) |
|
91
|
|
|
{ |
|
92
|
|
|
$ident = $logEntry->getChannel(); |
|
93
|
|
|
|
|
94
|
|
|
if (!openlog($ident, $this->logopts, $this->facility)) { |
|
95
|
|
|
throw new RuntimeException( |
|
96
|
|
|
Message::get(Message::LOG_SYSLOG_FAIL, $ident, $this->facility), |
|
97
|
|
|
Message::LOG_SYSLOG_FAIL |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
syslog( |
|
103
|
|
|
$this->priorities[$logEntry->getLevel()], |
|
104
|
|
|
$logEntry->getFormatted() |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritDoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function close() |
|
112
|
|
|
{ |
|
113
|
|
|
closelog(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|