structured logging
What is structured logging?
structured logging: Logging machine-parseable key/value records (usually JSON) instead of free-form text, so logs can be queried and filtered. Structured logging means writing logs as machine-readable records with named fields, usually JSON, instead of free-form sentences. {"level":"error","msg":"payment failed","order_id":4821} instead of "Payment failed for order 4821". The difference shows when you search. Finding every failed payment over 100 euros, or every error for one customer, is a simple filter on fields, rather than a fragile text search.
Frequently asked questions about structured logging
What should you keep in mind with structured logging?
Keep message texts constant and put variable details in fields, so similar events can be grouped. Add context such as request ID, user ID and trace ID automatically, for example through a logging context in your framework. Laravel supports this with Monolog formatters and Log::withContext(). As always, keep passwords, tokens and unnecessary personal data out of log fields.
What does an example of structured logging look like?
{"level":"error","msg":"payment failed","order_id":4821,"latency_ms":250} At a glance
| Category | Observability |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |