You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -135,7 +145,65 @@ protected function schedule(Schedule $schedule)
135
145
}
136
146
```
137
147
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.
0 commit comments