slogpfx

package module
v0.0.0-...-d8c5f92 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2023 License: MIT Imports: 4 Imported by: 1

README

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! 📜🖋️

Documentation

Overview

Example
// Create a handler that writes to stdout.
h := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: removeTimeAttr})

// Set the prefix for all log messages based on attribute "service".
prefixed := NewHandler(h, &HandlerOptions{
	PrefixKeys:      []string{"service"},
	PrefixFormatter: DefaultPrefixFormatter,
})

logger := slog.New(prefixed)

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

level=INFO msg="Hello World!"
level=INFO msg="billing > Hello World!"
level=ERROR msg="database > Connection error"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorizePrefix

func ColorizePrefix(f func(prefixes []slog.Value) string) func(prefixes []slog.Value) string

ColorizePrefix wraps a prefix formatter function to colorize its output with cyan ANSI codes.

func DefaultPrefixFormatter

func DefaultPrefixFormatter(prefixes []slog.Value) string

DefaultPrefixFormatter constructs a prefix string by joining all detected prefix values using ":". A " > " suffix is added at the end of the prefix string.

Types

type Handler

type Handler struct {
	Next slog.Handler // The next log handler in the chain.
	// contains filtered or unexported fields
}

Handler is a custom slog handler that wraps another slog.Handler to prepend a prefix to the log messages. The prefix is sourced from the log record's attributes using the keys specified in PrefixKeys.

func NewHandler

func NewHandler(next slog.Handler, opts *HandlerOptions) *Handler

NewHandler creates a new prefix logging handler. The new handler will prepend a prefix sourced from the log record's attributes to each log message before passing the record to the next handler.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle processes a log record, prepending a prefix to its message if needed, and then passes the record to the next handler.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

type HandlerOptions

type HandlerOptions struct {
	PrefixKeys []string // A list of keys used to fetch prefix values from the log record.

	// PrefixFormatter is a function to format the prefix of the log record.
	// If it's not set, the DefaultPrefixFormatter with ColorizePrefix wrapper is used.
	PrefixFormatter func(prefixes []slog.Value) string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL