slogctx

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: BSD-2-Clause Imports: 14 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 an entry to be in every log line afterwards, add it with slogctx.WithValue():

// ctx is already defined somewhere
slogctx.WithValue(ctx, "ctx key", "ctx value")

This will cause slogctx.Info("foo") to print out "msg"="foo" "ctx key"="ctx value"

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

AWS and GCP specific logging

To enable AWS and GCP specific logging (for field names and value formatting) use

slogctx.New(&slogctx.HandlerOptions{
			EnableAWS: true,
			EnableGCP: true,
		})

This will auto-detect if the code is running in AWS or GCP environment and format accordingly. If it isn't running in either environment it will fall back to default behaviour (with a short delay)

Documentation

Index

Constants

View Source
const (
	LevelCritical slog.Level = 16
)
View Source
const LevelFatal slog.Level = 16

Variables

This section is empty.

Functions

func AWSReplaceAttr added in v0.1.0

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

func Critical added in v0.1.0

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

func Debug

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

func Error

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

func Fatal added in v0.1.0

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

func GCPReplaceAttr added in v0.1.0

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

func Info

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

func ReplaceAttr added in v0.0.2

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

func SetOutput added in v0.1.0

func SetOutput(out io.Writer)

func Warn

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

func WithValue

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

WithValue adds a key-value pair to the *logging* context NOTE: It will not add it to normal 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                                         // add calling file and line to logs
	Level       slog.Level                                   // log level, default is Info
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr // function to replace attributes
	TimeFormat  string                                       // time format, default is RFC3339
	Format      LogFormat                                    // log format, default is Text for readability
	EnableAWS   bool                                         // enable AWS specific logging
	EnableGCP   bool                                         // enable GCP specific logging
}

type LogFormat added in v0.1.0

type LogFormat string
const (
	JSONFormat LogFormat = "json"
	TextFormat LogFormat = "text"
)

type Logger added in v0.1.0

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

func New

func New(opts *HandlerOptions) *Logger

func NewWithHandler added in v0.0.3

func NewWithHandler(handler slog.Handler) *Logger

func (*Logger) Critical added in v0.1.0

func (l *Logger) Critical(ctx context.Context, msg string, args ...any)

func (*Logger) Debug added in v0.1.0

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

func (*Logger) Error added in v0.1.0

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

func (*Logger) Fatal added in v0.1.0

func (l *Logger) Fatal(ctx context.Context, msg string, args ...any)

func (*Logger) GetHandler added in v0.1.0

func (l *Logger) GetHandler() slog.Handler

func (*Logger) Info added in v0.1.0

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

func (*Logger) LogAttrs added in v0.1.1

func (l *Logger) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)

func (*Logger) Warn added in v0.1.0

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

Jump to

Keyboard shortcuts

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