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
|
|
|
class LogLaddy implements LoggerInterface |
16
|
|
|
{ |
17
|
|
|
use \Psr\Log\LoggerTrait; // PSR implementation |
18
|
|
|
use \HexMakina\Debugger\Debugger; // Debugger |
19
|
|
|
|
20
|
|
|
const REPORTING_USER = 'user_messages'; |
21
|
|
|
const INTERNAL_ERROR = 'error'; |
22
|
|
|
const USER_EXCEPTION = 'exception'; |
23
|
|
|
const LOG_LEVEL_SUCCESS = 'ok'; |
24
|
|
|
|
25
|
|
|
private $has_halting_messages = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Everything went fine, which is always nice. |
29
|
|
|
* LogLaddy is a bit more optimistic than PSRLog |
30
|
|
|
* @param string $message |
31
|
|
|
* @param array $context |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
public function nice($message, array $context = array()) |
37
|
|
|
{ |
38
|
|
|
$this->log(LogLevel::NICE, $message, $context); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// -- Static handlers for error, use set_error_handler('\HexMakina\kadro\Logger\LogLaddy::error_handler') |
42
|
|
|
public static function error_handler($level, $message, $file = '', $line = 0) |
43
|
|
|
{ |
44
|
|
|
$loglevel = self::map_error_level_to_log_level($level); |
45
|
|
|
|
46
|
|
|
(new LogLaddy())->$loglevel($message, ['file' => $file, 'line' => $line, 'trace' => debug_backtrace()]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// -- Static handlers for throwables, use set_exception_handler('\HexMakina\kadro\Logger\LogLaddy::exception_handler'); |
50
|
|
|
public static function exception_handler(\Throwable $throwable) |
51
|
|
|
{ |
52
|
|
|
$context = []; |
53
|
|
|
$context['text'] = $throwable->getMessage(); |
54
|
|
|
$context['file'] = $throwable->getFile(); |
55
|
|
|
$context['line'] = $throwable->getLine(); |
56
|
|
|
$context['code'] = $throwable->getCode(); |
57
|
|
|
$context['class'] = get_class($throwable); |
58
|
|
|
$context['trace'] = $throwable->getTrace(); |
59
|
|
|
|
60
|
|
|
$lad = new LogLaddy(); |
61
|
|
|
if (is_subclass_of($throwable, 'Error') || get_class($throwable) === 'Error') { |
62
|
|
|
$lad->alert(self::INTERNAL_ERROR, $context); |
63
|
|
|
} elseif (is_subclass_of($throwable, 'Exception') || get_class($throwable) === 'Exception') { |
64
|
|
|
$lad->notice(self::USER_EXCEPTION, $context); |
65
|
|
|
} else { |
66
|
|
|
$lad->critical('Caught a Throwable that is not an Error or an Exception. This breaks everything.', $context); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function system_halted($level) |
71
|
|
|
{ |
72
|
|
|
switch ($level) { |
73
|
|
|
case LogLevel::ERROR: |
74
|
|
|
case LogLevel::CRITICAL: |
75
|
|
|
case LogLevel::ALERT: |
76
|
|
|
case LogLevel::EMERGENCY: |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// -- Implementation of LoggerInterface::log(), all other methods are in LoggerTrait |
83
|
|
|
|
84
|
|
|
public function log($level, $message, array $context = []) |
85
|
|
|
{ |
86
|
|
|
$display_error = null; |
87
|
|
|
|
88
|
|
|
// --- Handles Throwables (exception_handler()) |
89
|
|
|
if ($message == self::INTERNAL_ERROR || $message == self::USER_EXCEPTION) { |
90
|
|
|
$this->has_halting_messages = true; |
91
|
|
|
$display_error = self::format_throwable_message($context['class'], $context['code'], $context['file'], $context['line'], $context['text']); |
92
|
|
|
error_log($display_error); |
93
|
|
|
$display_error .= self::format_trace($context['trace'], false); |
94
|
|
|
self::HTTP_500($display_error); |
95
|
|
|
} elseif ($this->system_halted($level)) { // analyses error level |
96
|
|
|
$display_error = sprintf(PHP_EOL . '%s in file %s:%d' . PHP_EOL . '%s', $level, self::format_file($context['file']), $context['line'], $message); |
97
|
|
|
error_log($display_error); |
98
|
|
|
$display_error .= self::format_trace($context['trace'], true); |
99
|
|
|
self::HTTP_500($display_error); |
100
|
|
|
} else {// --- Handles user messages, through SESSION storage |
101
|
|
|
$this->report_to_user($level, $message, $context); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function HTTP_500($display_error) |
106
|
|
|
{ |
107
|
|
|
self::display_errors($display_error); |
108
|
|
|
http_response_code(500); |
109
|
|
|
die; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
// -- Allows to know if script must be halted after fatal error |
112
|
|
|
// TODO NEH.. not good |
113
|
|
|
public function has_halting_messages() |
114
|
|
|
{ |
115
|
|
|
return $this->has_halting_messages === true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// -- User messages |
119
|
|
|
|
120
|
|
|
// -- User messages:add one |
121
|
|
|
public function report_to_user($level, $message, $context = []) |
122
|
|
|
{ |
123
|
|
|
if (!isset($_SESSION[self::REPORTING_USER])) { |
124
|
|
|
$_SESSION[self::REPORTING_USER] = []; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (!isset($_SESSION[self::REPORTING_USER][$level])) { |
128
|
|
|
$_SESSION[self::REPORTING_USER][$level] = []; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$_SESSION[self::REPORTING_USER][$level][] = [$message, $context]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// -- User messages:get all |
135
|
|
|
public function get_user_report() |
136
|
|
|
{ |
137
|
|
|
return $_SESSION[self::REPORTING_USER] ?? []; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// -- User messages:reset all |
141
|
|
|
public function clean_user_report() |
142
|
|
|
{ |
143
|
|
|
unset($_SESSION[self::REPORTING_USER]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// -- Error level mapping from \Psr\Log\LogLevel.php & http://php.net/manual/en/errorfunc.constants.php |
147
|
|
|
/** Error level meaning , from \Psr\Log\LogLevel.php |
148
|
|
|
* const EMERGENCY = 'emergency'; // System is unusable. |
149
|
|
|
* const ALERT = 'alert'; // Action must be taken immediately, Example: Entire website down, database unavailable, etc. |
150
|
|
|
* const CRITICAL = 'critical'; // Application component unavailable, unexpected exception. |
151
|
|
|
* const ERROR = 'error'; // Run time errors that do not require immediate action |
152
|
|
|
* const WARNING = 'warning'; // Exceptional occurrences that are not errors, undesirable things that are not necessarily wrong |
153
|
|
|
* const NOTICE = 'notice'; // Normal but significant events. |
154
|
|
|
* const INFO = 'info'; // Interesting events. User logs in, SQL logs. |
155
|
|
|
* const DEBUG = 'debug'; // Detailed debug information. |
156
|
|
|
*/ |
157
|
|
|
private static function map_error_level_to_log_level($level): string |
158
|
|
|
{ |
159
|
|
|
// http://php.net/manual/en/errorfunc.constants.php |
160
|
|
|
$m = []; |
161
|
|
|
|
162
|
|
|
$m[E_ERROR] = $m[E_PARSE] = $m[E_CORE_ERROR] = $m[E_COMPILE_ERROR] = $m[E_USER_ERROR] = $m[E_RECOVERABLE_ERROR] = LogLevel::ALERT; |
163
|
|
|
$m[1] = $m[4] = $m[16] = $m[64] = $m[256] = $m[4096] = LogLevel::ALERT; |
164
|
|
|
|
165
|
|
|
$m[E_WARNING] = $m[E_CORE_WARNING] = $m[E_COMPILE_WARNING] = $m[E_USER_WARNING] = LogLevel::CRITICAL; |
166
|
|
|
$m[2] = $m[32] = $m[128] = $m[512] = LogLevel::CRITICAL; |
167
|
|
|
|
168
|
|
|
$m[E_NOTICE] = $m[E_USER_NOTICE] = LogLevel::ERROR; |
169
|
|
|
$m[8] = $m[1024] = LogLevel::ERROR; |
170
|
|
|
|
171
|
|
|
$m[E_STRICT] = $m[E_DEPRECATED] = $m[E_USER_DEPRECATED] = $m[E_ALL] = LogLevel::DEBUG; |
172
|
|
|
$m[2048] = $m[8192] = $m[16384] = $m[32767] = LogLevel::DEBUG; |
173
|
|
|
|
174
|
|
|
if (isset($m[$level])) { |
175
|
|
|
return $m[$level]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
throw new \Exception(__FUNCTION__ . "($level): $level is unknown"); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.