Logging Best Practices in Microservices and Kubernetes-Based Applications

Ife Ayelabola
9 min readJan 29, 2025

--

A few years ago, I wrote about logging best practices in traditional applications on my Medium account. However, logging has also evolved as software architectures have shifted towards microservices and containerised environments like Kubernetes. The old strategies still apply but must be extended to work effectively in distributed dynamic systems.

This revised post explores effective logging practices in microservices and Kubernetes applications. It clarifies each concept, details successful implementation strategies, and highlights key takeaways.

Thanks for reading! Subscribe for free to receive new posts and support my work.

Structured Logging: Essential for Scalability and Security

What is Structured Logging?

Structured logging is a way of logging data in a predictable, machine-readable format, often JSON. In microservices, structured logging ensures logs are easily parsed, searchable, and consistently formatted across different services. However, logging should also be considered part of application security. For example, we audit infrastructure for security and compliance, and logging allows us to audit application events, trace back actions, and detect anomalies.

Why Logging is a Security Protocol

Logging isn’t just for debugging — it provides a historical record of system events, making it a crucial security component. By maintaining structured logs, organisations can:

  • Detect tampering attempts or suspicious activities in real time.
  • Identify unauthorised access attempts and security breaches.
  • Ensure compliance with industry regulations like SOC 2, HIPAA, and GDPR.
  • Investigate incidents through forensic analysis when issues arise.
  • Mitigate repudiation attacks, where users deny performing specific actions by keeping immutable logs of all key events.

Log Privacy & Data Anonymisation

Logs should avoid storing sensitive user data to comply with regulations like GDPR and CCPA. Tokenisation and redaction can help prevent the exposure of personal data.

Example: Instead of logging:

{ "email": "user@example.com", "creditCard": "4242424242424242" }

Use:

{ "email": "u***@example.com", "creditCard": "**** **** **** 4242" }

How to Implement Structured Logging Securely

  • Use JSON format for logs instead of unstructured text.
  • Ensure all logs contain key security-relevant fields such as timestamps, request origins, and user activity.
  • Implement role-based access control (RBAC) to restrict access to logs.
  • Encrypt logs both in transit and at rest.
  • Integrate logs with SIEM (Security Information and Event Management) solutions like Splunk or Datadog for proactive security monitoring.

What We Have

  • Logs should be machine-readable (JSON format is preferred) for log aggregators.
  • Easily searchable using tools like ELK, Loki, and Splunk.
  • Ensures compliance and security auditing by logging critical system events.
  • Allows efficient debugging in a distributed system.

Example using Serilog in C#:

Log.Information("User {UserId} performed an action at {Timestamp}", userId, DateTime.UtcNow);

We can build a more resilient and auditable system by treating logging as a security mechanism rather than just a debugging tool.

Providing Context in Logs

What is Context in Logging?

Context in logging refers to adding meaningful information to log entries to ensure that logs are easily understandable and traceable. It helps engineers and operators quickly determine what happened, where, and why when debugging or analysing system behaviour.

Context includes details such as:

  • The application or service name.
  • The request ID or correlation ID for tracking.
  • Timestamps to track when the event occurred.
  • The user or session ID (if applicable and security compliant).
  • The method or function that generated the log.

However, while providing context, security must be a top priority — logs should never contain sensitive data such as passwords, or financial details.

How to Implement Context in Logs

  • Include metadata such as serviceName, requestId, and timestamp in each log.
  • Use structured logging formats like JSON to standardise context across logs.
  • Leverage Correlation IDs to link logs across multiple services (see the section on Correlation IDs below).
  • Avoid logging sensitive information to comply with security best practices.

Example structured log with context:

{
"timestamp": "2024-01-29T12:34:56Z",
"service": "user-service",
"level": "INFO",
"message": "User logged in",
"userId": "12345",
"requestId": "abcd-efgh-ijkl"
}

Providing structured and contextual logs ensures that logs remain useful for current developers and future teams who may need to debug issues years later.

What We Have

  • Contextual logs that provide clarity and traceability.
  • Use of structured formats like JSON to ensure uniformity.
  • Emphasis on avoiding sensitive data exposure.
  • Correlation ID integration for tracking distributed requests.

What is a Correlation ID?

A Correlation ID is a unique identifier assigned to a request or transaction as it traverses multiple services in a distributed system. It connects all logs associated with that request, simplifying the tracing of user actions, debugging of issues, and analysis of system performance.

Why Correlation IDs Are Important?

  • Enhances Traceability: Connects logs from various microservices that manage the same request.
  • Improves Debugging: Aids engineers in tracking a request’s journey through the system.
  • Supports Performance Monitoring: Identifies bottlenecks in request processing.
  • Facilitates Business Intelligence: Aggregates logs to better understand user behaviour and transactions.

Example Use Case

A user starts a checkout request on an e-commerce site. This request activates calls to:

  1. Authentication Service (to verify user identity)
  2. Order Service (to create an order)
  3. Payment Service (to process payment)
  4. Inventory Service (to update stock)

By assigning a Correlation ID (e.g., correlationId: “xyz-123-abc”), logs from all these services can be connected, offering a comprehensive view of the request lifecycle.

Deciding Log Levels: Filtering Noise in a Microservices World

What are Log Levels?

Log levels categorise logs by severity, helping filter out unnecessary noise while surfacing critical issues. They define the importance of a log message and guide engineers in prioritising issues.

Common Log Levels:

  • DEBUG: Provides detailed information for diagnosing issues during development. It should not be used in production.
  • INFO: General operational messages that indicate normal application behaviour.
  • WARN: Indicates a potential issue that does not affect functionality but may require attention.
  • ERROR: Captures failures in the application that need investigation but do not cause an immediate crash.
  • FATAL: Represents critical failures that may bring down the application.

Why It’s Important to Standardise Log Levels

Consistently using the correct log levels ensures that logs are meaningful and actionable. Teams should agree on how to classify logs to prevent overuse of ERROR or INFO messages where WARN would be more appropriate. This consistency helps:

  • Enable effective log filtering to concentrate on genuine issues.
  • Enhance log analysis and debugging by minimising unnecessary noise.
  • Assist in prioritising fixes based on log severity.
  • Assist junior engineers in understanding log categorisation via code reviews.

How to Implement Log Levels Effectively

  • Define a standard log-level policy for the team.
  • Review log levels during code reviews to maintain consistency and educate junior developers.
  • Use dynamic log-level configurations to adjust verbosity in production without redeploying services.
  • Ensure logging platforms allow filtering based on log levels for targeted analysis.

What We Have

  • Clearly defined log levels for categorisation.
  • Use of dynamic log-level settings in Kubernetes via environment variables.
  • Encourage discussion on log levels during code reviews.

Performance Considerations in Logging

Logging and Its Impact on Performance

Although logging is essential, it can introduce performance overhead, particularly in high-throughput applications developed in C#, Java or any other language. Frequent and excessive logging can:

  • Elevated CPU usage due to string formatting and file I/O operations.
  • Cause disk bloat if logs are not rotated and archived correctly.
  • Slow down the application when synchronous logging blocks execution.

How to Mitigate Performance Issues

  • Use Asynchronous Logging: Libraries like Serilog and NLog in C# support asynchronous logging to avoid blocking execution.
  • Batch Logging: Instead of writing logs immediately, accumulate and write them in batches to reduce I/O overhead.
  • Stream Logs to Kafka: By passing logs through a distributed streaming system like Apache Kafka, logs can be processed asynchronously in a scalable manner. Kafka ensures:
  • Logs are distributed efficiently across consumers.
  • If an error occurs, processing can resume from the last offset.
  • Logs can be replayed if necessary.
  • Implement Log Rotation: To retain logs efficiently, use log rotate or similar tools to archive and remove old logs, preventing disk space exhaustion.

Recommended Tools for Performance Optimisation

  • Serilog & NLog: Support asynchronous logging and structured output.
  • Apache Kafka: Distributed log streaming and processing.
  • logrotate: Automates log rotation and cleanup to free disk space.

What We Have

  • Use of asynchronous logging to prevent performance bottlenecks.
  • Streaming logs to Kafka for distributed processing.
  • Log rotation to manage disk space efficiently.

Rate-Limiting and Log Sampling

Rather than logging every request, only log a sample of non-critical messages, thereby reducing storage overhead.

Improvements to Logging Practices: Alerts, Multi-Environment Strategies, and Business Intelligence

Logging is not merely about capturing data; it’s about making it actionable. The improvements outlined below can enhance logging practices, rendering logs more valuable for monitoring, debugging, and business intelligence.

Alerts & Proactive Monitoring

What is Alert-Based Logging?

Alert-based logging triggers real-time notifications for anomalies, allowing teams to respond proactively. Instead of manually sifting through logs, alerts highlight key issues automatically.

How to Implement Alert-Based Logging

  • Identify Essential Metrics: Pinpoint necessary logs, such as elevated error rates, unsuccessful transactions, or security violations.
  • Establish Alert Notifications: Set up real-time notifications using log monitoring platforms like Grafana, Datadog, or Splunk.
  • Connect with Communication Platforms: Set up alerts to inform teams through channels like Slack, PagerDuty, or Microsoft Teams.
  • Optimise Alert Settings: Prevent alert fatigue by defining thresholds that guarantee only significant issues trigger notifications.

Example Alert Setup

  • Configure Grafana to send an alert when the error log count exceeds a predefined threshold:
alert:
conditions:
- type: threshold
threshold: 100
operator: gt
field: "log_error_count"
actions:
- notify: slack_channel
message: "A high error rate has been detected in the production logs!"

Benefits of Alert-Based Logging

  • Faster Incident Response: Detect and fix issues before they escalate.
  • Automated Monitoring: Reduce manual log searches.
  • Improved Reliability: Maintain system health by proactively addressing problems.

2. Multi-Environment Logging Strategies

What is Multi-Environment Logging?

Over logging ??

Different environments (development, staging, production) necessitate distinct logging strategies. Excessive logging in production may lead to performance issues, whereas insufficient logging in development can hinder debugging efforts.

We may not require the same logging level in the development environments as in the production environment. This will help us reduce noise, improve performance, or save costs.

How to Implement Multi-Environment Logging

  • Set Environment-Specific Log Levels: Configure different log levels for each environment.
  • Use Configuration Management: Store logging configurations in environment variables or configuration files.
  • Filter Logs by Environment: Use log aggregation tools to separate logs by environment.

Example Multi-Environment Log Configuration in C#

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Development": "Debug",
"Production": "Warning"
}
}
}

Benefits of Multi-Environment Logging

  • Improved Debugging in Development: Enhanced logging allows developers to pinpoint problems more effectively.
  • Enhanced Production Efficiency: Minimizing excessive logs conserves resources.
  • Distinct Log Separation: Simplifies the identification of various environments in log management systems.

3. Logs as a Business Intelligence Tool

What is Business Intelligence Logging?

A business intelligence log entry is a log record that documents information pertinent to company operations, user engagement, system efficiency, and feature usage. These logs enable organisations to derive valuable insights for informed decision-making, analytics, and optimisation.

How to Implement Business Intelligence Logging

  • Record Essential Business Events: Document occurrences like API response times, user interactions, and feature usage.
  • Focus on operational and user behaviour trends rather than just debugging.
  • Consolidate and Analyse Logs: Utilise platforms such as BigQuery, Snowflake, or AWS Athena to derive insights from structured logs.
  • Design Dashboards: Develop live visualisations in Grafana or Kibana to monitor critical metrics.

Example Business Intelligence Log Entry

{
"event": "APIResponseTime",
"endpoint": "/checkout",
"latency_ms": 250,
"timestamp": "2024-01-29T12:35:00Z"
}

Benefits of Business Intelligence Logging

  • Improve User Experience: Identify slow endpoints and optimise performance.
  • Feature Adoption Tracking: Understand which features users engage with most.
  • Data-Driven Decisions: Use logs to inform product development and operational improvements.

Wrapping Up: The Modern Approach to Logging

Logging in microservices and Kubernetes requires a structured, security-focused, and performance-optimised approach. Following these best practices can enhance debugging, security, and observability.

Your logs, with structured logging, correlation IDs, and observability tools, will help you debug and improve system reliability and security.

Organisations can extract more value from their logs by integrating alerts and monitoring, multi-environment logging, and business intelligence practices. Logging isn’t just about capturing data — it’s about using it to proactively manage applications, optimise performance, and drive business decisions.

Happy blogging!

--

--

No responses yet