Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
mb_scrub() is only available on PHP 8.3+, so the middleware will fatally error on PHP 8.2, which the package still declares as supported and tests in CI.
1 open finding
What changed in this PR
This PR fixes a bug in the codetech/laravel-api-logs package where responses whose body did not decode to a non-null JSON value were never logged: null was written to the non-nullable response_data column, causing the insert to fail (issue #45). This affected HTML error pages, empty 204 responses, and streamed/file responses.
The middleware now stores a redacted JSON array as before, any other non-null JSON value as-is, streamed/file responses (no readable content) as an empty string, and any other body as a UTF-8-scrubbed string so the json cast cannot fail. Documentation and feature tests were updated accordingly.
Changes:
- Extract response handling into a new
responseData()method that never returnsnull, falling back to an empty string (no content) or a scrubbed raw body. - Document that key redaction applies to JSON responses only.
- Add feature tests for HTML,
204, streamed, and non-UTF-8 responses.
| File | Description |
|---|---|
src/Http/Middleware/LogApiRequest.php |
Adds responseData() to safely store non-JSON/empty/streamed bodies instead of writing null. |
tests/Feature/LogApiRequestMiddlewareTest.php |
Adds routes and tests covering HTML, 204, streamed, and binary responses. |
docs/redaction.md |
Clarifies that key redaction applies only to JSON responses. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Closes #45.
Requests whose response body did not decode to a non-null JSON value were never logged:
nullwas written to the non-nullableresponse_datacolumn and the insert failed. This hit HTML error pages (a 403/404 for anAccept: */*client), empty 204 bodies, and streamed and file responses.LogApiRequestnow stores a JSON array redacted as before, any other non-null JSON value as is, and any other body as a string with invalid UTF-8 scrubbed so thejsoncast cannot fail on it. Streamed and file responses, which have no content to read, are stored as an empty string. No schema change.The redaction docs now state that key redaction applies to JSON responses only. Feature tests cover an HTML response, a 204, a streamed response and a body that is not valid UTF-8.