@@ -123,6 +123,87 @@ public function testLog()
123123 $ middleware ->post ($ response , 'abc ' , $ options );
124124 }
125125
126+ public function testLogWithCustomMessage ()
127+ {
128+ $ options = [
129+ LoggerMiddleware::class => [
130+ Options::LEVEL => LogLevel::DEBUG ,
131+ Options::ERROR_LEVEL => LogLevel::ERROR ,
132+ Options::URL_LEVEL => LogLevel::DEBUG ,
133+ Options::MESSAGE_PRE => '[{{transaction_id}}] Sending {{request.method}} request to {{request.uri}} ' ,
134+ Options::MESSAGE_POST => '[{{transaction_id}}] Response came back with HTTP code {{response.status_code}} ' ,
135+ Options::IGNORE_HEADERS => [
136+ 'X-Ignore-Request ' ,
137+ 'X-Ignore-Response ' ,
138+ ],
139+ Options::IGNORE_URI_QUERY_ITEMS => [
140+ 'strip_this_item ' ,
141+ ],
142+ ],
143+ ];
144+ $ request = new Request (
145+ 'GET ' ,
146+ 'https://example.com/?strip_this_item=0&dont_strip_this_item=1 ' ,
147+ [
148+ 'X-Foo ' => 'bar ' ,
149+ 'X-Ignore-Request ' => 'nope ' ,
150+ ]
151+ );
152+ $ response = new Response (
153+ 200 ,
154+ [
155+ 'X-Bar ' => 'foo ' ,
156+ 'X-Ignore-Response ' => 'nope ' ,
157+ ]
158+ );
159+
160+ $ logger = $ this ->prophesize (LoggerInterface::class);
161+ $ logger ->log (
162+ LogLevel::DEBUG ,
163+ '[abc] Sending GET request to https://example.com/?dont_strip_this_item=1 ' ,
164+ [
165+ 'transaction_id ' => 'abc ' ,
166+ 'request ' => [
167+ 'method ' => 'GET ' ,
168+ 'uri ' => 'https://example.com/?dont_strip_this_item=1 ' ,
169+ 'protocol_version ' => '1.1 ' ,
170+ 'headers ' => [
171+ 'Host ' => ['example.com ' ],
172+ 'X-Foo ' => ['bar ' ],
173+ ],
174+ ],
175+ ]
176+ )->shouldBeCalled ();
177+ $ logger ->log (
178+ LogLevel::DEBUG ,
179+ '[abc] Response came back with HTTP code 200 ' ,
180+ [
181+ 'transaction_id ' => 'abc ' ,
182+ 'request ' => [
183+ 'method ' => 'GET ' ,
184+ 'uri ' => 'https://example.com/?dont_strip_this_item=1 ' ,
185+ 'protocol_version ' => '1.1 ' ,
186+ 'headers ' => [
187+ 'Host ' => ['example.com ' ],
188+ 'X-Foo ' => ['bar ' ],
189+ ],
190+ ],
191+ 'response ' => [
192+ 'status_code ' => 200 ,
193+ 'status_reason ' => 'OK ' ,
194+ 'protocol_version ' => '1.1 ' ,
195+ 'headers ' => [
196+ 'X-Bar ' => ['foo ' ],
197+ ],
198+ ],
199+ ]
200+ )->shouldBeCalled ();
201+
202+ $ middleware = new LoggerMiddleware ($ logger ->reveal ());
203+ $ middleware ->pre ($ request , 'abc ' , $ options );
204+ $ middleware ->post ($ response , 'abc ' , $ options );
205+ }
206+
126207 public function testLogError ()
127208 {
128209 $ options = [
@@ -245,4 +326,57 @@ public function testLogErrorNoResponse()
245326 $ middleware ->pre ($ request , 'abc ' , $ options );
246327 $ middleware ->error ($ exception , 'abc ' , $ options );
247328 }
329+
330+ public function testLogErrorNoResponseCustomMessagee ()
331+ {
332+ $ options = [
333+ LoggerMiddleware::class => [
334+ Options::LEVEL => LogLevel::DEBUG ,
335+ Options::ERROR_LEVEL => LogLevel::ERROR ,
336+ Options::MESSAGE_ERROR => '[{{transaction_id}}] "{{error.message}}": {{error.code}} ' ,
337+ Options::IGNORE_HEADERS => [
338+ 'X-Ignore-Request ' ,
339+ 'X-Ignore-Response ' ,
340+ ],
341+ ],
342+ ];
343+ $ request = new Request (
344+ 'GET ' ,
345+ 'https://example.com/ ' ,
346+ [
347+ 'X-Foo ' => 'bar ' ,
348+ 'X-Ignore-Request ' => 'nope ' ,
349+ ]
350+ );
351+ $ exception = new Exception ('New Exception ' );
352+
353+ $ logger = $ this ->prophesize (LoggerInterface::class);
354+ $ logger ->log (
355+ LogLevel::ERROR ,
356+ '[abc] " ' . $ exception ->getMessage () . '": ' . $ exception ->getCode (),
357+ [
358+ 'transaction_id ' => 'abc ' ,
359+ 'request ' => [
360+ 'method ' => 'GET ' ,
361+ 'uri ' => 'https://example.com/ ' ,
362+ 'protocol_version ' => '1.1 ' ,
363+ 'headers ' => [
364+ 'Host ' => ['example.com ' ],
365+ 'X-Foo ' => ['bar ' ],
366+ ],
367+ ],
368+ 'error ' => [
369+ 'message ' => $ exception ->getMessage (),
370+ 'code ' => $ exception ->getCode (),
371+ 'file ' => $ exception ->getFile (),
372+ 'line ' => $ exception ->getLine (),
373+ 'trace ' => $ exception ->getTraceAsString (),
374+ ],
375+ ]
376+ )->shouldBeCalled ();
377+
378+ $ middleware = new LoggerMiddleware ($ logger ->reveal ());
379+ $ middleware ->pre ($ request , 'abc ' , $ options );
380+ $ middleware ->error ($ exception , 'abc ' , $ options );
381+ }
248382}
0 commit comments