diff --git a/README.md b/README.md index a18b3bf..e38ccda 100644 --- a/README.md +++ b/README.md @@ -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`. @@ -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`. diff --git a/config/http-logger.php b/config/http-logger.php index 39bcf3d..8747b1d 100644 --- a/config/http-logger.php +++ b/config/http-logger.php @@ -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`. diff --git a/src/LogNonGetRequests.php b/src/LogNonGetRequests.php index 6b5fa13..96c7204 100644 --- a/src/LogNonGetRequests.php +++ b/src/LogNonGetRequests.php @@ -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']); } } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 382dedd..6abfd2c 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -1,5 +1,6 @@ getLogFile()); }); + +it('doesnt log an incoming request when disabled', function () { + config(['http-logger.enabled' => false]); + + $this->call('post', '/'); + + assertFileDoesNotExist($this->getLogFile()); +}); diff --git a/tests/LogNonGetRequestsTest.php b/tests/LogNonGetRequestsTest.php index c05e91a..88134bf 100644 --- a/tests/LogNonGetRequestsTest.php +++ b/tests/LogNonGetRequestsTest.php @@ -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."); + } +});