Skip to content

Conversation

@mnapoli
Copy link
Member

@mnapoli mnapoli commented Jun 13, 2025

Edit: moved the class to https://github.com/brefphp/monolog-bridge

TODO:

The goal of this is to improve the overall logs experience with CloudWatch.

The usual format is to log unstructured text. This sucks because:

  • we can't filter by log level (e.g. look for errors)
  • we can't easily filter and see all logs of a request/invocation via the AWS request ID (e.g. as log metadata)
  • some log messages are split across multiple lines
  • exception traces especially are split across loads of lines, not easy to navigate in CloudWatch or other line-based viewers

Here's an example:

Screen-002902

A better approach is to switch to JSON-formatted logs:

  • we can add metadata to log records (e.g. request ID to filter/see all logs of a request, or filter by log level)
  • exception traces are organized as sub-metadata of the exception record, keeping things easier to navigate

But this isn't perfect:

  • depending on the tool you use to view the logs, the UX can be very very poor
  • it's hard to explore the logs quickly because now all info is nested in a log record, with no hierarchy to preview "just the message" quickly
  • Monolog's JSON formatter adds tons of info to the JSON record

Here's an example (Bref Cloud formats the JSON payload FYI):

Screen-002903

CloudWatch has some great feature though:

This is the log message {"key":"value"}

The log message above will automatically get parsed as text (the first part) and a JSON object attached (the second part).

That means we can have a simple log message + a structured JSON object.

On top of that, with CloudWatch:

  • we don't need to log the timestamp, CloudWatch automatically timestamps log records
  • I don't think we need to log the "channel" name: IMO channels make sense to split web logs from jobs or artisan commands, but in Lambda these are separate functions/logs anyway, so I think we can remove that part too

This is why I want to add this CloudWatchFormatter optimized for Bref users.

As you can see in the screenshots below, it is easy to navigate the logs textually. But it's also possible to have a lot more information nested in the log records via the JSON object.

In CloudWatch:

Screen-002905

In Bref Cloud:

Screen-002904

In CloudWatch Logs Insights, we can see that keys of the JSON objects are correctly detected:

Screen-002906

That allows us to create advanced queries, for example to search for "ERROR" logs with a specific exception class:

Screen-002907

Looking forward

I'm releasing this in Bref v2 so that we can all test this. If this works as well as I hope, the plan is to make this the default format in Laravel with the Bref-Laravel bridge.

I am also working on a v2 for https://github.com/brefphp/logger that uses the same format: brefphp/logger#11

@mnapoli mnapoli marked this pull request as ready for review June 13, 2025 19:46
@mnapoli mnapoli self-assigned this Jun 13, 2025
mnapoli added a commit to brefphp/laravel-bridge that referenced this pull request Jun 14, 2025
@mnapoli mnapoli merged commit ca91daf into master Jun 14, 2025
9 checks passed
@mnapoli mnapoli deleted the logs-formatter branch June 14, 2025 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants