1 | <?php |
||
15 | final class Psr3ErrorHandler implements ErrorHandler |
||
16 | { |
||
17 | /** |
||
18 | * Defines which error levels should be mapped to certain error types by default. |
||
19 | */ |
||
20 | private const DEFAULT_ERROR_LEVEL_MAP = [ |
||
21 | \ParseError::class => LogLevel::CRITICAL, |
||
22 | \Throwable::class => LogLevel::ERROR, |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * The default log level. |
||
27 | */ |
||
28 | private const DEFAULT_LOG_LEVEL = LogLevel::ERROR; |
||
29 | |||
30 | /** |
||
31 | * The key under which the error should be passed to the log context. |
||
32 | */ |
||
33 | public const ERROR_KEY = 'error'; |
||
34 | |||
35 | /** |
||
36 | * @var LoggerInterface |
||
37 | */ |
||
38 | private $logger; |
||
39 | |||
40 | /** |
||
41 | * Defines which error levels should be mapped to certain error types. |
||
42 | * |
||
43 | * Note: The errors are checked in order, so if you want to define fallbacks for classes higher in the tree |
||
44 | * make sure to add them to the end of the map. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | private $levelMap = []; |
||
49 | |||
50 | /** |
||
51 | * Ignores the severity when detecting the log level. |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | private $ignoreSeverity = false; |
||
56 | |||
57 | /** |
||
58 | * Enables the handler to accept detecting non-PSR-3 log levels. |
||
59 | * |
||
60 | * Note: when detecting an invalid level the handler will silently fall back to the default. |
||
61 | * |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $allowNonPsrLevels = false; |
||
65 | |||
66 | public function __construct(LoggerInterface $logger, array $levelMap = []) |
||
73 | |||
74 | /** |
||
75 | * Ignores the severity when detecting the log level. |
||
76 | */ |
||
77 | public function ignoreSeverity(bool $ignoreSeverity = true): void |
||
81 | |||
82 | /** |
||
83 | * Enables the handler to accept detecting non-PSR-3 log levels. |
||
84 | */ |
||
85 | public function allowNonPsrLevels(bool $allowNonPsrLevels = true): void |
||
89 | |||
90 | public function handle(\Throwable $t, array $context = []): void |
||
107 | |||
108 | /** |
||
109 | * Determines the level for the error. |
||
110 | */ |
||
111 | private function getLevel(\Throwable $t, array $context): string |
||
140 | |||
141 | /** |
||
142 | * Checks whether a log level exists. |
||
143 | */ |
||
144 | private function checkLevel(string $level): bool |
||
148 | |||
149 | /** |
||
150 | * Validates whether a log level exists (if non-PSR levels are not allowed). |
||
151 | */ |
||
152 | private function validateLevel(string $level): bool |
||
156 | |||
157 | /** |
||
158 | * Determines the error type. |
||
159 | */ |
||
160 | private function getType(\Throwable $t): string |
||
170 | } |
||
171 |