This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorlog.php
349 lines (305 loc) · 10 KB
/
errorlog.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* @category ErrorLog
* @package ErrorLog
* @copyright Copyright (c) 2010, Bellière Ludovic
* @license http://opensource.org/licenses/mit-license.php MIT license
*/
require_once 'ErrorLog/Exception.php';
class ErrorLog {
//
const EMERG = 0; // Emergency: system is unusable
const ALERT = 1; // Alert: action must be taken immediately
const CRIT = 2; // Critical: critical conditions
const ERR = 3; // Error: error conditions
const WARN = 4; // Warning: warning conditions
const NOTICE = 5; // Notice: normal, but significant, condition
const INFO = 6; // Informational: informational messages
const DEBUG = 7; // Debug: debug-level messages
const LOG_NONE = 0x0010; // log type unreconized
const LOG_MESSAGE = 0x0011; // simple message
const LOG_PHPERROR = 0x0012; // php error, can be lethal
const LOG_EXCEPTION = 0x0013; // exception, lethal
protected static $_instance = null;
protected $dump_session_data = true;
protected $previous_error_handler = false;
protected $previous_exception_handler = false;
protected $writers = null;
protected $writers_path = null;
/**
* Highter value of this will be ignored.
*
* @var integer
*/
protected $_logLevel = 5;
/**
* List of writer where filters don't apply.
* @var array
*/
protected $_logLevelException = array();
protected function __construct($config)
{
$this->writers_path = dirname(__FILE__).'/ErrorLog/Writer/';
if (!is_array($config))
{
$config = (array) $config;
}
if (isset($config['writers']) && is_array($config['writers']))
{
foreach($config['writers'] as $writer => $writer_config)
{
self::loadWriter($writer, $writer_config);
}
}
if (isset($config['dump_session_data']))
{
$this->dump_session_data = (bool) $config['dump_session_data'];
}
if (isset($config['logLevel'])) {
if (self::getErrorLevelLabel($config['logLevel']) !== false) {
self::setLogLevel($config['logLevel']);
}
if (isset($config['logLevelException']))
{
$this->_logLevelException = (array) $config['logLevelException'];
}
}
}
protected static function init($config)
{
return static::$_instance = new static($config);
}
/**
* Return an instance of the object or null if the value of $auto_create is false.
*
* @param boolean $auto_create
* @return ErrorLog|null
*/
public static function getInstance($config = array(), $auto_create = true)
{
if ((bool) $auto_create && is_null(static::$_instance)) {
static::init($config);
}
return static::$_instance;
}
/**
*
*/
public function write($message, $severity, $file='', $line=0, $backtrace='', $extra=array())
{
if (empty($message))
{
throw new ErrorLog_Exception('First parameter is required.');
}
if (!is_int($severity))
{
throw new ErrorLog_Exception('Second parameter is invalid.');
}
$logType = self::LOG_NONE;
if (isset($extra['logType']))
{
$logType = $extra['logType'];
unset($extra['logType']);
}
$logData = array(
'message' => $message,
'severity' => $severity,
'file' => $file,
'line' => $line,
'raised' => date('c'),
'backtrace' => $backtrace,
'params' => array(
'post' => $_POST,
'get' => $_GET,
'cookie' => $_COOKIE
),
'env' => $_SERVER,
);
if ($this->dump_session_data && isset($_SESSION)) {
$logData['session'] = $_SESSION;
}
if (!empty($extra))
{
if (is_array($extra))
{
$info = array();
foreach ($extra as $key => $value)
{
if (is_string($key))
{
$logData[$key] = $value;
}
else
{
$info[] = $value;
}
}
} else
{
$info = $extra;
}
if (!empty($info))
{
$logData['info'] = $info;
}
}
self::_write($logData, $logType);
}
/**
* Send data to the loaded writers.
* Apply a basic filter on the severity level.
*
* @param array $logData
* @param integer $logType
*/
protected function _write($logData, $logType) {
foreach($this->writers as $writername => $writer)
{
if (!in_array($writername, $this->_logLevelException) && $logData['severity'] <= $this->_logLevel)
{
$writer->store($logData,$logType);
}
}
}
/**
* Load a writer.
* If self::registerExceptionHandler() is already called, it restore the default php handler before throwing anything.
*
* @param string $writer
* @param array $config
* @throw ErrorLog_Exception on file/object missing
* @return ErrorLog
*/
protected function loadWriter($writer, $config = array())
{
if (!is_array($config))
{
$config = (array) $config;
}
$writer = ucfirst($writer);
if (file_exists($this->writers_path . $writer . '.php'))
{
require $this->writers_path . $writer . '.php';
$writer_name = 'ErrorLog_Writer_' . $writer;
if (class_exists($writer_name))
{
if (empty($config))
{
$this->writers[strtolower($writer)] = new $writer_name();
}
else
{
$this->writers[strtolower($writer)] = new $writer_name($config);
}
}
else
{
if ($this->previous_exception_handler !== false)
{
restore_exception_handler();
}
throw new ErrorLog_Exception('Writer object "' . $writer_name . '" couldn\'t be found.');
}
}
else
{
if ($this->previous_exception_handler !== false)
{
restore_exception_handler();
}
throw new ErrorLog_Exception('The file for the writer ' . $writer . ' couldn\'t be found.');
}
return $this;
}
public function setLogLevel($loglevel)
{
$this->_logLevel = $loglevel;
}
public function logException(Exception $exception)
{
$extra = array('logType' => self::LOG_EXCEPTION);
$this->write($exception->getMessage(), $exception->getCode(), $exception->getFile(), $exception->getLine(), $exception->getTraceAsString(), $extra);
}
public function logPhpError($errno, $errstr, $errfile='', $errline=0, array $errcontext=array())
{
$data = array('logType' => self::LOG_PHPERROR);
if (!empty($errorcontext))
{
$data['errorcontext'] = $errorcontext;
}
$this->write($errstr, $errno, $errfile, $errline, debug_backtrace(), $data);
}
/**
* @param string $message
*/
public function log($message)
{
if (empty($message) || !is_string($message))
{
return false;
}
$this->write($message, self::INFO, '', 0, '', array('logType' => self::LOG_MESSAGE));
return true;
}
/**
* @param string $message
* @param int $errorlevel
*/
public function warn($message, $errorlevel=4)
{
if (empty($message) || !is_string($message))
{
return false;
}
$this->write($message, $errorlevel, '', 0, '', array('logType' => self::LOG_MESSAGE));
return true;
}
/**
* @param string $message
* @param int $errorlevel
*/
public function error($message, $file, $line, $severity=3)
{
if (empty($message) || !is_string($message))
{
return false;
}
$this->write($message, $severity, $file, $line, debug_backtrace(), array('logType' => self::LOG_MESSAGE));
return true;
}
public function registerErrorHandler($level=false)
{
if ($level === false)
{
$level = E_ALL | E_STRICT;
}
$this->previous_error_handler = set_error_handler(array($this, 'logPhpError'), $level);
}
public function registerExceptionHandler()
{
$this->previous_exception_handler = set_exception_handler(array($this, 'logException'));
}
static public function getErrorLevelLabel($errorLevel)
{
$r = new ReflectionClass('ErrorLog');
$constants = array_flip($r->getConstants());
if (array_key_exists($errorLevel, $constants) && strpos($constants[$errorLevel], 'LOG_') === false)
{
return $constants[$errorLevel];
}
else
{
return false;
}
}
public function __destruct()
{
if (!empty($this->writers))
{
foreach ($this->writers as $name => $object)
{
$object->close();
}
}
}
}