Implement log levels in Go using the standard library's slog package by configuring a Handler with a specific Level threshold.
Go does not have built-in log levels; you must use the log package with a custom Writer or a third-party library like slog (Go 1.21+) to filter output by severity.
package main
import (
"log/slog"
"os"
)
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo, // Filter: Debug, Info, Warn, Error
}))
logger.Debug("This is hidden")
logger.Info("This is visible")
logger.Error("This is visible")
}
Log levels act like a volume knob for your application's messages, letting you hide minor details in production while keeping them visible during development. You set a minimum severity threshold, and the system automatically ignores anything quieter than that setting. Think of it as a security guard who only lets in visitors with a specific clearance level.