Skip to content
generated from atomicgo/template

䷖ Slog handler that maps different handlers to different log levels

License

Notifications You must be signed in to change notification settings

atomicgo/splitslog

Repository files navigation

AtomicGo | splitslog

Downloads Latest Release Tests Coverage Unit test count License: MIT Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/splitslog

splitslog

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"}
}

Index

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(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.

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