Add informative log messages with sufficient context to help operators diagnose issues, understand system state, and trace complex operations. Include relevant error details, system configuration status, and operational flow information.
Key practices:
Example from user accounting initialization:
utmp, utmpErr := NewUtmpBackend(cfg.UtmpFile, cfg.WtmpFile, cfg.BtmpFile)
if utmpErr == nil {
uacc.utmp = utmp
slog.DebugContext(ctx, "utmp user accounting is active")
}
wtmpdb, wtmpdbErr := NewWtmpdbBackend(cfg.WtmpdbFile)
if wtmpdbErr == nil {
uacc.wtmpdb = wtmpdb
slog.DebugContext(ctx, "wtmpdb user accounting is active")
}
if uacc.utmp == nil && uacc.wtmpdb == nil {
slog.DebugContext(
ctx, "no user accounting backends are available, sessions will not be logged locally",
"utmp_error", utmpErr, "wtmpdb_error", wtmpdbErr,
)
}
This approach helps operators understand what’s happening in the system and provides the context needed to resolve issues when they occur.
Enter the URL of a public GitHub repository