1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\LogLaddy; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LogLevel; |
6
|
|
|
|
7
|
|
|
class PHPErrorToPSRLevel |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var array<int,string> $level_mapping |
12
|
|
|
*/ |
13
|
|
|
private static array $level_mapping = []; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
if (empty(self::$level_mapping)) { |
19
|
|
|
self::createErrorLevelMap(); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function map(int $error): string |
24
|
|
|
{ |
25
|
|
|
if (!isset(self::$level_mapping[$error])) { |
26
|
|
|
throw new \Exception(sprintf('%s(%d): %d is unknown', __FUNCTION__, $error, $error)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return self::$level_mapping[$error]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Error level meaning, from \Psr\Log\LogLevel.php |
34
|
|
|
* |
35
|
|
|
* const EMERGENCY = 'emergency'; |
36
|
|
|
* // System is unusable. |
37
|
|
|
* const ALERT = 'alert'; |
38
|
|
|
* // Action must be taken immediately, Example: Entire website down, database unavailable, etc. |
39
|
|
|
* const CRITICAL = 'critical'; |
40
|
|
|
* // Application component unavailable, unexpected exception. |
41
|
|
|
* const ERROR = 'error'; |
42
|
|
|
* // Run time errors that do not require immediate action |
43
|
|
|
* const WARNING = 'warning'; |
44
|
|
|
* // Exceptional occurrences that are not errors, undesirable things that are not necessarily wrong |
45
|
|
|
* const NOTICE = 'notice'; |
46
|
|
|
* // Normal but significant events. |
47
|
|
|
* const INFO = 'info'; |
48
|
|
|
* // Interesting events. User logs in, SQL logs. |
49
|
|
|
* const DEBUG = 'debug'; |
50
|
|
|
* // Detailed debug information. |
51
|
|
|
* |
52
|
|
|
* Error level mapping from \Psr\Log\LogLevel.php & http://php.net/manual/en/errorfunc.constants.php |
53
|
|
|
*/ |
54
|
|
|
private static function createErrorLevelMap(): void |
55
|
|
|
{ |
56
|
|
|
$errorLevels = [ |
57
|
|
|
LogLevel::CRITICAL => [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR], |
58
|
|
|
LogLevel::ERROR => [E_WARNING, E_CORE_WARNING, E_COMPILE_WARNING, E_USER_WARNING], |
59
|
|
|
LogLevel::DEBUG => [E_NOTICE, E_USER_NOTICE, E_STRICT, E_DEPRECATED, E_USER_DEPRECATED, E_ALL], |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
self::$level_mapping = []; |
63
|
|
|
|
64
|
|
|
foreach ($errorLevels as $logLevel => $errors) { |
65
|
|
|
self::$level_mapping += array_fill_keys($errors, $logLevel); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// $critical = array_fill_keys( |
69
|
|
|
// [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR], |
70
|
|
|
// LogLevel::CRITICAL |
71
|
|
|
// ); |
72
|
|
|
|
73
|
|
|
// $error = array_fill_keys( |
74
|
|
|
// [E_WARNING, E_CORE_WARNING, E_COMPILE_WARNING, E_USER_WARNING], |
75
|
|
|
// LogLevel::ERROR |
76
|
|
|
// ); |
77
|
|
|
|
78
|
|
|
// $debug = array_fill_keys( |
79
|
|
|
// [E_NOTICE, E_USER_NOTICE, E_STRICT, E_DEPRECATED, E_USER_DEPRECATED, E_ALL], |
80
|
|
|
// LogLevel::DEBUG |
81
|
|
|
// ); |
82
|
|
|
|
83
|
|
|
// self::$level_mapping = $critical + $error + $debug; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|