Go Log Zerolog Level
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.
Right
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
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead 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
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