1 | <?php |
||
52 | final class ExceptionLogger implements LoggerInterface, ResettableInterface |
||
53 | { |
||
54 | use LoggerTrait; |
||
55 | |||
56 | /** |
||
57 | * Exception threshold |
||
58 | * |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $threshold; |
||
62 | |||
63 | private $decoratedLogger; |
||
64 | |||
65 | /** |
||
66 | * Constructor |
||
67 | */ |
||
68 | 9 | public function __construct($threshold = Logger::ERROR) |
|
73 | |||
74 | /** |
||
75 | * Throws an exception for all messages with error level or higher |
||
76 | * |
||
77 | * @param mixed $level The log level |
||
78 | * @param string $message The log message |
||
79 | * @param array $context The log context |
||
80 | * |
||
81 | * @throws \Exception Exception that occured |
||
82 | * @throws \RuntimeException Log message as exception |
||
83 | */ |
||
84 | 17 | public function log($level, $message, array $context = []) |
|
85 | { |
||
86 | 17 | $level = Logger::toMonologLevel($level); |
|
87 | |||
88 | 17 | if ($this->isTriggered($level)) { |
|
89 | 3 | throw $this->getContextException($context) ?: new RuntimeException($message, $level); |
|
90 | } |
||
91 | |||
92 | 15 | $this->decoratedLogger->addRecord($level, $message, $context); |
|
93 | 15 | } |
|
94 | |||
95 | /** |
||
96 | * Return whether an exception should be triggered |
||
97 | * |
||
98 | * @param int $level Log level |
||
99 | * |
||
100 | * @return bool Exception should be triggered |
||
101 | */ |
||
102 | 17 | protected function isTriggered($level) |
|
106 | |||
107 | /** |
||
108 | * Return the context exception (if any) |
||
109 | * |
||
110 | * @param array $context Context |
||
111 | * |
||
112 | * @return \Exception|null Context exception |
||
113 | */ |
||
114 | 3 | protected function getContextException(array $context) |
|
115 | { |
||
116 | 3 | return (isset($context['exception']) && ($context['exception'] instanceof \Exception)) ? |
|
117 | 3 | $context['exception'] : null; |
|
118 | } |
||
119 | |||
120 | public function reset() |
||
124 | } |
||
125 |