Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "log" "log/slog" "os" "github.com/fatih/color" "github.com/mashiike/slogutils" ) func main() { middleware := slogutils.NewMiddleware( slog.NewJSONHandler, slogutils.MiddlewareOptions{ ModifierFuncs: map[slog.Level]slogutils.ModifierFunc{ slog.LevelDebug: slogutils.Color(color.FgBlack), slog.LevelInfo: nil, slog.LevelWarn: slogutils.Color(color.FgYellow), slog.LevelError: slogutils.Color(color.FgRed, color.Bold), }, RecordTransformerFuncs: []slogutils.RecordTransformerFunc{ slogutils.ConvertLegacyLevel( map[string]slog.Level{ "DEBUG": slog.LevelDebug, "INFO": slog.LevelInfo, "WARN": slog.LevelWarn, "ERROR": slog.LevelError, }, false, ), }, Writer: os.Stdout, HandlerOptions: &slog.HandlerOptions{ Level: slog.LevelWarn, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { if a.Key == "time" { return slog.Attr{} } return a }, }, }, ) slog.SetDefault(slog.New(middleware)) ctx := slogutils.With(context.Background(), slog.Int64("request_id", 12)) slog.WarnContext(ctx, "foo") slog.ErrorContext(ctx, "bar") slog.DebugContext(ctx, "baz") slog.WarnContext(ctx, "buzz") log.Println("[DEBUG] this is not slog.") log.Println("[INFO] this is not slog.") log.Println("[WARN] this is not slog.") log.Println("[ERROR] this is not slog.") }
Output: {"level":"WARN","msg":"foo","request_id":12} {"level":"ERROR","msg":"bar","request_id":12} {"level":"WARN","msg":"buzz","request_id":12} {"level":"WARN","msg":"this is not slog."} {"level":"ERROR","msg":"this is not slog."}
Index ¶
- func ConvertLegacyLevel(levelMap map[string]slog.Level, caseInsensitive bool) func(slog.Record) slog.Record
- func DefaultAttrs(args ...any) func(slog.Record) slog.Record
- func DropAttrs(keys ...string) func(slog.Record) slog.Record
- func RenameAttrs(m map[string]string) func(slog.Record) slog.Record
- func UniqueAttrs() func(slog.Record) slog.Record
- func With(ctx context.Context, args ...any) context.Context
- type Middleware
- func (m *Middleware[H]) Clone() *Middleware[H]
- func (m *Middleware[H]) Enabled(ctx context.Context, l slog.Level) bool
- func (m *Middleware[H]) Handle(ctx context.Context, record slog.Record) error
- func (m *Middleware[H]) SetMinLevel(l slog.Leveler)
- func (m *Middleware[H]) WithAttrs(as []slog.Attr) slog.Handler
- func (m *Middleware[H]) WithGroup(name string) slog.Handler
- type MiddlewareOptions
- type ModifierFunc
- type RecordTransformerFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertLegacyLevel ¶ added in v0.2.0
func ConvertLegacyLevel(levelMap map[string]slog.Level, caseInsensitive bool) func(slog.Record) slog.Record
ConvertLegacyLevel returns a RecordTransformerFunc that converts legacy level to slog.Level. The legacy level is the first word in the message enclosed in square brackets. The levelMap maps the legacy level to slog.Level. If caseInsensitive is true, the legacy level is case insensitive. If the legacy level is not found in the levelMap, the slog.Record is not changed. If the slog.Record.Level is not slog.LevelInfo, the slog.Record is not changed.
Example:
ConvertLegacyLevel(map[string]slog.Level{"debug": slog.LevelDebug}, true) If the message is "[DEBUG] hello world", the slog.Record.Level is converted to slog.LevelDebug.
func DefaultAttrs ¶
DefaultAttrs returns a RecordTransformerFunc that adds the given attributes to a slog.Record if they don't already exist.
func DropAttrs ¶
DropAttrs returns a RecordTransformerFunc that drops the given attributes from a slog.Record.
func RenameAttrs ¶ added in v0.2.0
RenameAttrs returns a RecordTransformerFunc that renames the given attributes from a slog.Record.
func UniqueAttrs ¶ added in v0.2.0
UniqueAttrs returns a RecordTransformerFunc that removes duplicate attributes from a slog.Record.
Types ¶
type Middleware ¶
Middleware is a slog.Handler that modifies log lines.
func NewMiddleware ¶
func NewMiddleware[H slog.Handler](f func(io.Writer, *slog.HandlerOptions) H, opts MiddlewareOptions) *Middleware[H]
func (*Middleware[H]) Clone ¶
func (m *Middleware[H]) Clone() *Middleware[H]
Clone returns a new Middleware with the same Handler and modifierFuncs.
func (*Middleware[H]) SetMinLevel ¶ added in v0.4.0
func (m *Middleware[H]) SetMinLevel(l slog.Leveler)
type MiddlewareOptions ¶
type MiddlewareOptions struct { // ModifierFuncs is a map of log levels to ModifierFunc. ModifierFuncs map[slog.Level]ModifierFunc // RecordTransformerFuncs is a list of RecordTransformerFunc. RecordTransformerFuncs []RecordTransformerFunc // Writer is the writer to write to. Writer io.Writer // HandlerOptions are options for the handler. HandlerOptions *slog.HandlerOptions }
MiddlewareOptions are options for creating a Middleware.
type ModifierFunc ¶
ModifierFunc is a function that modifies a log line.
func Color ¶
func Color(attr ...color.Attribute) ModifierFunc
Color returns a ModifierFunc that colors the log line.