Skip to content

Commit 074be30

Browse files
authored
Fix meaning of exception only (#12)
* Fixed the constructor argument of * Updated changelog * Updated readme file
1 parent d9031ef commit 074be30

File tree

5 files changed

+61
-27
lines changed

5 files changed

+61
-27
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All Notable changes to `gmponos/guzzle_logger` will be documented in this file
44

5+
## 0.5.0 - 2018-10-01
6+
7+
### Changed
8+
- **BREAKING CHANGE** Renamed the variable `$logRequestOnExceptionOnly` to `$onExceptionOnly`. The purpose of this constructor argument was
9+
to log request and responses only if an exceptgition occurs. If you were manually setting this argument as true now you must set it
10+
as false as the variables meaning is inverted.
11+
- Deprecated the option `requests`. It will be removed on my next version.
12+
513
## 0.4.0 - 2018-09-12
614

715
### Changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The signature of the LoggerMiddleware class is the following:
5555
``LoggerMiddleware(LoggerInterface $logger, $logRequests = true, $logStatistics = false, array $thresholds = [])``
5656

5757
- **logger** - The PSR-3 logger to use for logging.
58-
- **logRequests** - By default the middleware is set to log every request and response. If you wish that to log only the requests and responses that you retrieve a status code above 4xx set this as false.
58+
- **$onExceptionOnly** - By default the middleware is set to log every request and response. If you wish that to log only the requests and responses that you retrieve a status code above 4xx set this as true.
5959
- **logStatistics** - If you set logStatistics as true and this as true then guzzle will also log statistics about the requests.
6060
- **thresholds** - An array that you may use to change the thresholds of logging the responses.
6161

@@ -66,7 +66,7 @@ You can set on each request options about your log.
6666
```php
6767
$client->get("/", [
6868
'log' => [
69-
'requests' => true,
69+
'on_exception_only' => true,
7070
'statistics' => true,
7171
'error_threshold' => null,
7272
'warning_threshold' => null,
@@ -81,8 +81,8 @@ $client->get("/", [
8181
```
8282

8383
- ``sensitive`` if you set this to true then the body of request/response will not be logged as it will be considered that it contains sensitive information.
84-
- ``requests`` Do not log anything unless if the request is above the threshold or inside the levels.
85-
- ``statistics`` if the requests variable is true and this is also true the logger will log statistics about the request
84+
- ``on_exception_only`` Do not log anything unless if the response status code is above the threshold.
85+
- ``statistics`` if the `on_exception_only` option/variable is true and this is also true the middleware will log statistics about the HTTP call.
8686
- ``levels`` set custom log levels for each response status code
8787

8888
## Change log

src/Middleware/LoggerMiddleware.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LoggerMiddleware
1919
/**
2020
* @var bool Whether or not to log requests as they are made.
2121
*/
22-
private $logRequestOnExceptionOnly;
22+
private $onExceptionOnly;
2323

2424
/**
2525
* @var bool
@@ -50,18 +50,18 @@ class LoggerMiddleware
5050
* Creates a callable middleware for logging requests and responses.
5151
*
5252
* @param LoggerInterface $logger
53-
* @param bool $logRequestOnExceptionOnly
54-
* @param bool $logStatistics
53+
* @param bool $onExceptionOnly The request and the response will be logged only in cases there is an exception or if they status code exceeds the thresholds.
54+
* @param bool $logStatistics If this is true an extra row will be added that will contain some HTTP statistics.
5555
* @param array $thresholds
5656
*/
5757
public function __construct(
5858
LoggerInterface $logger,
59-
$logRequestOnExceptionOnly = true,
59+
$onExceptionOnly = false,
6060
$logStatistics = false,
6161
array $thresholds = []
6262
) {
6363
$this->logger = $logger;
64-
$this->logRequestOnExceptionOnly = $logRequestOnExceptionOnly;
64+
$this->onExceptionOnly = $onExceptionOnly;
6565
$this->logStatistics = $logStatistics;
6666
$this->thresholds = array_merge([
6767
'error' => 499,
@@ -80,7 +80,7 @@ public function __invoke(callable $handler)
8080
return function (RequestInterface $request, array $options) use ($handler) {
8181
$this->setOptions($options);
8282

83-
if ($this->logRequestOnExceptionOnly === true) {
83+
if ($this->onExceptionOnly === false) {
8484
$this->logRequest($request);
8585
if ($this->logStatistics && !isset($options['on_stats'])) {
8686
$options['on_stats'] = $this->logStatistics();
@@ -183,7 +183,7 @@ private function logResponse(ResponseInterface $response)
183183
private function handleSuccess(RequestInterface $request)
184184
{
185185
return function (ResponseInterface $response) use ($request) {
186-
if ($this->logRequestOnExceptionOnly === true) {
186+
if ($this->onExceptionOnly === false) {
187187
$this->logResponse($response);
188188
return $response;
189189
}
@@ -205,7 +205,7 @@ private function handleSuccess(RequestInterface $request)
205205
private function handleFailure(RequestInterface $request)
206206
{
207207
return function (\Exception $reason) use ($request) {
208-
if ($this->logRequestOnExceptionOnly === false) {
208+
if ($this->onExceptionOnly === true) {
209209
$this->logRequest($request);
210210
}
211211

@@ -314,20 +314,28 @@ private function setOptions(array $options)
314314
return;
315315
}
316316

317+
$options = $options['log'];
318+
if (isset($options['requests'])) {
319+
@trigger_error('Using option "requests" is deprecated and it will be removed on the next version. Use "on_exception_only"', E_USER_DEPRECATED);
320+
if (!isset($options['on_exception_only'])) {
321+
$options['on_exception_only'] = !$options['requests'];
322+
}
323+
}
324+
317325
$defaults = [
318-
'requests' => $this->logRequestOnExceptionOnly,
326+
'on_exception_only' => $this->onExceptionOnly,
319327
'statistics' => $this->logStatistics,
320328
'warning_threshold' => 399,
321329
'error_threshold' => 499,
322330
'levels' => [],
323331
'sensitive' => false,
324332
];
325333

326-
$options = array_merge($defaults, $options['log']);
334+
$options = array_merge($defaults, $options);
327335
$this->logCodeLevel = $options['levels'];
328336
$this->thresholds['warning'] = $options['warning_threshold'];
329337
$this->thresholds['error'] = $options['error_threshold'];
330-
$this->logRequestOnExceptionOnly = $options['requests'];
338+
$this->onExceptionOnly = $options['on_exception_only'];
331339
$this->logStatistics = $options['statistics'];
332340
$this->sensitive = $options['sensitive'];
333341
}

tests/TestApp/HistoryLogger.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
class HistoryLogger implements LoggerInterface
88
{
9-
109
/**
1110
* @var array
1211
*/
@@ -16,7 +15,7 @@ class HistoryLogger implements LoggerInterface
1615
* System is unusable.
1716
*
1817
* @param string $message
19-
* @param array $context
18+
* @param array $context
2019
* @return void
2120
*/
2221
public function emergency($message, array $context = [])
@@ -35,7 +34,7 @@ public function emergency($message, array $context = [])
3534
* trigger the SMS alerts and wake you up.
3635
*
3736
* @param string $message
38-
* @param array $context
37+
* @param array $context
3938
* @return void
4039
*/
4140
public function alert($message, array $context = [])
@@ -53,7 +52,7 @@ public function alert($message, array $context = [])
5352
* Example: Application component unavailable, unexpected exception.
5453
*
5554
* @param string $message
56-
* @param array $context
55+
* @param array $context
5756
* @return void
5857
*/
5958
public function critical($message, array $context = [])
@@ -70,7 +69,7 @@ public function critical($message, array $context = [])
7069
* be logged and monitored.
7170
*
7271
* @param string $message
73-
* @param array $context
72+
* @param array $context
7473
* @return void
7574
*/
7675
public function error($message, array $context = [])
@@ -89,7 +88,7 @@ public function error($message, array $context = [])
8988
* that are not necessarily wrong.
9089
*
9190
* @param string $message
92-
* @param array $context
91+
* @param array $context
9392
* @return void
9493
*/
9594
public function warning($message, array $context = [])
@@ -105,7 +104,7 @@ public function warning($message, array $context = [])
105104
* Normal but significant events.
106105
*
107106
* @param string $message
108-
* @param array $context
107+
* @param array $context
109108
* @return void
110109
*/
111110
public function notice($message, array $context = [])
@@ -123,7 +122,7 @@ public function notice($message, array $context = [])
123122
* Example: User logs in, SQL logs.
124123
*
125124
* @param string $message
126-
* @param array $context
125+
* @param array $context
127126
* @return void
128127
*/
129128
public function info($message, array $context = [])
@@ -139,7 +138,7 @@ public function info($message, array $context = [])
139138
* Detailed debug information.
140139
*
141140
* @param string $message
142-
* @param array $context
141+
* @param array $context
143142
* @return void
144143
*/
145144
public function debug($message, array $context = [])
@@ -154,9 +153,9 @@ public function debug($message, array $context = [])
154153
/**
155154
* Logs with an arbitrary level.
156155
*
157-
* @param mixed $level
156+
* @param mixed $level
158157
* @param string $message
159-
* @param array $context
158+
* @param array $context
160159
* @return void
161160
*/
162161
public function log($level, $message, array $context = [])
@@ -167,4 +166,12 @@ public function log($level, $message, array $context = [])
167166
'context' => $context,
168167
];
169168
}
169+
170+
/**
171+
* Cleans the history of the logger.
172+
*/
173+
public function clean()
174+
{
175+
$this->history = [];
176+
}
170177
}

tests/Unit/LoggerMiddlewareTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function logSuccessfulTransaction()
8585
/**
8686
* @test
8787
*/
88-
public function doNotLogSuccessfulTransaction()
88+
public function logOnlyResponseWhenLogRequestsIsSetToFalse()
8989
{
9090
$this->appendResponse(200)
9191
->createClient([
@@ -96,6 +96,17 @@ public function doNotLogSuccessfulTransaction()
9696
->get('/');
9797

9898
$this->assertCount(0, $this->logger->history);
99+
100+
$this->logger->clean();
101+
102+
$this->appendResponse(200)->createClient()
103+
->get('/', [
104+
'log' => [
105+
'requests' => false,
106+
],
107+
]);
108+
109+
$this->assertCount(0, $this->logger->history);
99110
}
100111

101112
/**

0 commit comments

Comments
 (0)