1 | <?php |
||
39 | class ErrorlogHandler |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * Log |
||
44 | * |
||
45 | * @var Logger |
||
46 | * |
||
47 | * @access protected |
||
48 | */ |
||
49 | protected $log; |
||
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) |
|
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) |
|
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 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: