Fixed Seven-Field JSON Logger: How a Full-Stack Project Logs Frontend and Backend Without Losing the Trail
A missing trace_id and mismatched timestamps turned a simple 500 error into hours of guesswork; this configuration cuts incident location time from roughly 30 minutes to a few minutes. The distinct value is making one request's logs searchable and linkable across frontend, backend, and separate error files, while keeping sensitive data out of the written records without extra tooling.
Logging stops being guesswork when you treat it as five separate decisions—level, format, sink, file splitting, and retention. This setup, hardened from a real incident, uses five standard levels (no custom sixth), emits one JSON line per event with seven fixed fields (timestamp, level, service, env, module, trace_id, message), and routes INFO and above to app.log while ERROR and above also land in error.log as a fallback copy. app.log rotates at midnight keeping 30 files; error.log rotates by size at 10 MB with 5 backups.
On the frontend, errors are collected at 100% while performance and behavior events are sampled at 10%. Events are buffered, sent in batches of 10 or every 5 seconds via fetch, with a pagehide handler using navigator.sendBeacon to guarantee the final batch arrives. A shared trace_id is generated in middleware, passed to the frontend, and used to group every log from the same request. Redaction happens at the handler layer before anything touches disk—passwords, phone numbers, and emails are masked or stripped—and the message field is kept free of raw user input to prevent log injection.
The article also sets rules for logging frequency: each request entry/exit and state change logs every time, success-path details are sampled, and loops plus successful health checks are forbidden. A capacity formula shows that 100 QPS with 2 logs per request and 500 bytes each produces about 8.64 GB/day at sustained peak, but a more realistic 300k requests/day lands at roughly 300 MB/day uncompressed and under 2 GB across 30 days after compression. For compliance-sensitive industries, audit logs need 12–36 months retention, far beyond the 30-day runtime baseline.
Separating the five logging decisions upfront prevents most rework; symptoms like unsearchable errors or overflowing disks map directly to one missing decision.
Putting redaction at the handler layer rather than in business code is what makes it actually hold; scattered business-level filtering always has blind spots.
The decision tree's default of not splitting by module preserves trace_id's ability to stitch a request across services; splitting by function works against the core debugging model.
The frontend's 100% error collection paired with 10% sampling of behavior is a pragmatic trade-off that accepts missing some events but never misses a failure signal.
Logging into stdout for containers (Twelve-Factor) and writing to files for single VMs shows the same principle applied to different deployment realities rather than a one-size-fits-all answer.
Fixed seven-field JSON logging sits between two pressures: AI-friendly logs versus human-readable logs. One position holds that logs are written for AI, not people. The counter argues AI can digest an entire time window while a human needs to spot recent anomalies quickly, so fixed fields plus single-line JSON is the workable middle ground. Sampling by trace chain is also presented as the way to keep the full trail intact.
Logs are for AI to read, not for humans.
That's only half true. AI reads the entire time window, while a human troubleshooting only looks at the last few lines of anomalies. So the approach that actually passes on both sides is fixed fields plus single-line JSON: a human can locate it at a glance, and AI can be fed the whole chunk without reordering. Logs optimized purely for AI will make the human miserable the night a production incident happens.
Sampling by trace chain is what keeps the trail from breaking.