|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Origin: https://github.com/guzzle/log-subscriber |
|
4
|
|
|
* Copyright (c) 2014 Michael Dowling, https://github.com/mtdowling <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
namespace Commercetools\Core\Helper\Subscriber\Log; |
|
25
|
|
|
|
|
26
|
|
|
use GuzzleHttp\Event\RequestEvents; |
|
27
|
|
|
use GuzzleHttp\Event\SubscriberInterface; |
|
28
|
|
|
use GuzzleHttp\Event\CompleteEvent; |
|
29
|
|
|
use GuzzleHttp\Event\ErrorEvent; |
|
30
|
|
|
use Psr\Log\LoggerInterface; |
|
31
|
|
|
use Psr\Log\LogLevel; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Plugin class that will add request and response logging to an HTTP request. |
|
35
|
|
|
* |
|
36
|
|
|
* The log plugin uses a message formatter that allows custom messages via |
|
37
|
|
|
* template variable substitution. |
|
38
|
|
|
* |
|
39
|
|
|
* @see MessageLogger for a list of available template variable substitutions |
|
40
|
|
|
*/ |
|
41
|
|
|
class LogSubscriber implements SubscriberInterface |
|
42
|
|
|
{ |
|
43
|
|
|
/** @var LoggerInterface */ |
|
44
|
|
|
private $logger; |
|
45
|
|
|
|
|
46
|
|
|
/** @var Formatter Formatter used to format log messages */ |
|
47
|
|
|
private $formatter; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $logLevel; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param LoggerInterface|callable|resource|null $logger Logger used to log |
|
56
|
|
|
* messages. Pass a LoggerInterface to use a PSR-3 logger. Pass a |
|
57
|
|
|
* callable to log messages to a function that accepts a string of |
|
58
|
|
|
* data. Pass a resource returned from ``fopen()`` to log to an open |
|
59
|
|
|
* resource. Pass null or leave empty to write log messages using |
|
60
|
|
|
* ``echo()``. |
|
61
|
|
|
* @param string|Formatter $formatter Formatter used to format log |
|
62
|
|
|
* messages or a string representing a message formatter template. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct($logger = null, $formatter = null, $logLevel = LogLevel::INFO) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->logLevel = $logLevel; |
|
67
|
|
|
$this->logger = $logger instanceof LoggerInterface |
|
68
|
|
|
? $logger |
|
69
|
|
|
: new SimpleLogger($logger); |
|
70
|
|
|
|
|
71
|
|
|
$this->formatter = $formatter instanceof Formatter |
|
72
|
|
|
? $formatter |
|
73
|
|
|
: new Formatter($formatter); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getEvents() |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
// Fire after responses are verified (which trigger error events). |
|
80
|
|
|
'complete' => ['onComplete', RequestEvents::VERIFY_RESPONSE - 10], |
|
81
|
|
|
'error' => ['onError', RequestEvents::EARLY] |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function onComplete(CompleteEvent $event) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->logger->log( |
|
88
|
|
|
substr($event->getResponse()->getStatusCode(), 0, 1) == '2' |
|
89
|
|
|
? $this->logLevel |
|
90
|
|
|
: LogLevel::WARNING, |
|
91
|
|
|
$this->formatter->format( |
|
92
|
|
|
$event->getRequest(), |
|
93
|
|
|
$event->getResponse() |
|
94
|
|
|
), |
|
95
|
|
|
[ |
|
96
|
|
|
'request' => $event->getRequest(), |
|
97
|
|
|
'response' => $event->getResponse() |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function onError(ErrorEvent $event) |
|
103
|
|
|
{ |
|
104
|
|
|
$ex = $event->getException(); |
|
105
|
|
|
$this->logger->log( |
|
106
|
|
|
LogLevel::CRITICAL, |
|
107
|
|
|
$this->formatter->format( |
|
108
|
|
|
$event->getRequest(), |
|
109
|
|
|
$event->getResponse(), |
|
110
|
|
|
$ex |
|
111
|
|
|
), |
|
112
|
|
|
[ |
|
113
|
|
|
'request' => $event->getRequest(), |
|
114
|
|
|
'response' => $event->getResponse(), |
|
115
|
|
|
'exception' => $ex |
|
116
|
|
|
] |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|