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 \HexMakina\Debugger\Debugger; |
||
17 | use \HexMakina\Interfaces\StateAgentInterface; |
||
18 | |||
19 | class LogLaddy extends \Psr\Log\AbstractLogger |
||
20 | { |
||
21 | public const REPORTING_USER = 'user_messages'; |
||
22 | public const INTERNAL_ERROR = 'error'; |
||
23 | public const USER_EXCEPTION = 'exception'; |
||
24 | public const LOG_LEVEL_SUCCESS = 'ok'; |
||
25 | |||
26 | private $hasHaltingMessages = false; |
||
27 | |||
28 | private $state_agent = null; |
||
29 | private $debugger = null; |
||
30 | |||
31 | public function __construct(StateAgentInterface $agent, Debugger $debug) |
||
32 | { |
||
33 | $this->state_agent = $agent; |
||
34 | $this->debugger = $debug; |
||
35 | $this->setHandlers(); |
||
36 | } |
||
37 | |||
38 | public function setHandlers() |
||
39 | { |
||
40 | set_error_handler([$this, 'errorHandler']); |
||
41 | set_exception_handler([$this, 'exceptionHandler']); |
||
42 | } |
||
43 | |||
44 | public function restoreHandlers() |
||
45 | { |
||
46 | restore_error_handler(); |
||
47 | restore_exception_handler(); |
||
48 | } |
||
49 | |||
50 | /* |
||
51 | * handler for errors |
||
52 | * use set_error_handler('\HexMakina\kadro\Logger\LogLaddy::error_handler') |
||
53 | */ |
||
54 | public function errorHandler($level, $message, $file = '', $line = 0) |
||
55 | { |
||
56 | $loglevel = self::mapErrorLevelToLogLevel($level); |
||
57 | $this->$loglevel($message, ['file' => $file, 'line' => $line, 'trace' => debug_backtrace()]); |
||
58 | } |
||
59 | |||
60 | /* |
||
61 | * static handlers for throwables, |
||
62 | * use set_exception_handler('\HexMakina\kadro\Logger\LogLaddy::exception_handler'); |
||
63 | */ |
||
64 | public function exceptionHandler(\Throwable $throwable) |
||
65 | { |
||
66 | if ($throwable instanceof \Exception) { |
||
67 | $this->alert(self::USER_EXCEPTION, [$throwable]); |
||
68 | } elseif ($throwable instanceof \Error) { |
||
69 | $this->error(self::INTERNAL_ERROR, [$throwable]); |
||
70 | } else { |
||
71 | $this->critical('Caught an unknown Throwable. This breaks everything.', [$throwable]); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | public function systemHalted($level) |
||
76 | { |
||
77 | switch ($level) { |
||
78 | case LogLevel::ERROR: |
||
0 ignored issues
–
show
|
|||
79 | case LogLevel::CRITICAL: |
||
80 | case LogLevel::ALERT: |
||
81 | case LogLevel::EMERGENCY: |
||
82 | return true; |
||
83 | } |
||
84 | return false; |
||
85 | } |
||
86 | |||
87 | // -- Implementation of LoggerInterface::log(), all other methods are in LoggerTrait |
||
88 | |||
89 | public function log($level, $message, array $context = []) |
||
90 | { |
||
91 | $display_error = null; |
||
92 | |||
93 | // --- Handles Throwables (exception_handler()) |
||
94 | if ($message === self::INTERNAL_ERROR || $message === self::USER_EXCEPTION) { |
||
95 | $this->hasHaltingMessages = true; |
||
96 | if (($context = current($context)) !== false) { |
||
97 | $display_error = Debugger::formatThrowable($context); |
||
98 | $display_error .= PHP_EOL . Debugger::tracesToString($context->getTrace(), false); |
||
99 | error_log($display_error); |
||
100 | self::HTTP500($display_error); |
||
101 | } |
||
102 | } elseif ($this->systemHalted($level)) { // analyses error level |
||
103 | $display_error = sprintf( |
||
104 | PHP_EOL . '%s in file %s:%d' . PHP_EOL . '%s', |
||
105 | $level, |
||
106 | Debugger::formatFilename($context['file']), |
||
107 | $context['line'], |
||
108 | $message |
||
109 | ); |
||
110 | |||
111 | error_log($display_error); |
||
112 | |||
113 | $display_error .= PHP_EOL . Debugger::tracesToString($context['trace'], true); |
||
114 | self::HTTP500($display_error); |
||
115 | } else {// --- Handles user messages, through SESSION storage |
||
116 | $this->state_agent->addMessage($level, $message, $context); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | public static function HTTP500($display_error) |
||
121 | { |
||
122 | Debugger::displayErrors($display_error); |
||
123 | http_response_code(500); |
||
124 | } |
||
125 | |||
126 | // -- Allows to know if script must be halted after fatal error |
||
127 | // TODO NEH.. not good |
||
128 | |||
129 | public function hasHaltingMessages() |
||
130 | { |
||
131 | |||
132 | ddt('DEPRECATED CALL halting messages should halt the system, not be detected', __FUNCTION__); |
||
133 | } |
||
134 | |||
135 | |||
136 | // -- Error level mapping from \Psr\Log\LogLevel.php & http://php.net/manual/en/errorfunc.constants.php |
||
137 | /** Error level meaning , from \Psr\Log\LogLevel.php |
||
138 | * const EMERGENCY = 'emergency'; // System is unusable. |
||
139 | * const ALERT = 'alert'; // Action must be taken immediately, Example: Entire website down, database unavailable, etc. |
||
140 | * const CRITICAL = 'critical'; // Application component unavailable, unexpected exception. |
||
141 | * const ERROR = 'error'; // Run time errors that do not require immediate action |
||
142 | * const WARNING = 'warning'; // Exceptional occurrences that are not errors, undesirable things that are not necessarily wrong |
||
143 | * const NOTICE = 'notice'; // Normal but significant events. |
||
144 | * const INFO = 'info'; // Interesting events. User logs in, SQL logs. |
||
145 | * const DEBUG = 'debug'; // Detailed debug information. |
||
146 | */ |
||
147 | private static function mapErrorLevelToLogLevel($level): string |
||
148 | { |
||
149 | // http://php.net/manual/en/errorfunc.constants.php |
||
150 | $m = []; |
||
151 | |||
152 | $m[E_ERROR] = $m[E_PARSE] = $m[E_CORE_ERROR] = $m[E_COMPILE_ERROR] = $m[E_USER_ERROR] = $m[E_RECOVERABLE_ERROR] = LogLevel::ALERT; |
||
153 | $m[1] = $m[4] = $m[16] = $m[64] = $m[256] = $m[4096] = LogLevel::ALERT; |
||
154 | |||
155 | $m[E_WARNING] = $m[E_CORE_WARNING] = $m[E_COMPILE_WARNING] = $m[E_USER_WARNING] = LogLevel::CRITICAL; |
||
156 | $m[2] = $m[32] = $m[128] = $m[512] = LogLevel::CRITICAL; |
||
157 | |||
158 | $m[E_NOTICE] = $m[E_USER_NOTICE] = LogLevel::ERROR; |
||
159 | $m[8] = $m[1024] = LogLevel::ERROR; |
||
160 | |||
161 | $m[E_STRICT] = $m[E_DEPRECATED] = $m[E_USER_DEPRECATED] = $m[E_ALL] = LogLevel::DEBUG; |
||
162 | $m[2048] = $m[8192] = $m[16384] = $m[32767] = LogLevel::DEBUG; |
||
163 | |||
164 | if (isset($m[$level])) { |
||
165 | return $m[$level]; |
||
166 | } |
||
167 | |||
168 | throw new \Exception(__FUNCTION__ . "($level): $level is unknown"); |
||
169 | } |
||
170 | } |
||
171 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths