Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

log/slog Integration

Use sloghandler.New to create a slog.Handler backed by a clog logger. This lets any code that accepts slog.Handler or *slog.Logger produce clog-formatted output.

import "github.com/gechr/clog/sloghandler"

h := sloghandler.New(clog.Default, nil)
logger := slog.New(h)

logger.Info("request handled", "method", "GET", "status", 200)
// INF ℹ️ request handled method=GET status=200

Options

h := sloghandler.New(clog.Default, &sloghandler.Options{
  AddSource: true,           // include source file:line in each entry
  Level:     slog.LevelWarn, // override minimum level (nil = use logger's level)
})

Level Mapping

slog levelclog level
< LevelDebugclog.LevelTrace
LevelDebugclog.LevelDebug
LevelInfoclog.LevelInfo
LevelWarnclog.LevelWarn
LevelErrorclog.LevelError
> LevelErrorclog.LevelFatal

Records mapped to LevelFatal are logged but do not call os.Exit - only clog’s own Fatal().Msg() does that.

Groups and Attrs

WithGroup and WithAttrs work as expected. Groups use dot-notation keys matching clog’s Dict() pattern:

import "github.com/gechr/clog/sloghandler"

h := sloghandler.New(clog.Default, nil)
logger := slog.New(h).WithGroup("req")

logger.Info("handled", "method", "GET", "status", 200)
// INF ℹ️ handled req.method=GET req.status=200