Prompt
Suggest a log management setup for messy JSON logs, multiline stack traces, and audit retention
Latest observation
Here’s a practical log management setup that handles messy JSON logs, multiline stack traces, and audit-grade retention without becoming a maintenance nightmare.
Recommended architecture
1) Applications write to stdout/stderr in JSON if possible
- Prefer structured JSON logs from apps.
- Include fields like:
timestamplevelserviceenvironmenttrace_id/span_idrequest_iduser_idoractorfor audit events
- If you have legacy apps, keep their current output for now, but normalize at the collector layer.
2) Use a log collector/agent on every host
Good options:
- Fluent Bit: lightweight, very common
- Vector: excellent parsing/transform pipeline
- Filebeat: solid if you’re in the Elastic ecosystem
Collector responsibilities:
- Read container logs / files / journald
- Detect and merge multiline stack traces
- Parse JSON when valid
- Attach metadata like hostname, pod, namespace, service, env
- Route logs to different destinations based on type
3) Normalize and enrich before storage
Create a pipeline that:
- Tries to parse JSON
- Falls back to raw text if parsing fails
- Preserves the original message in a field like
raw_message - Extracts stack traces into one event
- Adds consistent tags:
log_type=app|audit|security|infrasource=api|worker|db|authtenantsensitivity=normal|restricted
4) Store in two tiers
Use separate destinations for different needs:
Hot search store
For operational troubleshooting:
- OpenSearch / Elasticsearch
- Grafana Loki if you want lower-cost log search and already use Grafana
- Retain 7–30 days depending on volume
Immutable archive for audit/compliance
For audit logs:
- Object storage like S3, GCS, or Azure Blob
- Enable:
- WORM/immutability
- versioning
- lifecycle policies
- encryption
- Retain based on policy, often 1–7 years
Handling messy JSON logs
Bad JSON logs are common when apps:
- print extra text before/after JSON
- emit partial JSON
- mix stack traces into the same line
- have embedded newline characters
Best practice
At the collector:
- Try strict JSON parsing.
- If it fails:
- preserve raw line
- tag as
parse_error - optionally apply regex cleanup rules for known formats
- Never drop the original event.
Practical tip
Keep both:
message: normalized display stringraw_message: original input
That helps when parsers break.
Handling multiline stack traces
This is critical for Java, Python, Go, Node, and .NET apps.
Multiline strategy
Configure the agent to detect the start of a new log entry using:
- timestamp prefix
- JSON opening brace
- known log format pattern
Then glue following lines together until the next event.
Examples:
- Java stack traces starting with
Exceptionand continuing withat ... - Python tracebacks beginning with
Traceback (most recent call last): - Logs with embedded
Caused by:
Important
Do multiline merging at the edge before shipping to the backend. If you wait until later, searching and alerting get much worse.
Audit logging design
Audit logs should be treated differently from app logs.
Audit log characteristics
- Append-only
- Highly structured
- Minimal free-form text
- Include:
- who did it
- what changed
- when
- from where
- result/success/failure
- object/resource identifier
- old/new values when appropriate
Separate pipeline
Send audit events to:
- a dedicated index/bucket
- stricter access controls
- longer retention
- immutable storage
Suggested fields
{
"timestamp": "2026-10-13T12:34:56Z",
"event_type": "user.permission_changed",
"actor": {"id": "u123", "type": "user"},
"target": {"type": "role", "id": "admin"},
"action": "grant",
"result": "success",
"source_ip": "203.0.113.10",
"service": "admin-api",
"environment": "prod"
}
Retention policy suggestion
A common setup:
Operational logs
- Hot searchable: 14–30 days
- Warm/cold archive: 90–180 days
- Optional long-term archive for specific teams
Audit logs
- Searchable: 90–180 days
- Immutable archive: 1–7 years, depending on compliance needs
Security logs
- Keep longer than app logs, often 6–12 months
Rules
- Separate retention by log class
- Keep compliance logs out of general deletion policies
- Use lifecycle rules automatically, not manual cleanup
Access control and security
- Restrict audit logs to a smaller group
- Encrypt in transit and at rest
- Use service identities for ingestion
- Mask secrets, tokens, passwords, PII
- Add tamper-evidence if needed:
- hash chaining
- immutable object storage
- signed log batches
A good default stack
If you want something pragmatic and widely used:
Option A: Open-source, flexible
- Fluent Bit or Vector for collection/parsing
- OpenSearch for search and dashboards
- S3-compatible object storage for immutable archive
- Grafana for visualization
Option B: Observability-centric
- Vector
- Loki
- S3
- Grafana
Option C: Elastic ecosystem
- Filebeat/Elastic Agent
- Elasticsearch
- ILM + S3 snapshots
- Kibana
Recommended implementation pattern
- Standardize app logging where possible.
- Use a collector to:
- merge multiline entries
- parse JSON
- enrich metadata
- Route logs into:
- app logs index
- audit logs index
- security logs index
- Apply retention by stream.
- Archive audit logs immutably.
- Monitor pipeline health and parse failures.
If you want the simplest reliable answer
Use:
- Vector or Fluent Bit
- multiline parsing at the edge
- JSON normalization
- OpenSearch/Loki for search
- S3 with immutability for audit retention
That setup covers all three problems well.
If you want, I can also give you:
- a reference architecture diagram,
- a Fluent Bit or Vector config example, or
- a retention policy template for app/security/audit logs.