|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* LogLaddy |
|
5
|
|
|
* |
|
6
|
|
|
* I carry a log – yes. Is it funny to you? It is not to me. |
|
7
|
|
|
* Behind all things are reasons. Reasons can even explain the absurd. |
|
8
|
|
|
* |
|
9
|
|
|
* LogLaddy manages error reporting |
|
10
|
|
|
* PSR-3 Compliant, with a NICE bonus |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace HexMakina\LogLaddy; |
|
14
|
|
|
|
|
15
|
|
|
// Debugger |
|
16
|
|
|
use Psr\Log\LogLevel; |
|
17
|
|
|
use HexMakina\Debugger\Debugger; |
|
18
|
|
|
use HexMakina\BlackBox\StateAgentInterface; |
|
19
|
|
|
|
|
20
|
|
|
class LogLaddy extends \Psr\Log\AbstractLogger |
|
21
|
|
|
{ |
|
22
|
|
|
private $state_agent = null; |
|
23
|
|
|
private static $level_mapping = null; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public function __construct(StateAgentInterface $agent = null) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->state_agent = $agent; |
|
29
|
|
|
$this->setHandlers(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function setHandlers() |
|
33
|
|
|
{ |
|
34
|
|
|
set_error_handler([$this, 'errorHandler']); |
|
35
|
|
|
set_exception_handler([$this, 'exceptionHandler']); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function restoreHandlers() |
|
39
|
|
|
{ |
|
40
|
|
|
restore_error_handler(); |
|
41
|
|
|
restore_exception_handler(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/* |
|
45
|
|
|
* handler for errors |
|
46
|
|
|
* use set_error_handler('\HexMakina\kadro\Logger\LogLaddy::error_handler') |
|
47
|
|
|
*/ |
|
48
|
|
|
public function errorHandler($level, $message, $file = '', $line = 0) |
|
49
|
|
|
{ |
|
50
|
|
|
$loglevel = self::mapErrorLevelToLogLevel($level); |
|
51
|
|
|
$this->$loglevel($message, ['file' => $file, 'line' => $line, 'trace' => debug_backtrace()]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/* |
|
55
|
|
|
* static handlers for throwables, |
|
56
|
|
|
* use set_exception_handler('\HexMakina\kadro\Logger\LogLaddy::exception_handler'); |
|
57
|
|
|
*/ |
|
58
|
|
|
public function exceptionHandler(\Throwable $throwable) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->critical(Debugger::formatThrowable($throwable), $throwable->getTrace()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function log($level, $message, array $context = []) |
|
64
|
|
|
{ |
|
65
|
|
|
switch ($level) { |
|
66
|
|
|
case LogLevel::DEBUG: |
|
67
|
|
|
Debugger::visualDump($context, $message, true); |
|
68
|
|
|
break; |
|
69
|
|
|
|
|
70
|
|
|
case LogLevel::INFO: |
|
71
|
|
|
case LogLevel::NOTICE: |
|
72
|
|
|
case LogLevel::WARNING: |
|
73
|
|
|
if (is_null($this->state_agent)) { |
|
74
|
|
|
Debugger::visualDump($context, $message, true); |
|
75
|
|
|
} else { |
|
76
|
|
|
$this->state_agent->addMessage($level, $message, $context); |
|
77
|
|
|
} |
|
78
|
|
|
break; |
|
79
|
|
|
|
|
80
|
|
|
case LogLevel::CRITICAL: |
|
81
|
|
|
case LogLevel::ALERT: |
|
82
|
|
|
case LogLevel::EMERGENCY: |
|
83
|
|
|
// if dev, show, else logto file |
|
84
|
|
|
echo Debugger::toHTML($message, $level, $context, true); |
|
|
|
|
|
|
85
|
|
|
http_response_code(500); |
|
86
|
|
|
die; |
|
|
|
|
|
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
// -- Error level mapping from \Psr\Log\LogLevel.php & http://php.net/manual/en/errorfunc.constants.php |
|
93
|
|
|
/** Error level meaning , from \Psr\Log\LogLevel.php |
|
94
|
|
|
* const EMERGENCY = 'emergency'; |
|
95
|
|
|
* // System is unusable. |
|
96
|
|
|
* const ALERT = 'alert'; |
|
97
|
|
|
* // Action must be taken immediately, Example: Entire website down, database unavailable, etc. |
|
98
|
|
|
* const CRITICAL = 'critical'; |
|
99
|
|
|
* // Application component unavailable, unexpected exception. |
|
100
|
|
|
* const ERROR = 'error'; |
|
101
|
|
|
* // Run time errors that do not require immediate action |
|
102
|
|
|
* const WARNING = 'warning'; |
|
103
|
|
|
* // Exceptional occurrences that are not errors, undesirable things that are not necessarily wrong |
|
104
|
|
|
* const NOTICE = 'notice'; |
|
105
|
|
|
* // Normal but significant events. |
|
106
|
|
|
* const INFO = 'info'; |
|
107
|
|
|
* // Interesting events. User logs in, SQL logs. |
|
108
|
|
|
* const DEBUG = 'debug'; |
|
109
|
|
|
* // Detailed debug information. |
|
110
|
|
|
*/ |
|
111
|
|
|
private static function mapErrorLevelToLogLevel($level): string |
|
112
|
|
|
{ |
|
113
|
|
|
// http://php.net/manual/en/errorfunc.constants.php |
|
114
|
|
|
if (is_null(self::$level_mapping)) { |
|
115
|
|
|
self::$level_mapping = [ |
|
116
|
|
|
E_ERROR => LogLevel::ALERT, |
|
117
|
|
|
E_PARSE => LogLevel::ALERT, |
|
118
|
|
|
E_CORE_ERROR => LogLevel::ALERT, |
|
119
|
|
|
E_COMPILE_ERROR => LogLevel::ALERT, |
|
120
|
|
|
E_USER_ERROR => LogLevel::ALERT, |
|
121
|
|
|
E_RECOVERABLE_ERROR => LogLevel::ALERT, |
|
122
|
|
|
1 => LogLevel::ALERT, |
|
123
|
|
|
4 => LogLevel::ALERT, |
|
124
|
|
|
16 => LogLevel::ALERT, |
|
125
|
|
|
64 => LogLevel::ALERT, |
|
126
|
|
|
256 => LogLevel::ALERT, |
|
127
|
|
|
4096 => LogLevel::ALERT, |
|
128
|
|
|
|
|
129
|
|
|
E_WARNING => LogLevel::CRITICAL, |
|
130
|
|
|
E_CORE_WARNING => LogLevel::CRITICAL, |
|
131
|
|
|
E_COMPILE_WARNING => LogLevel::CRITICAL, |
|
132
|
|
|
E_USER_WARNING => LogLevel::CRITICAL, |
|
133
|
|
|
2 => LogLevel::CRITICAL, |
|
134
|
|
|
32 => LogLevel::CRITICAL, |
|
135
|
|
|
128 => LogLevel::CRITICAL, |
|
136
|
|
|
512 => LogLevel::CRITICAL, |
|
137
|
|
|
|
|
138
|
|
|
E_NOTICE => LogLevel::ERROR, |
|
139
|
|
|
E_USER_NOTICE => LogLevel::ERROR, |
|
140
|
|
|
8 => LogLevel::ERROR, |
|
141
|
|
|
1024 => LogLevel::ERROR, |
|
142
|
|
|
|
|
143
|
|
|
E_STRICT => LogLevel::DEBUG, |
|
144
|
|
|
E_DEPRECATED => LogLevel::DEBUG, |
|
145
|
|
|
E_USER_DEPRECATED => LogLevel::DEBUG, |
|
146
|
|
|
E_ALL => LogLevel::DEBUG, |
|
147
|
|
|
2048 => LogLevel::DEBUG, |
|
148
|
|
|
8192 => LogLevel::DEBUG, |
|
149
|
|
|
16384 => LogLevel::DEBUG, |
|
150
|
|
|
32767 => LogLevel::DEBUG, |
|
151
|
|
|
]; |
|
152
|
|
|
} |
|
153
|
|
|
if (!isset(self::$level_mapping[$level])) { |
|
154
|
|
|
throw new \Exception(__FUNCTION__ . "($level): $level is unknown"); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return self::$level_mapping[$level]; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.