-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathl8.php
executable file
·260 lines (215 loc) · 8.61 KB
/
l8.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* L8.php
* High-speed, zero-dependancy RFC-5424 compatable redis-backed logger for PHP.
*
* Copyright (C) 2014 Alan McFarlane <[email protected]>
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
**/
class L8 {
// context version
const VERSION = '1.1.0';
// redis configuration
public static $server = 'localhost:6379';
public static $db = 1;
public static $channel = 'L8'; // NB will also use <$channel>_seq
// syslog facilities
const DEBUG = 1;
const INFO = 2;
const NOTICE = 4;
const WARNING = 8;
const ERROR = 16;
const CRITICAL = 32;
const ALERT = 64;
const EMERGENCY = 128;
// data source
const LOG_STATEMENT = 1;
const ERROR_HANDLER = 2;
const EXCEPTION_HANDLER = 3;
// internals
protected static $socket;
protected static $domain;
/* -- */
public static function debug($message, array $context = array()) {
return static::log(static::DEBUG, $message, $context);
}
public static function info($message, array $context = array()) {
return static::log(static::INFO, $message, $context);
}
public static function notice($message, array $context = array()) {
return static::log(static::NOTICE, $message, $context);
}
public static function warning($message, array $context = array()) {
return static::log(static::WARNING, $message, $context);
}
public static function error($message, array $context = array()) {
return static::log(static::ERROR, $message, $context);
}
public static function critical($message, array $context = array()) {
return static::log(static::CRITICAL, $message, $context);
}
public static function alert($message, array $context = array()) {
return static::log(static::ALERT, $message, $context);
}
public static function emergency($message, array $context = array()) {
return static::log(static::EMERGENCY, $message, $context);
}
/* -- */
protected static function log($level, $message, array $context = array()) {
if (count($trace = debug_backtrace(false, 2)) > 1) {
$file = $trace[1]['file'];
$line = $trace[1]['line'];
} else {
$file = '';
$line = 0;
}
return static::write($level, static::LOG_STATEMENT, $message, $file,
$line, $context);
}
/* -- */
public static function error_handler($errno, $message, $file = '', $line = 0,
$context = array()) {
if (!is_array($context)) {
$context = array($context);
}
$map = array(
E_ERROR => array('E_ERROR', static::ERROR),
E_WARNING => array('E_WARNING', static::WARNING),
E_PARSE => array('E_PARSE', static::ERROR),
E_NOTICE => array('E_NOTICE', static::NOTICE),
E_CORE_ERROR => array('E_CORE_ERROR', static::ERROR),
E_CORE_WARNING => array('E_CORE_WARNING', static::WARNING),
E_COMPILE_ERROR => array('E_COMPILE_ERROR', static::ERROR),
E_COMPILE_WARNING => array('E_COMPILE_WARNING', static::WARNING),
E_USER_ERROR => array('E_USER_ERROR', static::ERROR),
E_USER_WARNING => array('E_USER_WARNING', static::WARNING),
E_USER_NOTICE => array('E_USER_NOTICE', static::NOTICE),
E_STRICT => array('E_STRICT', static::WARNING),
E_RECOVERABLE_ERROR => array('E_RECOVERABLE_ERROR', static::WARNING),
E_DEPRECATED => array('E_DEPRECATED', static::WARNING),
E_USER_DEPRECATED => array('E_USER_DEPRECATED', static::WARNING),
);
list($prefix, $level) = array_key_exists($errno, $map)
? $map[$errno]
: array('E_UNKOWN(' . $errno . ')', static::ERROR);
return static::write($level, static::ERROR_HANDLER, $prefix . ':' .
$message, $file, $line, $context);
}
public static function exception_handler($e) {
return static::write(static::ERROR, static::EXCEPTION_HANDLER,
$e->getMessage(),
$e->getFile(), $e->getLine(), array());
}
/* -- */
protected static function write($level, $source, $message, $file, $line,
$context) {
// connect to redis server
if (!isset(static::$socket)) {
static::$socket = stream_socket_client(static::$server);
if (static::$socket) {
static::select(static::$db);
static::$domain = array_key_exists('SERVER_NAME', $_SERVER)
? strtolower($_SERVER['SERVER_NAME'])
: null;
}
}
// make sure the socket was opened
if (!static::$socket) {
return false;
}
// generate the atomic log entry id #
$id = static::incr(self::$channel . '.seq');
if ($id == '9223372036854775807') { // 2^63-1
$redis->set(self::$channel . '.seq', 0);
}
// construct the data record
$record = json_encode(
array(
'version' => static::VERSION,
'id' => $id,
'domain' => static::$domain,
'time' => time(),
'level' => $level,
'source' => $source,
'message' => $message,
'filename' => $file,
'line' => $line,
'context' => base64_encode(json_encode($context)),
)
);
static::publish(self::$channel, $record);
return true;
}
/* -- */
protected static function incr($key) {
return static::execute(array('INCR', $key));
}
protected static function publish($channel, $message) {
return static::execute(array('PUBLISH', $channel, $message));
}
protected static function select($index) {
return static::execute(array('SELECT', $index));
}
protected static function set($key, $value) {
return static::execute(array('SET', $key, $value));
}
/* -- */
protected static function execute($args) {
$cmd = '*' . count($args) . "\r\n";
foreach ($args as $arg) {
$cmd .= '$' . strlen($arg) . "\r\n" . $arg . "\r\n";
}
fwrite(static::$socket, $cmd);
return static::parseResponse();
}
protected static function parseResponse() {
$line = fgets(static::$socket);
list($type, $result) = array(
$line[0],
substr($line, 1,
strlen($line) - 3)
);
if ($type == '-') {
throw new Exception($result);
} elseif ($type == '$') {
if ($result == -1) {
$result = null;
} else {
$line = fread(static::$socket, $result + 2);
$result = substr($line, 0, strlen($line) - 2);
}
} elseif ($type == '*') {
$count = (int)$result;
for ($i = 0, $result = array(); $i < $count; $i++) {
$result[] = static::parseResponse();
}
}
return $result;
}
}