1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
14
|
|
|
* all copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Log; |
26
|
|
|
|
27
|
|
|
use Brickoo\Component\IO\Stream\SocketStream; |
28
|
|
|
use Brickoo\Component\IO\Stream\StreamWriter; |
29
|
|
|
use Brickoo\Component\Common\Assert; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* SyslogLogger |
33
|
|
|
* |
34
|
|
|
* Logs to a common syslog server over a socket stream. |
35
|
|
|
* @link http://tools.ietf.org/html/rfc3164 |
36
|
|
|
* @author Celestino Diaz <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
class SyslogLogger implements Logger { |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Declaration of the facility constants. |
42
|
|
|
* @var integer |
43
|
|
|
*/ |
44
|
|
|
const FACILITY_KERNEL = 0; |
45
|
|
|
const FACILITY_USER_LEVEL = 1; |
46
|
|
|
const FACILITY_MAIL = 2; |
47
|
|
|
const FACILITY_DAEMON = 3; |
48
|
|
|
const FACILITY_SECURITY = 4; |
49
|
|
|
const FACILITY_SYSLOGD = 5; |
50
|
|
|
const FACILITY_PRINTER = 6; |
51
|
|
|
const FACILITY_NETWORK = 7; |
52
|
|
|
const FACILITY_UUCP = 8; |
53
|
|
|
const FACILITY_CLOCK = 9; |
54
|
|
|
const FACILITY_AUTH = 10; |
55
|
|
|
const FACILITY_FTP = 11; |
56
|
|
|
const FACILITY_NTP = 12; |
57
|
|
|
const FACILITY_LOG = 13; |
58
|
|
|
const FACILITY_LOG_ALERT = 14; |
59
|
|
|
const FACILITY_CLOCK_DAEMON = 15; |
60
|
|
|
const FACILITY_USER_0 = 16; |
61
|
|
|
const FACILITY_USER_1 = 17; |
62
|
|
|
const FACILITY_USER_2 = 18; |
63
|
|
|
const FACILITY_USER_3 = 19; |
64
|
|
|
const FACILITY_USER_4 = 20; |
65
|
|
|
const FACILITY_USER_5 = 21; |
66
|
|
|
const FACILITY_USER_6 = 22; |
67
|
|
|
const FACILITY_USER_7 = 23; |
68
|
|
|
|
69
|
|
|
/** @var \Brickoo\Component\IO\Stream\SocketStream */ |
70
|
|
|
private $socketStream; |
71
|
|
|
|
72
|
|
|
/** @var string */ |
73
|
|
|
private $hostname; |
74
|
|
|
|
75
|
|
|
/** @var integer */ |
76
|
|
|
private $facility; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Class constructor. |
80
|
|
|
* @param \Brickoo\Component\IO\Stream\SocketStream $socketStream |
81
|
|
|
* @param string $hostname the hostname of the machine running |
82
|
|
|
* @param integer $facility the facility of the sending messages, default USER_0 |
83
|
|
|
* @throws \InvalidArgumentException if an argument is not valid |
84
|
|
|
*/ |
85
|
1 |
|
public function __construct(SocketStream $socketStream, $hostname, $facility = self::FACILITY_USER_0) { |
86
|
1 |
|
Assert::isString($hostname); |
87
|
1 |
|
Assert::isInteger($facility); |
88
|
|
|
|
89
|
1 |
|
$this->socketStream = $socketStream; |
90
|
1 |
|
$this->hostname = $hostname; |
91
|
1 |
|
$this->facility = $facility; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
/** {@inheritDoc} */ |
95
|
2 |
|
public function log($messages, $severity) { |
96
|
2 |
|
Assert::isInteger($severity); |
97
|
|
|
|
98
|
1 |
|
if (!is_array($messages)) { |
99
|
1 |
|
$messages = [$messages]; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
1 |
|
$this->sendMessages($messages, $severity); |
103
|
1 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sends the messages to the syslog server. |
108
|
|
|
* @param array $messages the messages to send |
109
|
|
|
* @param integer $severity the severity of the message(s) |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
1 |
|
private function sendMessages(array $messages, $severity) { |
113
|
1 |
|
$streamWriter = new StreamWriter($this->socketStream->open()); |
114
|
|
|
|
115
|
1 |
|
$messageHeader = $this->getMessageHeader($severity); |
116
|
1 |
|
foreach ($messages as $message) { |
117
|
1 |
|
$streamWriter->write($messageHeader." ".$message); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
1 |
|
$this->socketStream->close(); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns the log message header. |
125
|
|
|
* @param integer $severity the severity of the log message(s) |
126
|
|
|
* @return string the log message header |
127
|
|
|
*/ |
128
|
1 |
|
private function getMessageHeader($severity) { |
129
|
1 |
|
return sprintf("<%d>%s %s", (($this->facility * 8) + $severity), date("c"), $this->hostname); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|