Designing structured logs: define an API log schema and connect traces
Structured logs are more than logs serialized as JSON. They use consistent field names, types, and meanings so systems can parse and interpret them reliably. For an API, standardize the event time, severity, and service origin; define request attributes such as route, outcome, and error; and attach the active trace ID and span ID when available. This makes searching, aggregation, and incident analysis easier.
Structured logs are more than logs serialized as JSON. They use consistent field names, types, and meanings so systems can parse and interpret them reliably. For an API, standardize the event time, severity, and service origin; define request attributes such as route, outcome, and error; and attach the active trace ID and span ID when available. This makes searching, aggregation, and incident analysis easier.
JSON and structured logs are different
OpenTelemetry distinguishes structured, semistructured, and unstructured logs. JSON is only an encoding format: if field names or types change from one message to the next, the data does not have a stable structure. For example, recording status as a number in one event and a string in another, or naming the same concept statusCode and http_status, complicates downstream analysis.
A structured event uses fields from a defined schema. An unstructured log is plain natural-language text without consistent field boundaries. A semistructured log has readable separators such as key=value or JSON, but different producers may still use different fields or types, requiring normalization.
Design API log-record fields
The OpenTelemetry log data model has commonly used top-level fields and event-specific attributes. The event time (Timestamp) can differ from the time it was observed by a collector (ObservedTimestamp). SeverityText is the level name from the original logging library; SeverityNumber is a normalized number for comparison and filtering. Resource identifies the source, such as the service or runtime environment, while Attributes carry details that vary by event.
When processing an API request, include its trace ID and span ID if an active trace exists, so the log can be linked to that request's trace. An event name should identify a stable event type, not a per-occurrence value. For example, keep api.request.completed fixed and put the route, method, status code, duration, and error type in attributes. Record user or request IDs only where needed and with care; do not use them as unbounded metric dimensions.
| Field kind | Example | Design principle |
|---|---|---|
| Common top-level fields | Timestamp, SeverityNumber, TraceId, SpanId | Keep meanings and types consistent throughout the logging pipeline |
| Resource | service.name, deployment.environment.name, service.version | Share stable source identity across logs from the same origin |
| Event attributes | http.request.method, http.route, http.response.status_code, error.type | Use agreed names for useful event details to search, filter, and aggregate |
| Body | A short human-readable summary | Do not use it as a substitute for the schema or put sensitive data in it |
Record errors and events consistently
Define what each severity means instead of relying only on its label. OpenTelemetry SeverityNumber ranges are TRACE 1–4, DEBUG 5–8, INFO 9–12, WARN 13–16, ERROR 17–20, and FATAL 21–24; higher numbers indicate greater severity. Do not sort WARNING, WARN, and warning as arbitrary strings. Check how your logging implementation maps its levels to OpenTelemetry values.
Give an exception or failure event a consistent event name, time, severity, and structured error attributes. A value describing an operation as a whole may belong on a span attribute; a distinct occurrence at a point during the operation may be a log event. Represent work with a duration and clear start and end as a span. Add only the context each event needs instead of copying every field into every record, to manage volume and cost.
Rollout and safety checks
**Define the schema.** Specify required fields, types, meanings, whether fields may be absent, severity mapping, and compatibility rules.
**Check the logging library and OTel bridge.** The OpenTelemetry Logs specification is stable, but SDK implementation maturity varies by language. Check support for your chosen language and version.
**Instrument one API route first.** Record success, validation 4xx, server 5xx, timeout, and cancellation with the same schema, then check trace-ID correlation. This article does not claim those checks were run.
**Control sensitive data and volume.** Do not collect request or response bodies, authorization headers, passwords, tokens, or personal data by default. Apply allowlists, masking, and length limits to necessary values, and set a retention policy.
**Validate schema changes.** Before changing a field name or type, check compatibility with downstream searches and dashboards, and whether old and new formats can be handled together for a transition period.
Sources and scope
OpenTelemetry, [Logs](https://opentelemetry.io/docs/concepts/signals/logs/)
OpenTelemetry, [Logs Data Model](https://opentelemetry.io/docs/specs/otel/logs/data-model/)
OpenTelemetry, [Semantic conventions for events](https://opentelemetry.io/docs/specs/semconv/general/events/)
Based on official OpenTelemetry documentation reviewed on 2026-11-19. Logs API and SDK implementation status can vary by language. This article covers API application log structure and correlation; it does not prescribe a specific logging backend or legal retention requirement.