Skip to content

Commit

Permalink
Merge pull request #64 from marcell-ferenc/feature/http-logger-enable…
Browse files Browse the repository at this point in the history
…d-config

Add ability to enable/disable HTTP Logging via .env variable
  • Loading branch information
freekmurze authored Jul 15, 2024
2 parents 76f0b41 + 2f8abff commit f8bfadb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ This is the contents of the published config file:
```php
return [

/*
* Determine if the http-logger middleware should be enabled.
*/
'enabled' => env('HTTP_LOGGER_ENABLED', true),

/*
* The log profile which determines whether a request should be logged.
* It should implement `LogProfile`.
Expand Down Expand Up @@ -115,6 +120,7 @@ and `LogWriter` class will write the request to a log.
A default log implementation is added within this package.
It will only log `POST`, `PUT`, `PATCH`, and `DELETE` requests
and it will write to the default Laravel logger.
Logging is enabled by default but can be toggled on or off via the `HTTP_LOGGER_ENABLED` variable in the `.env` file.

You're free to implement your own log profile and/or log writer classes,
and configure it in `config/http-logger.php`.
Expand Down
5 changes: 5 additions & 0 deletions config/http-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

return [

/*
* Determine if the http-logger middleware should be enabled.
*/
'enabled' => env('HTTP_LOGGER_ENABLED', true),

/*
* The log profile which determines whether a request should be logged.
* It should implement `LogProfile`.
Expand Down
4 changes: 4 additions & 0 deletions src/LogNonGetRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class LogNonGetRequests implements LogProfile
{
public function shouldLogRequest(Request $request): bool
{
if (! config('http-logger.enabled')) {
return false;
}

return in_array(strtolower($request->method()), ['post', 'put', 'patch', 'delete']);
}
}
9 changes: 9 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<?php

use function PHPUnit\Framework\assertFileDoesNotExist;
use function PHPUnit\Framework\assertFileExists;

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

assertFileExists($this->getLogFile());
});

it('doesnt log an incoming request when disabled', function () {
config(['http-logger.enabled' => false]);

$this->call('post', '/');

assertFileDoesNotExist($this->getLogFile());
});
10 changes: 10 additions & 0 deletions tests/LogNonGetRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@
$this->assertFalse($this->logProfile->shouldLogRequest($request), "{$method} should not be logged.");
}
});

it('doesnt log when disabled', function () {
config(['http-logger.enabled' => false]);

foreach (['post', 'put', 'patch', 'delete'] as $method) {
$request = $this->makeRequest($method, $this->uri);

$this->assertFalse($this->logProfile->shouldLogRequest($request), "{$method} should not be logged.");
}
});

0 comments on commit f8bfadb

Please sign in to comment.