slogctx

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 2 Imported by: 1

README

slogctx

CI codecov Enabled Linters Go Report Card Documentation license

slogctx means slog and context. It provides a simple way to log context information with the slog package.

Install

go get github.com/akm/slogctx@latest

Usage

You can register your preparing function like this:

	slogctx.Add(func(ctx context.Context, rec slog.Record) slog.Record {
		val, ok := ctx.Value(ctxKey1).(string)
		if ok {
			rec.Add("key1", val)
		}
		return rec
	})

And you can get a logger working with your handle function by using slogctx.New instead of slog.New .

    handler := slog.NewTextHandler(writer, nil)
    logger := slogctx.New(handler)

writer must be a io.Writer like os.Stdout, bytes.Buffer or etc.

	ctx := context.WithValue(context.Background(), ctxKey1, "value1")
	logger.InfoContext(ctx, "foo")

Then logger outputs a log with foo as msg and value1 as key1 like this:

time=2024-12-21T12:18:51.893+09:00 level=INFO msg=foo key1=value1

See example_test.go for more detail.

Alternatives

CONTRIBUTING

If you find a bug, typo, incorrect test, have an idea, or want to help with an existing issue, please create an issue or pull request.

LICENSE

MIT

Documentation

Overview

Package slogctx provides a simple way to log context information with the slog package.

slogctx.Add functin is used to add a function to add a value to slog.Record from context.Context. slogctx.New function is used to instantiate slog.Logger with a Handler extended the Handler given as an argument.

See examples for more information.

slogctx.Add and slogctx.New are package-level functions. slogctx.Add affects all logger instances created by slogctx.New. If you want to add a function to a specific logger instance, use slogctx.Namespace. slogctx.Namespace has more primitive functions such as Namespace.AddHandlerConv than slogctx.Add. You can create a namespace by slogctx.NewNamespace.

Example

An example of logging with a value in context.

// Define a empty anonymous struct as key for value in context.
type ctxKeyType struct{}
ctxKey := ctxKeyType{}

// Add a function to add a value to slog.Record from context.Context.
slogctx.Add(func(ctx context.Context, rec slog.Record) slog.Record {
	val, ok := ctx.Value(ctxKey).(string)
	if ok {
		rec.Add("key1", val)
	}
	return rec
})
// TextHandler with testOptions have a function to remove "time" from log.
hander := slog.NewTextHandler(os.Stdout, testOptions)

// Instantiate slog.Logger by using slog.New.
logger0 := slog.New(hander)
// Instantiate slog.Logger by using slogctx.New.
logger1 := slogctx.New(hander)

// Log with a value in context.
ctx1 := context.WithValue(context.TODO(), ctxKey, "value1")
logger0.InfoContext(ctx1, "blah blah")
logger1.InfoContext(ctx1, "blah blah")
Output:

level=INFO msg="blah blah"
level=INFO msg="blah blah" key1=value1

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v0.4.0

func Add(f RecordConv)

Add appends a RecordConv function to the default Namespace.

func New

func New(h slog.Handler) *slog.Logger

New returns a new Logger with the default Namespace.

func SetDefault

func SetDefault(ns *Namespace)

SetDefault sets the default Namespace.

Types

type Handle added in v0.4.0

type Handle = func(context.Context, slog.Record) error

Handle is a function as same as slog.Handler.Handle.

type HandleConv added in v0.4.0

type HandleConv = func(HandleFactory) HandleFactory

HandleConv is a function that converts a Handle function.

func RecordHandleConv added in v0.4.0

func RecordHandleConv(prepare RecordConv) HandleConv

RecordHandleConv converts a RecordConv function to a HandleConv function.

type HandleFactory added in v0.5.1

type HandleFactory = func(slog.Handler) Handle

HandleFactory is a function that creates a Handle function with a Handler.

type HandlerConv added in v0.4.0

type HandlerConv = func(slog.Handler) slog.Handler

HandleConv is a function that converts a Handle function.

func NewHandlerConv added in v0.4.0

func NewHandlerConv(fn HandleConv) HandlerConv

NewHandlerConv returns a HandlerConv function from a HandleConv function.

type Namespace

type Namespace []HandlerConv

Namespace is a slice of HandlerConv.

func Default

func Default() *Namespace

Default returns the default Namespace.

func NewNamespace

func NewNamespace() *Namespace

NewNamespace returns a new Namespace.

func (*Namespace) AddHandleConv added in v0.4.0

func (x *Namespace) AddHandleConv(fn HandleConv)

AddHandleConv appends a HandleConv function to the Namespace.

func (*Namespace) AddHandlerConv added in v0.4.0

func (x *Namespace) AddHandlerConv(fn HandlerConv)

AddHandlerConv appends a HandlerConv function to the Namespace.

func (*Namespace) AddRecordConv added in v0.4.0

func (x *Namespace) AddRecordConv(fn RecordConv)

AddRecordConv appends a RecordConv function to the Namespace.

func (*Namespace) New added in v0.4.0

func (x *Namespace) New(h slog.Handler) *slog.Logger

New returns a new Logger with the Namespace.

func (*Namespace) Wrap added in v0.4.0

func (x *Namespace) Wrap(h slog.Handler) slog.Handler

Wrap returns a new Handler with the Namespace.

type RecordConv added in v0.4.0

type RecordConv = func(context.Context, slog.Record) slog.Record

RecordConv is a function that converts a Record function with context.Context.

Jump to

Keyboard shortcuts

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