Skip to content

Commit

Permalink
Merge pull request #3 from dniccum/bugfix/response-body-overload-prot…
Browse files Browse the repository at this point in the history
…ection

Bugfix/response body overload protection
  • Loading branch information
dniccum authored Oct 27, 2022
2 parents 165d404 + 00318be commit f2697eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ Route::post('/create-project', [ \App\Http\Controllers\SampleController::class,
->middleware([ 'route-logging' ]);
```

#### IMPORTANT: Response Body Length Limit

Assuming that your database migration and model are unchanged, there is currently logic in place that will prevent the `response_body` column from being overloaded. What does this mean? In the event that the response body of the request that you are logging is very large (ie returning 500+ results from an API), the number of bytes/characters might be too large for the database to store without failing. If the response body **is longer than 65,000 characters, the response body will NOT be saved.**

### Log Cleanup (optional)

**Note** – this is not required but *HIGHLY* recommended as this could lead to large amounts of data within your database in short periods of time.
Expand Down
6 changes: 5 additions & 1 deletion src/Http/Middleware/RequestLogging.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public function terminate(Request $request, JsonResponse $response)
$logEntry->request_header = $request->header();
$logEntry->ip = $request->ip();
$logEntry->status_code = $response->getStatusCode();
$logEntry->response_body = json_decode($response->getContent(), true);
if ($response->getContent() && strlen($response->getContent()) <= 65000) {
$logEntry->response_body = json_decode($response->getContent(), true);
} else {
$logEntry->response_body = 'Response was too long to store.';
}
$logEntry->save();
}

Expand Down

0 comments on commit f2697eb

Please sign in to comment.