|
3 | 3 |
|
4 | 4 | class Logger
|
5 | 5 | {
|
6 |
| - const FATAL = 5; |
7 |
| - const ERROR = 4; |
8 |
| - const WARN = 3; |
9 |
| - const INFO = 2; |
10 |
| - const DEBUG = 1; |
11 |
| - |
12 |
| - public $level=2, $stream, $formatter, $datetime_format; |
13 |
| - |
14 |
| - public function __construct($stream) |
15 |
| - { |
16 |
| - $this->stream = is_string($stream)? fopen($stream, 'a') : $stream; |
17 |
| - $this->datetime_format = "D M d H:i:s Y"; |
18 |
| - $this->formatter = function($severity, $datetime, $progname, $msg) { |
19 |
| - return "[$datetime] $severity -- $progname: $msg\n"; |
20 |
| - }; |
21 |
| - } |
22 |
| - |
23 |
| - public function close() |
24 |
| - { |
25 |
| - if(is_resource($this->stream)) fclose($this->stream); |
26 |
| - } |
27 |
| - |
28 |
| - public function info($msg) { return $this->log(Logger::INFO,func_get_args()); } |
29 |
| - public function debug($msg) { return $this->log(Logger::DEBUG,func_get_args()); } |
30 |
| - public function warn($msg) { return $this->log(Logger::WARN,func_get_args()); } |
31 |
| - public function error($msg) { return $this->log(Logger::ERROR,func_get_args()); } |
32 |
| - public function fatal($msg) { return $this->log(Logger::FATAL,func_get_args()); } |
33 |
| - |
34 |
| - //protected |
35 |
| - protected function log($level,$args) |
36 |
| - { |
37 |
| - $prog = ""; |
38 |
| - $message = ""; |
39 |
| - $block = null; |
40 |
| - foreach($args as $arg) |
41 |
| - { |
42 |
| - if(is_string($arg)) $message = $arg; |
43 |
| - if(is_callable($arg)) $block = $arg; |
44 |
| - } |
45 |
| - if($level < $this->level) return false; |
46 |
| - $formatter = $this->formatter; |
47 |
| - if(!is_null($block)) |
48 |
| - { |
49 |
| - $prog = $message; |
50 |
| - $message = $block(); |
51 |
| - } |
52 |
| - return fwrite($this->stream, $formatter($this->severity_label($level),@date($this->datetime_format),$prog,$message)); |
53 |
| - } |
54 |
| - |
55 |
| - protected function severity_label($level) |
56 |
| - { |
57 |
| - $ref = new \ReflectionClass($this); |
58 |
| - foreach($ref->getConstants() as $key=>$value) if($value == $level) return $key; |
59 |
| - return ""; |
60 |
| - } |
| 6 | + const EMERGENCY = 'emergency'; |
| 7 | + const ALERT = 'alert'; |
| 8 | + const CRITICAL = 'critical'; |
| 9 | + const ERROR = 'error'; |
| 10 | + const WARNING = 'warning'; |
| 11 | + const NOTICE = 'notice'; |
| 12 | + const INFO = 'info'; |
| 13 | + const DEBUG = 'debug'; |
| 14 | + |
| 15 | + public $stream, $formatter, $datetime_format; |
| 16 | + |
| 17 | + public function __construct($stream) |
| 18 | + { |
| 19 | + $this->stream = is_string($stream)? fopen($stream, 'a') : $stream; |
| 20 | + $this->datetime_format = "D M d H:i:s Y"; |
| 21 | + $this->formatter = function($severity, $datetime, $msg) { |
| 22 | + return "[$datetime] $severity -- $msg\n"; |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + public function close() |
| 27 | + { |
| 28 | + if(is_resource($this->stream)) fclose($this->stream); |
| 29 | + } |
| 30 | + |
| 31 | + public function emergency($message, array $context = array()) |
| 32 | + { |
| 33 | + $this->log(static::EMERGENCY, $message, $context); |
| 34 | + } |
| 35 | + |
| 36 | + public function alert($message, array $context = array()) |
| 37 | + { |
| 38 | + $this->log(static::ALERT, $message, $context); |
| 39 | + } |
| 40 | + |
| 41 | + public function critical($message, array $context = array()) |
| 42 | + { |
| 43 | + $this->log(static::CRITICAL, $message, $context); |
| 44 | + } |
| 45 | + |
| 46 | + public function error($message, array $context = array()) |
| 47 | + { |
| 48 | + $this->log(static::ERROR, $message, $context); |
| 49 | + } |
| 50 | + |
| 51 | + public function warning($message, array $context = array()) |
| 52 | + { |
| 53 | + $this->log(static::WARNING, $message, $context); |
| 54 | + } |
| 55 | + |
| 56 | + public function notice($message, array $context = array()) |
| 57 | + { |
| 58 | + $this->log(static::NOTICE, $message, $context); |
| 59 | + } |
| 60 | + |
| 61 | + public function info($message, array $context = array()) |
| 62 | + { |
| 63 | + $this->log(static::INFO, $message, $context); |
| 64 | + } |
| 65 | + |
| 66 | + public function debug($message, array $context = array()) |
| 67 | + { |
| 68 | + $this->log(static::DEBUG, $message, $context); |
| 69 | + } |
| 70 | + |
| 71 | + public function log($level, $message, array $context = array()) |
| 72 | + { |
| 73 | + if(!defined(__CLASS__.'::'.strtoupper($level))) { |
| 74 | + throw new \InvalidArgumentException("Log level $level is not defined."); |
| 75 | + } |
| 76 | + |
| 77 | + if(is_callable($message)) $message = $message($context); |
| 78 | + $this->write($level, $message, $this->formatter); |
| 79 | + } |
| 80 | + |
| 81 | + //protected |
| 82 | + protected function write($level, $message, $format) |
| 83 | + { |
| 84 | + fwrite( $this->stream, $format(strtoupper($level), @date($this->datetime_format), $message) ); |
| 85 | + } |
61 | 86 | }
|
0 commit comments