Skip to content

Latest commit

 

History

History
59 lines (39 loc) · 1.41 KB

README.md

File metadata and controls

59 lines (39 loc) · 1.41 KB

slogpfx - Prefix Logging for slog

Easily prefix your log messages with attributes from the log record using slogpfx.

demo

Features

Installation

go get -u github.com/dpotapov/slogpfx

Usage

Here's a quick example to get you started:

h := slog.NewTextHandler(os.Stdout, nil)

// Use the prefix based on the attribute "service"
prefixed := slogpfx.NewHandler(h, &slogpfx.HandlerOptions{
    PrefixKeys:      []string{"service"}
})

logger := slog.New(prefixed)

logger.Info("Hello World!")
logger.Info("Hello World!", "service", "billing")
logger.With("service", "database").Error("Connection error")

Using in conjunction with lmittmann/tint and mattn/go-colorable packages:

h := tint.NewHandler(colorable.NewColorable(os.Stdout), nil)

// Use the prefix based on the attribute "service"
prefixed := slogpfx.NewHandler(h, &slogpfx.HandlerOptions{
    PrefixKeys:      []string{"service"},
})

logger := slog.New(prefixed)

Customization

You can customize the way prefixes are displayed using the PrefixFormatter option.


Happy Logging! 📜🖋️