skip to content

Search

Logging Levels in Go

2 min read

Severity and intent of the log message is what determines which logging level to use.

LogInfo ✅

  • The message describes expected, routine, or successful operations.
  • You’re providing context or breadcrumbs for debugging later.
  • The message is not indicative of a problem, but may still be useful for understanding flow or state.
log.Info("User successfully logged in")
log.Info("Started background job for report generation")
log.Info("Connected to database")

LogWarn ⚠️

  • Something unexpected happened, but the app can continue running.
  • It may not be critical now, but could cause issues down the line.
  • You’re highlighting a potential risk, degraded performance, or a fallback being used.
log.Warn("Failed to fetch user profile image; using default avatar")
log.Warn("Cache miss for key: session_123")
log.Warn("Third-party API responded slowly")

LogInfo vs. LogWarn - Rule of Thumb

Use Warn if you or another developer would want to be aware of the situation during routine monitoring or debugging, because it might signal a problem or inefficiency.

Use Info for useful milestones or routine operational insight.

LogError ❌

  • An operation failed and it shouldn’t have.
  • The issue prevents correct execution or requires manual attention.
  • You are handling the error (e.g. catching it and not crashing the program), but it’s still a real problem.
  • It signals a bug, data issue, or infrastructure problem.
log.Error("Failed to connect to database", zap.Error(err))
log.Error("Unable to save user preferences", zap.String("user_id", id), zap.Error(err))
log.Error("Unexpected panic in request handler", zap.Any("request", req), zap.Error(err))

Summary of logging levels

LevelUse for
DebugLow-level details, mainly useful during development or troubleshooting.
InfoExpected, routine operations. No issues.
WarnSomething unexpected or suboptimal, but not fatal.
ErrorSomething went wrong. Needs fixing or further investigation.
FatalLogs and exits the program immediately (use sparingly).