slogctx

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: BSD-2-Clause Imports: 9 Imported by: 1

README

slogctx - context based logging

Usage

Initalise the logger by adding this in your main.go

slogctx.New(nil)

After this you can log with context with:

slogctx.Info(ctx, "log message",
    slog.String("var name", var),
    )

You can also use other methods, such as slog.Int(), slog.Bool(), slog.Any() and others. Avoid using slog.Any() when possible, as it uses reflect and that is an expensive operation.

Note: If you don't have context available you can replace it with nil

Using context

To add new fields to context, do it with slogctx.WithValue():

// ctx is already defined somewhere
slogctx.WithValue(ctx, "var name", var)

Modifying log level and time format

If you want to change the log level from the default (debug) you can pass new HandlerOptions to slogctx.New():

slogctx.New(&slogctx.HandlerOptions{
			AddSource:   true,
			Level:       slog.LevelDebug,
			ReplaceAttr: slogctx.ReplaceAttr,
			TimeFormat:  time.DateTime,
		})

Or if you want a new handler altogether, you can use slogctx.NewWithHandler():

slogctx.NewWithHandler(slog.NewTextHandler(os.Stdout,
		&slog.HandlerOptions{
			AddSource:   opts.AddSource,
			Level:       opts.Level,
			ReplaceAttr: opts.ReplaceAttr,
		}))

Note: You can't define time format with the default slog handler

Benefits

If you get context from, for example your service's REST endpoint being called, you can add values to context that might help with debugging. Then when you carry the context forwards, these values will be carried as well so identifying bugs and things will be easier.

Also our otel package adds a trace id to the context by default, so if you log context, you will much more easily find all the log entries from that call by filtering with that trace id.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string, args ...any)

func Error

func Error(ctx context.Context, msg string, args ...any)

func Info

func Info(ctx context.Context, msg string, args ...any)

func New

func New(opts *HandlerOptions)

func NewWithHandler added in v0.0.3

func NewWithHandler(handler slog.Handler)

func ReplaceAttr added in v0.0.2

func ReplaceAttr(groups []string, a slog.Attr) slog.Attr

func Warn

func Warn(ctx context.Context, msg string, args ...any)

func WithValue

func WithValue(parent context.Context, key string, val any) context.Context

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

func (Handler) Enabled

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

func (Handler) Handle

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

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 added in v0.0.3

type HandlerOptions struct {
	AddSource   bool
	Level       slog.Level
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
	TimeFormat  string
}

Jump to

Keyboard shortcuts

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