Skip to content

Go Log Zerolog Level

DodaTech 1 min read

In this tutorial, you'll learn about Zerolog: Log Level Not Working. We cover key concepts, practical examples, and best practices.

Zerolog log levels -- Configure zerolog's global log level to control which messages are printed.

The Problem

Zerolog only prints messages at or above the configured level. Debug messages are suppressed by default. Use zerolog.SetGlobalLevel to control output.

Wrong

log.Debug().Msg("connecting to database")
log.Info().Msg("server started")

Output:

// Debug message not printed. Default level is Info.
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Str("db", "postgres").Msg("connecting")
log.Info().Msg("server started")

Output:

{"level":"debug","db":"postgres","message":"connecting"}
{"level":"info","message":"server started"}

Prevention

  • Default level is Info. Debug/Trace are hidden
  • Use zerolog.SetGlobalLevel(zerolog.DebugLevel) to show debug
  • Use zerolog.TimeFieldFormat for custom timestamp
  • Level-based logging: log.Debug(), log.Info(), log.Error()
  • zerolog is the fastest Go logger

Common Mistakes with log zerolog level

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

**How to set level per module?**

Use sub-loggers: log.Level(zerolog.WarnLevel).

What log levels does zerolog support?

Trace, Debug, Info, Warn, Error, Fatal, Panic.

Does zerolog support pretty printing?

Yes. Use zerolog.ConsoleWriter for human-readable output.


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro