slog#

Standard library package for structured logging.

Usage#

Basic Logging#

The slog.<Method>("message", "<key>", <value>, ...) pattern is used for slog.Info, slog.Debug, slog.Warn, and slog.Error.

eg. slog.Info("user logged in", "id", userID)

Default format: YYYY/MM/DD HH:mm:ss LEVEL message key=value ...

JSON Handler#

Use JSONHandler for machine-readable JSON structured logging.

options := &slog.HandlerOptions{Level: slog.LevelDebug}
handler := slog.NewJSONHandler(os.Stderr, options)
mySlog := slog.New(handler)

lastLogin := time.Date(2023, 01, 01, 11, 50, 00, 00, time.UTC)
mySlog.Debug("debug message",
    "id", userID,
    "last_login", lastLogin)