1 | <?php |
||
39 | class ErrorlogHandler |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * Log |
||
44 | * |
||
45 | * @var Logger |
||
46 | * |
||
47 | * @access protected |
||
48 | */ |
||
49 | protected $logger; |
||
50 | |||
51 | /** |
||
52 | * Rethrow exception? |
||
53 | * |
||
54 | * @var bool |
||
55 | * |
||
56 | * @access protected |
||
57 | */ |
||
58 | protected $rethrow = true; |
||
59 | |||
60 | /** |
||
61 | * Level |
||
62 | * |
||
63 | * @var string |
||
64 | * |
||
65 | * @access protected |
||
66 | */ |
||
67 | protected $level = Level::ALERT; |
||
68 | |||
69 | /** |
||
70 | * Create an ErrorlogHandler |
||
71 | * |
||
72 | * @param Logger $logger Psr\Log logger |
||
73 | * |
||
74 | * @access public |
||
75 | */ |
||
76 | 4 | public function __construct(Logger $logger) |
|
80 | |||
81 | /** |
||
82 | * Set log level to use |
||
83 | * |
||
84 | * @param mixed $level Level to log exceptions |
||
85 | * |
||
86 | * @return $this |
||
87 | * |
||
88 | * @access public |
||
89 | */ |
||
90 | 1 | public function setLogLevel($level) |
|
95 | |||
96 | /** |
||
97 | * Set if handler should retrhow exception |
||
98 | * |
||
99 | * @param bool $bool exception is re thrown if true |
||
100 | * |
||
101 | * @return $this |
||
102 | * |
||
103 | * @access public |
||
104 | */ |
||
105 | 1 | public function setRethrow($bool) |
|
110 | |||
111 | /** |
||
112 | * Log an exception if thrown |
||
113 | * |
||
114 | * @param Request $request PSR7 HTTP Request |
||
115 | * @param Response $response PSR7 HTTP Response |
||
116 | * @param callable $next Next callable middleware |
||
117 | * |
||
118 | * @return Response |
||
119 | * |
||
120 | * @access public |
||
121 | * |
||
122 | * @throws Exception throws caught exception if rethrow is true |
||
123 | */ |
||
124 | 4 | public function __invoke(Request $request, Response $response, callable $next) |
|
136 | |||
137 | /** |
||
138 | * Log |
||
139 | * |
||
140 | * @param Exception $exception Exception to log |
||
141 | * |
||
142 | * @return void |
||
143 | * |
||
144 | * @access protected |
||
145 | */ |
||
146 | 3 | protected function log(Exception $exception) |
|
147 | { |
||
148 | 3 | $this->logger->log( |
|
149 | 3 | $this->level, |
|
150 | 3 | $this->formatException($exception), |
|
151 | 3 | ['exception' => $exception] |
|
152 | ); |
||
153 | 3 | } |
|
154 | |||
155 | /** |
||
156 | * Format Exception into log message |
||
157 | * |
||
158 | * @param Exception $exception Exception to log |
||
159 | * |
||
160 | * @return string |
||
161 | * |
||
162 | * @access protected |
||
163 | */ |
||
164 | 3 | protected function formatException(Exception $exception) |
|
165 | { |
||
166 | 3 | return sprintf( |
|
167 | 3 | 'Uncaught Exception %s: "%s" at %s line %s', |
|
168 | get_class($exception), |
||
169 | 3 | $exception->getMessage(), |
|
170 | 3 | $exception->getFile(), |
|
171 | 3 | $exception->getLine() |
|
172 | ); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Format response to return |
||
177 | * |
||
178 | * @param Exception $exception Caught exception |
||
179 | * @param Response $response Response object |
||
180 | * |
||
181 | * @return Response |
||
182 | * |
||
183 | * @access protected |
||
184 | */ |
||
185 | 1 | protected function formatResponse(Exception $exception, Response $response) |
|
196 | } |
||
197 |