Proposed JSON error-log schema (minimal required + optional):json
{
"timestamp": "ISO8601",
"level": "ERROR|WARN|INFO",
"service": "string",
"env": "prod|staging|dev",
"request_id": "uuid",
"span_id": "hex (optional)",
"user_id": "string|null",
"error_type": "ValidationError|TimeoutError|NullPointerException|...",
"message": "human-readable summary",
"http": { "method": "GET", "path": "/v1/x", "status": 400 },
"duration_ms": 123,
"downstream": { "service": "name", "endpoint": "url", "status": 504 },
"stacktrace": "string (optional)",
"tags": { "...": "..." },
"sample_rate": 0.01
}
Three example entries:json
{"timestamp":"2025-11-22T14:05:12Z","level":"ERROR","service":"orders","env":"prod","request_id":"a1b2","user_id":"u-42","error_type":"ValidationError","message":"invalid order: missing field 'items'","http":{"method":"POST","path":"/orders","status":400},"stacktrace":null}
json
{"timestamp":"2025-11-22T14:06:20Z","level":"ERROR","service":"payments","env":"prod","request_id":"c3d4","user_id":"u-99","error_type":"TimeoutError","message":"downstream-payments timed out after 3000ms","downstream":{"service":"downstream-payments","endpoint":"/charge","status":504},"duration_ms":3000,"stacktrace":null,"sample_rate":0.1}
json
{"timestamp":"2025-11-22T14:07:01Z","level":"ERROR","service":"catalog","env":"prod","request_id":"e5f6","user_id":null,"error_type":"NullPointerException","message":"unexpected null when reading product.price","stacktrace":"com.example.Catalog.getPrice(Catalog.java:87)\n...","sample_rate":1.0}
Which fields and why:- timestamp, level, service, env: routing/alerting and filtering.- request_id/span_id: trace correlation for incident investigation.- user_id: impact scope and auditing (nullable).- error_type/message: quick classification and human summary.- http/downstream/duration_ms: context for latency/failure causes.- tags/sample_rate: attach metadata and indicate sampling.Stacktrace policy:- Include full stacktrace for high-severity/internal exceptions and for all errors in non-prod.- For noisy or frequent errors (validation, client errors), omit stacktrace and log only message + error_type.- Use deterministic sampling (e.g., 1% or 10% with sample_rate field) for medium-severity downstream/timeouts to capture patterns without noise.- Always preserve request_id/span_id so sampled traces can be retrieved later.