Standardizing Application Logs: From Structured Logging to Alerting

    This article explains how to standardize application logging in three stages: adopting a structured JSON format, propagating a request ID across the call chain for traceability, and configuring alerts on error spikes. It covers required fields, sensitive-data masking, log rotation and sampling, verification steps, a troubleshooting table, and a pre-launch checklist. It applies to teams that control their own logging code and use a centralized log store.

    Standardizing Application Logs: From Structured Logging to Alerting

    1. Problem Statement and Scope

    Many services emit logs as plain lines of text, which makes it hard to filter by request, user, or service when something breaks. This article applies to teams running services whose logging code they can modify. If logs are fully managed by a third-party platform, some configuration must be done through that platform’s own mechanisms, which this article does not cover.

    What is structured logging? It means every log record uses a fixed set of key-value fields, such as timestamp, level, service name, request identifier, and message. With fixed fields, search tools can filter by field directly instead of relying on regular expressions.

    This article addresses three questions: which fields logs should contain, how to make logs searchable, and how to automatically notify the right people when errors occur.

    2. Prerequisites

    1. The service has a unified startup entry point and configuration mechanism, such as environment variables or config files. Log level, output path, and the centralized storage address should all be read from configuration, not hard-coded.
    2. The team agrees on the meaning of each log level. DEBUG is for development-time troubleshooting; INFO records key business milestones; WARN indicates anomalies that can recover automatically; ERROR indicates failures that require human attention.
    3. A centralized tool exists that can receive and search logs. The tool’s name and access address are decided by the team and injected into services via environment variables.
    4. The team has defined which fields are sensitive, such as passwords, access tokens, national ID numbers, and full phone numbers. Such content must never be written to logs.

    3. Implementation Steps

    Step 1: Unify the output format. Use one JSON record per line, containing at least: timestamp (ISO 8601 with timezone), log level, service name, service version, and message text.

    Step 2: Add a correlation identifier. Generate a request ID for each incoming request and propagate it through the entire call chain; every related log entry records this ID. For cross-service calls, pass it in a request header, and the receiving service should reuse the upstream request ID instead of generating a new one.

    Step 3: Standardize business fields. Examples include order number and internal user number. Record only internal identifiers, never raw personal information.

    Step 4: Centralize masking. Handle sensitive fields in one place before logs are written, typically replacing values with a fixed placeholder such as “*“. Masking logic should live in the logging component itself, so individual call sites don’t each handle (and potentially miss) it.

    Step 5: Control log volume. Sample high-frequency success logs, or keep only records whose latency exceeds a threshold. Error logs should always be recorded in full.

    Step 6: Configure rotation and retention. Rotate log files by size or by days. The retention period is determined jointly by compliance requirements and storage cost, and should be documented in writing.

    Step 7: Configure alerts. Send notifications when the ERROR count spikes within a time window, or when the failure rate of a critical endpoint exceeds a set value. Thresholds should be based on your team’s own historical data, not copied from other projects.

    A sample log entry in this format (values are illustrative only):

    1. {"ts":"2026-11-23T08:30:12+08:00","level":"INFO","service":"order-api","version":"1.4.2","request_id":"req-7f3a","msg":"order created","order_id":"O20261123001"}

    4. Verification

    1. Start the service locally, place an order request, and check that every line of the output file is valid JSON. You can parse each line with jq: cat app.log | jq -c . Any parse error means non-JSON output still exists.
    2. Search by a single request ID and confirm that logs from the gateway through all downstream services are retrievable and in a sensible order.
    3. Submit test data containing sensitive fields and verify that no plaintext appears in the centralized store. Perform this step in a test environment.
    4. Trigger a controlled error and confirm the alert reaches the right people within the agreed time.

    5. Troubleshooting Table

    Symptom Likely Cause Fix
    Multi-line text in logs that won’t parse Somewhere still uses the old print function or legacy format Search globally for old output calls and replace them
    Request ID breaks at a downstream service Header names are inconsistent, or middleware doesn’t forward the header Standardize the header name and check forwarding logic in middleware
    Plaintext still appears after masking Sensitive data shows up in exception stack traces or third-party library logs Add a fallback filter at the log processing layer and review how exception messages are generated
    Disk usage growing rapidly No rotation configured, or high-frequency logs not sampled Configure rotation and downsample success requests
    Frequent false-positive alerts Thresholds are below normal fluctuation ranges Recompute baselines from historical data and add a duration condition

    6. Caveats and Risks

    Changing the log format may break parsing scripts that depend on the old format. Notify affected parties before rollout and keep a transition period during which both old and new formats coexist.

    Adding fields increases storage cost. The number of fields should be driven by actual search needs, not by adding everything “just in case.”

    DEBUG-level logging should not be enabled by default in production. If enabled temporarily, set an automatic expiration time.

    Before making changes, run through the full verification procedure in a test environment and keep the current version available for rollback if problems arise.

    7. Conclusion and Checklist

    The value of logs comes from three things: a unified format, a correlation identifier that spans the call chain, and actionable alerts. Without any one of these, logs are hard to use during an incident.

    Before going live, confirm each item:

    ☐ Every log line is valid JSON
    ☐ All logs carry service name, version, and request ID
    ☐ Sensitive fields are masked before writing
    ☐ Log rotation and retention are configured
    ☐ At least one ERROR alert has been verified to reach its recipients
    ☐ The common issues in the troubleshooting table have been shared with the team