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.
Types ¶
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
HandleFactory is a function that creates a Handle function with a Handler.
type HandlerConv ¶ added in v0.4.0
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 (*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.
type RecordConv ¶ added in v0.4.0
RecordConv is a function that converts a Record function with context.Context.