Skip to content

Commit 09a3c03

Browse files
Add ability to enable/disable HTTP Logging via .env variable
- new configuration option enabled in config/http-logger.php - update the default log implementation to check the enabled configuration before logging requests
1 parent 76f0b41 commit 09a3c03

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

config/http-logger.php

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

33
return [
44

5+
/*
6+
* Determine if the http-logger middleware should be enabled.
7+
*/
8+
'enabled' => env('HTTP_LOGGER_ENABLED', true),
9+
510
/*
611
* The log profile which determines whether a request should be logged.
712
* It should implement `LogProfile`.

src/LogNonGetRequests.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class LogNonGetRequests implements LogProfile
88
{
99
public function shouldLogRequest(Request $request): bool
1010
{
11+
if (! config('http-logger.enabled')) {
12+
return false;
13+
}
14+
1115
return in_array(strtolower($request->method()), ['post', 'put', 'patch', 'delete']);
1216
}
1317
}

tests/IntegrationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
<?php
22

3+
use function PHPUnit\Framework\assertFileDoesNotExist;
34
use function PHPUnit\Framework\assertFileExists;
45

56
it('logs an incoming request via the middleware', function () {
67
$this->call('post', '/');
78

89
assertFileExists($this->getLogFile());
910
});
11+
12+
it('doesnt log an incoming request when disabled', function () {
13+
config(['http-logger.enabled' => false]);
14+
15+
$this->call('post', '/');
16+
17+
assertFileDoesNotExist($this->getLogFile());
18+
});

tests/LogNonGetRequestsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@
2121
$this->assertFalse($this->logProfile->shouldLogRequest($request), "{$method} should not be logged.");
2222
}
2323
});
24+
25+
it('doesnt log when disabled', function () {
26+
config(['http-logger.enabled' => false]);
27+
28+
foreach (['post', 'put', 'patch', 'delete'] as $method) {
29+
$request = $this->makeRequest($method, $this->uri);
30+
31+
$this->assertFalse($this->logProfile->shouldLogRequest($request), "{$method} should not be logged.");
32+
}
33+
});

0 commit comments

Comments
 (0)