Skip to content

Commit 31aec75

Browse files
committed
Updated readme to explain custom logging checking logic
1 parent 65b94a6 commit 31aec75

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

README.md

+70-3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ return [
114114
*/
115115
'get_json_values_as_array' => true,
116116

117+
/**
118+
* The class responsible for determining if a request should be logged.
119+
*
120+
* Out of the box options are:
121+
* Mtownsend\RequestResponseLogger\Support\Logging\LogAll::class,
122+
* Mtownsend\RequestResponseLogger\Support\Logging\LogClientErrorsOnly::class,
123+
* Mtownsend\RequestResponseLogger\Support\Logging\LogSuccessOnly::class,
124+
*/
125+
'should_log_handler' => \Mtownsend\RequestResponseLogger\Support\Logging\LogAll::class,
126+
117127
];
118128
```
119129

@@ -135,7 +145,65 @@ protected function schedule(Schedule $schedule)
135145
}
136146
```
137147

138-
## Advanced Usage
148+
# Advanced Usage
149+
150+
## Conditional logging
151+
152+
This package provides support for specifying custom conditions before a request/response is logged to the database.
153+
154+
It comes with 3 default options out of the box:
155+
156+
* LogAll - log request & response
157+
* LogClientErrorsOnly - log only responses that have an http status code of 4XX
158+
* LogSuccessOnly - log only responses that have an http status code of 2XX
159+
* ...or your own!
160+
161+
Creating your own conditional logic is pretty straightforward and can be done in 2 simple steps:
162+
163+
1. First, create a custom class that will perform your conditional checks for logging. For demonstration purposes let's say we're going to create a conditional logic check to only log requests made from external services and not your own web app. You can use the following code as a template.
164+
165+
```php
166+
<?php
167+
168+
namespace App\Support\Logging;
169+
170+
use Illuminate\Http\Request;
171+
use Mtownsend\RequestResponseLogger\Support\Logging\Contracts\ShouldLogContract;
172+
173+
class LogExternalRequests implements ShouldLogContract
174+
{
175+
public $request;
176+
public $response;
177+
178+
public function __construct(Request $request, $response)
179+
{
180+
$this->request = $request;
181+
$this->response = $response;
182+
}
183+
184+
/**
185+
* Return a truth-y value to log the request and response.
186+
* Return false-y value to skip logging.
187+
*
188+
* @return bool
189+
*/
190+
public function shouldLog(): bool
191+
{
192+
// Custom logic goes here...
193+
}
194+
}
195+
```
196+
197+
2. Open up your `config/log-requests-and-responses.php` and set the `should_log_handler` key to your class.
198+
199+
```php
200+
[
201+
//...
202+
'should_log_handler' => \App\Support\Logging\LogExternalRequests::class,
203+
]
204+
```
205+
206+
...and that's it! Your custom logging logic will now be used any time the middleware is executed.
139207

140208
## Model scopes
141209

@@ -168,12 +236,11 @@ Then in your model, extend the base model:
168236

169237
namespace App\Models;
170238

171-
use Illuminate\Database\Eloquent\Factories\HasFactory;
172239
use Mtownsend\RequestResponseLogger\Models\RequestResponseLog as BaseRequestResponseLog;
173240

174241
class RequestResponseLog extends BaseRequestResponseLog
175242
{
176-
use HasFactory;
243+
//
177244
}
178245
```
179246

0 commit comments

Comments
 (0)