Posts Guidelines for logging and tracing
Post
Cancel

Guidelines for logging and tracing

Purpose of this document is to Logging and Tracing Guidelines while developing application to avoid false alarm and do best practices.

Core Log Levels & Span Statuses

Effective observability requires distinguishing between system-level failures and business-level logic.

Log Levels

  • Fatal/Panic: Use fatal or panic error strictly when system should not continue. For example- Important environment initialization or resource initialization etc.
  • Error: Reserved strictly for System Failures. Use this when the application cannot perform its intended function due to external factors like a database being down, network timeouts, or infrastructure issues.
  • Warn: Used for Security Issues or Unusual Behavior. These aren’t necessarily failures yet but require investigation.
  • Info: Used for Business Validation and user-driven errors (e.g., an “Invalid Input” or “User Already Exists”).
  • Debug/Trace: High-verbosity logs intended for Development and deep-dive troubleshooting.

Span Statuses

In distributed tracing, spans help track the flow of a request.

  • Ok: Indicates success or successful business validation.
  • Error: Indicates a system failure.
  • Event: Used via AddEvent to attach specific business context or observability markers to a span.

The Five Golden Rules

  1. System Failures Only: Use the Error log level only when the system itself fails.
  2. Business Info: Use the Info log level for business logic outcomes (like validation failures).
  3. Real Errors Only: Use RecordError in spans specifically for technical failures, not business logic deviations.
  4. Contextual Events: Use AddEvent within spans to document business-related issues.
  5. Data Privacy: Never log sensitive information (PII, passwords, secrets).

Implementation Workflow

The following table illustrates how to handle logging and tracing across different architectural layers based on the workflow diagram:

LayerActionType
InitializerSystem initializationLog: Fatal/Panic; Span: No span event
ControllerInitial RequestLog: Info/Error; Span: Add Event
ServiceHandle LogicLog: Info (Optional); Span: Business Op
RepositorySystem FailureLog: Error; Span: Record Error
RepositoryBusiness ValidationLog: Info; Span: Add Event
External ServiceExternal FailureLog: Error; Span: Add Event

Practical Scenarios

Here is how to apply these guidelines to common real-world scenarios:

  • Invalid Login: This is a business logic outcome. Log as Info and use Add Event in the span.
  • Database Error / Timeout: This is a system failure. Log as Error and use Record Error in the span.
  • Unauthorized: Typically logged as Warn (security) or Info (business validation) depending on context.

Bonus

You can Download this Handbook in a diagram form to keep it handy.