Documentation | Contributing | Code of Conduct
import "atomicgo.dev/splitslog"
Package splitslog provides a handler that splits log records to different handlers based on their level.
The most common use case is to split logs to stdout and stderr based on their level.
package main
import (
"log/slog"
"os"
"atomicgo.dev/splitslog"
)
func main() {
splitter := splitslog.Splitter{
// Debug and info messages are printed to stdout.
slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil),
slog.LevelInfo: slog.NewJSONHandler(os.Stdout, nil),
// Warn and error messages are printed to stderr.
slog.LevelWarn: slog.NewJSONHandler(os.Stderr, nil),
slog.LevelError: slog.NewJSONHandler(os.Stderr, nil),
}
handler := splitslog.NewSplitHandler(splitter)
logger := slog.New(handler)
logger.Info("info message prints to stdout")
logger.Error("error message prints to stderr")
// stdout: {"time":"2023-09-07T16:56:22.563817+02:00","level":"INFO","msg":"info message prints to stdout"}
// stderr: {"time":"2023-09-07T16:56:22.564103+02:00","level":"ERROR","msg":"error message prints to stderr"}
}
- type SplitHandler
- func NewSplitHandler(splitter Splitter) *SplitHandler
- func (h *SplitHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *SplitHandler) Handle(ctx context.Context, record slog.Record) error
- func (h *SplitHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *SplitHandler) WithGroup(name string) slog.Handler
- type Splitter
type SplitHandler
SplitHandler is a handler that splits log records to different handlers based on their level.
type SplitHandler struct {
Splitter Splitter
// contains filtered or unexported fields
}
func NewSplitHandler
func NewSplitHandler(splitter Splitter) *SplitHandler
NewSplitHandler returns a new SplitHandler.
package main
import (
"log/slog"
"os"
"atomicgo.dev/splitslog"
)
func main() {
splitter := splitslog.Splitter{
// Debug and info messages are printed to stdout.
slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil),
slog.LevelInfo: slog.NewJSONHandler(os.Stdout, nil),
// Warn and error messages are printed to stderr.
slog.LevelWarn: slog.NewJSONHandler(os.Stderr, nil),
slog.LevelError: slog.NewJSONHandler(os.Stderr, nil),
}
handler := splitslog.NewSplitHandler(splitter)
logger := slog.New(handler)
logger.Info("info message prints to stdout")
logger.Error("error message prints to stderr")
// stdout: {"time":"2023-09-07T16:56:22.563817+02:00","level":"INFO","msg":"info message prints to stdout"}
// stderr: {"time":"2023-09-07T16:56:22.564103+02:00","level":"ERROR","msg":"error message prints to stderr"}
}
func (*SplitHandler) Enabled
func (h *SplitHandler) Enabled(ctx context.Context, level slog.Level) bool
Enabled implements Handler.Enabled.
func (*SplitHandler) Handle
func (h *SplitHandler) Handle(ctx context.Context, record slog.Record) error
Handle implements Handler.Handle.
func (*SplitHandler) WithAttrs
func (h *SplitHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs implements Handler.WithAttrs.
func (*SplitHandler) WithGroup
func (h *SplitHandler) WithGroup(name string) slog.Handler
WithGroup implements Handler.WithGroup.
type Splitter
Splitter is a map of log levels to handlers. The default log levels (slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError) must be present, otherwise the SplitHandler panics.
type Splitter map[slog.Level]slog.Handler
Generated by gomarkdoc
AtomicGo.dev · with ❤️ by @MarvinJWendt | MarvinJWendt.com