Home Solutions Industries Portfolio Pricing Blog Affiliate Contact
Quote
← Back to all posts
Structured Logging Best Practices: Logs You Can Actually Search and Use
DevOps & Infrastructure

Structured Logging Best Practices: Logs You Can Actually Search and Use

BL
BlackCode Admin November 19, 2026 · 2 min read

Logs are often the first thing engineers look at during an incident, and too often they are a wall of unstructured text that is impossible to filter. Structured logging writes each event as machine-readable fields, usually JSON, so logs can be searched, filtered and aggregated.

What a good log event contains

  • Timestamp in UTC.
  • Level: debug, info, warning, error.
  • A short, consistent message.
  • Request or correlation ID.
  • Service name and environment.
  • Relevant context: user ID, order ID, endpoint, duration.
  • Error type and stack trace for failures.

Log levels, used consistently

LevelUse for
ERRORSomething failed and needs attention
WARNINGUnexpected but handled
INFOImportant business events: order placed, payment received
DEBUGDetail for troubleshooting, usually off in production

Tired of risky deployments and surprise downtime?

We set up CI/CD, monitoring and infrastructure that lets your team ship calmly.

WhatsApp us+254 731 209 601Book a free demo20 minutes, no costCall us+254 731 209 601

Never log these

  • Passwords, tokens and API keys.
  • Full card numbers or M-Pesa PINs.
  • ID numbers and health data unless strictly needed and protected.
  • Entire request bodies containing personal data.

Add automatic redaction for known sensitive fields. Logs are often kept longer and accessed more widely than databases, so leaks there are common.

Controlling cost and noise

  1. Log events, not every line of execution.
  2. Sample high-volume, low-value logs.
  3. Set retention: shorter for debug, longer for audit logs.
  4. Alert on error patterns rather than reading logs manually.

Key takeaways

  • Write logs as structured fields.
  • Include a correlation ID in every event.
  • Redact sensitive data automatically.
  • Set levels and retention deliberately.

Frequently asked questions

Where should logs be stored?

In a central, searchable system, not only on individual servers.

How long should we keep logs?

Typically 14 to 90 days for application logs, longer for security and audit logs.

Can Django produce structured logs?

Yes, with a JSON formatter and middleware that adds request IDs.

Tired of risky deployments and surprise downtime?

We set up CI/CD, monitoring and infrastructure that lets your team ship calmly.

WhatsApp us+254 731 209 601Book a free demo20 minutes, no costCall us+254 731 209 601

0 comments 0 likes

Comments (0)

Be the first to comment on this post.