-
Notifications
You must be signed in to change notification settings - Fork 35
/
Logger.php
496 lines (449 loc) · 13.8 KB
/
Logger.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?php
/**
* Finally, a light, permissions-checking logging class.
*
* Originally written for use with wpSearch
*
* Usage:
* $log = new Logger('/var/log/', LogLevel::INFO);
* $log->info('Returned a million search results'); //Prints to the log file
* $log->error('Oh dear.'); //Prints to the log file
* $log->debug('x = 5'); //Prints nothing due to current severity threshhold
*
* @author Kenny Katzgrau <katzgrau@gmail.com>
* @since July 26, 2008
* @link https://github.com/katzgrau/KLogger
* @version 1.0.0
* @modified by 李茂祥limx 2018
*/
class LogLevel
{
const EMERGENCY = '紧急情况 emergency';
const ALERT = '严重警报 alert';
const CRITICAL = '重要问题 critical';
const ERROR = '一般错误 error';
const WARNING = '警告 warning';
const NOTICE = '提示 notice';
const INFO = '信息 info';
const DEBUG = '调试 debug';
}
interface LoggerInterface
{
/**
* System is unusable.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function emergency($message, array $context = array());
/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function alert($message, array $context = array());
/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function critical($message, array $context = array());
/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function error($message, array $context = array());
/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function warning($message, array $context = array());
/**
* Normal but significant events.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function notice($message, array $context = array());
/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function info($message, array $context = array());
/**
* Detailed debug information.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function debug($message, array $context = array());
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log($level, $message, array $context = array());
}
abstract class AbstractLogger implements LoggerInterface
{
public function emergency($message, array $context = array())
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}
public function alert($message, array $context = array())
{
$this->log(LogLevel::ALERT, $message, $context);
}
public function critical($message, array $context = array())
{
$this->log(LogLevel::CRITICAL, $message, $context);
}
public function error($message, array $context = array())
{
$this->log(LogLevel::ERROR, $message, $context);
}
public function warning($message, array $context = array())
{
$this->log(LogLevel::WARNING, $message, $context);
}
public function notice($message, array $context = array())
{
$this->log(LogLevel::NOTICE, $message, $context);
}
public function info($message, array $context = array())
{
$this->log(LogLevel::INFO, $message, $context);
}
public function debug($message, array $context = array())
{
$this->log(LogLevel::DEBUG, $message, $context);
}
}
class Logger extends AbstractLogger
{
/**
* KLogger options
* Anything options not considered 'core' to the logging library should be
* settable view the third parameter in the constructor
*
* Core options include the log file path and the log threshold
*
* @var array
*/
protected $options = array (
'extension' => 'txt',
'dateFormat' => 'Y-m-d H:i:s.u',
'filename' => false,
'flushFrequency' => false,
'prefix' => 'log_',
'logFormat' => false,
'appendContext' => true,
);
/**
* Path to the log file
* @var string
*/
private $logFilePath;
/**
* Current minimum logging threshold
* @var integer
*/
protected $logLevelThreshold = LogLevel::DEBUG;
/**
* The number of lines logged in this instance's lifetime
* @var int
*/
private $logLineCount = 0;
/**
* Log Levels
* @var array
*/
protected $logLevels = array(
LogLevel::EMERGENCY => 0,
LogLevel::ALERT => 1,
LogLevel::CRITICAL => 2,
LogLevel::ERROR => 3,
LogLevel::WARNING => 4,
LogLevel::NOTICE => 5,
LogLevel::INFO => 6,
LogLevel::DEBUG => 7
);
/**
* This holds the file handle for this instance's log file
* @var resource
*/
private $fileHandle;
/**
* This holds the last line logged to the logger
* Used for unit tests
* @var string
*/
private $lastLine = '';
/**
* Octal notation for default permissions of the log file
* @var integer
*/
private $defaultPermissions = 0777;
/**
* Class constructor
*
* @param string $logDirectory File path to the logging directory
* @param string $logLevelThreshold The LogLevel Threshold
* @param array $options
*
* @internal param string $logFilePrefix The prefix for the log file name
* @internal param string $logFileExt The extension for the log file
*/
public function __construct($logDirectory, $logLevelThreshold = LogLevel::DEBUG, array $options = array())
{
$this->logLevelThreshold = $logLevelThreshold;
$this->options = array_merge($this->options, $options);
$logDirectory = rtrim($logDirectory, DIRECTORY_SEPARATOR);
if ( ! file_exists($logDirectory)) {
mkdir($logDirectory, $this->defaultPermissions, true);
}
if(strpos($logDirectory, 'php://') === 0) {
$this->setLogToStdOut($logDirectory);
$this->setFileHandle('w+');
} else {
$this->setLogFilePath($logDirectory);
if(file_exists($this->logFilePath) && !is_writable($this->logFilePath)) {
throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.');
}
$this->setFileHandle('a');
}
if ( ! $this->fileHandle) {
throw new RuntimeException('The file could not be opened. Check permissions.');
}
}
/**
* @param string $stdOutPath
*/
public function setLogToStdOut($stdOutPath) {
$this->logFilePath = $stdOutPath;
}
/**
* @param string $logDirectory
*/
public function setLogFilePath($logDirectory) {
if ($this->options['filename']) {
if (strpos($this->options['filename'], '.log') !== false || strpos($this->options['filename'], '.txt') !== false) {
$this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'];
}
else {
$this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'].'.'.$this->options['extension'];
}
} else {
$this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['prefix'].date('Y-m-d').'.'.$this->options['extension'];
}
}
/**
* @param $writeMode
*
* @internal param resource $fileHandle
*/
public function setFileHandle($writeMode) {
$this->fileHandle = fopen($this->logFilePath, $writeMode);
}
/**
* Class destructor
*/
public function __destruct()
{
if ($this->fileHandle) {
fclose($this->fileHandle);
}
}
/**
* Sets the date format used by all instances of KLogger
*
* @param string $dateFormat Valid format string for date()
*/
public function setDateFormat($dateFormat)
{
$this->options['dateFormat'] = $dateFormat;
}
/**
* Sets the Log Level Threshold
*
* @param string $logLevelThreshold The log level threshold
*/
public function setLogLevelThreshold($logLevelThreshold)
{
$this->logLevelThreshold = $logLevelThreshold;
}
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return null
*/
public function log($level, $message, array $context = array())
{
if ($this->logLevels[$this->logLevelThreshold] < $this->logLevels[$level]) {
return;
}
$message = $this->formatMessage($level, $message, $context);
$this->write($message);
}
/**
* Writes a line to the log without prepending a status or timestamp
*
* @param string $message Line to write to the log
* @return void
*/
public function write($message)
{
if (null !== $this->fileHandle) {
if (fwrite($this->fileHandle, $message) === false) {
die('Logger文件无法写入,请注意写入权限!The file could not be written to. Check that appropriate permissions have been set.');
} else {
$this->lastLine = trim($message);
$this->logLineCount++;
if ($this->options['flushFrequency'] && $this->logLineCount % $this->options['flushFrequency'] === 0) {
fflush($this->fileHandle);
}
}
}
}
/**
* Get the file path that the log is currently writing to
*
* @return string
*/
public function getLogFilePath()
{
return $this->logFilePath;
}
/**
* Get the last line logged to the log file
*
* @return string
*/
public function getLastLogLine()
{
return $this->lastLine;
}
/**
* Formats the message for logging.
*
* @param string $level The Log Level of the message
* @param string $message The message to log
* @param array $context The context
* @return string
*/
protected function formatMessage($level, $message, $context)
{
if ($this->options['logFormat']) {
$parts = array(
'date' => $this->getTimestamp(),
'level' => strtoupper($level),
'level-padding' => str_repeat(' ', 9 - strlen($level)),
'priority' => $this->logLevels[$level],
'message' => $message,
'context' => json_encode($context),
);
$message = $this->options['logFormat'];
foreach ($parts as $part => $value) {
$message = str_replace('{'.$part.'}', $value, $message);
}
} else {
$message = "[{$this->getTimestamp()}] [{$level}] {$message}";
}
if ($this->options['appendContext'] && ! empty($context)) {
$message .= PHP_EOL.$this->indent($this->contextToString($context));
}
return $message.PHP_EOL;
}
/**
* Gets the correctly formatted Date/Time for the log entry.
*
* PHP DateTime is dump, and you have to resort to trickery to get microseconds
* to work correctly, so here it is.
*
* @return string
*/
private function getTimestamp()
{
$originalTime = microtime(true);
$micro = sprintf("%06d", ($originalTime - floor($originalTime)) * 1000000);
$date = new DateTime(date('Y-m-d H:i:s.'.$micro, $originalTime));
return $date->format($this->options['dateFormat']);
}
/**
* Takes the given context and coverts it to a string.
*
* @param array $context The Context
* @return string
*/
protected function contextToString($context)
{
$export = '';
foreach ($context as $key => $value) {
$export .= "{$key}: ";
$export .= preg_replace(array(
'/=>\s+([a-zA-Z])/im',
'/array\(\s+\)/im',
'/^ |\G /m'
), array(
'=> $1',
'array()',
' '
), str_replace('array (', 'array(', var_export($value, true)));
$export .= PHP_EOL;
}
return str_replace(array('\\\\', '\\\''), array('\\', '\''), rtrim($export));
}
/**
* Indents the given string with the given indent.
*
* @param string $string The string to indent
* @param string $indent What to use as the indent.
* @return string
*/
protected function indent($string, $indent = ' ')
{
return $indent.str_replace("\n", "\n".$indent, $string);
}
}