Documentation ¶
Index ¶
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func DebugContextf(ctx context.Context, format string, args ...any)
- func Debugf(format string, args ...any)
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func ErrorContextf(ctx context.Context, format string, args ...any)
- func Errorf(format string, args ...any)
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func InfoContextf(ctx context.Context, format string, args ...any)
- func Infof(format string, args ...any)
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- func WarnContextf(ctx context.Context, format string, args ...any)
- func Warnf(format string, args ...any)
- func WithLogger(ctx context.Context, logger *Logger) context.Context
- func WithValues(ctx context.Context, args ...any) context.Context
- type Handler
- type Logger
- func (l *Logger) Base() *slog.Logger
- func (l *Logger) DebugContextf(ctx context.Context, format string, args ...any)
- func (l *Logger) Debugf(format string, args ...any)
- func (l *Logger) ErrorContextf(ctx context.Context, format string, args ...any)
- func (l *Logger) Errorf(format string, args ...any)
- func (l *Logger) Handler() slog.Handler
- func (l *Logger) InfoContextf(ctx context.Context, format string, args ...any)
- func (l *Logger) Infof(format string, args ...any)
- func (l *Logger) WarnContextf(ctx context.Context, format string, args ...any)
- func (l *Logger) Warnf(format string, args ...any)
- func (l *Logger) With(args ...any) *Logger
- func (l *Logger) WithGroup(name string) *Logger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DebugContext ¶ added in v1.2.0
DebugContext calls DebugContext on the context logger.
func DebugContextf ¶ added in v1.2.0
DebugContextf calls DebugContextf on the context logger. If a Logger is found in the context, it will be used.
func ErrorContext ¶ added in v1.2.0
ErrorContext calls ErrorContext on the context logger.
func ErrorContextf ¶ added in v1.2.0
ErrorContextf calls ErrorContextf on the context logger.
func InfoContext ¶ added in v1.2.0
InfoContext calls InfoContext on the context logger. If a Logger is found in the context, it will be used.
func InfoContextf ¶ added in v1.2.0
InfoContextf calls InfoContextf on the context logger. If a Logger is found in the context, it will be used.
func WarnContext ¶ added in v1.2.0
WarnContext calls WarnContext on the context logger. If a Logger is found in the context, it will be used.
func WarnContextf ¶ added in v1.2.0
WarnContextf calls WarnContextf on the context logger. If a Logger is found in the context, it will be used.
func WithLogger ¶ added in v1.2.0
func WithValues ¶ added in v1.2.0
With returns a new context with the given values. Values are expected to be key-value pairs, where the key is a string. e.g. WithValues(ctx, "foo", "bar", "baz", 1) If a value already exists, it is overwritten. If an odd number of arguments are provided, With panics.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a slog.Handler that adds context values to the log record. Values are added via WithValues.
Example ¶
package main import ( "context" "log/slog" "os" "github.com/wlynch/slogctx" "github.com/wlynch/slogctx/slogtest" ) func main() { log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ // Remove time for repeatable results ReplaceAttr: slogtest.RemoveTime, })) ctx := context.Background() ctx = slogctx.WithValues(ctx, "foo", "bar") log.InfoContext(ctx, "hello world", slog.Bool("baz", true)) }
Output: level=INFO msg="hello world" baz=true
func NewHandler ¶
NewHandler configures a new context aware slog handler. If h is nil, the default slog handler is used.
type Logger ¶ added in v1.2.0
Logger implements a wrapper around slog.Logger that adds formatter functions (e.g. Infof, Errorf)
Example ¶
package main import ( "context" "log/slog" "os" "github.com/wlynch/slogctx" "github.com/wlynch/slogctx/slogtest" ) func main() { log := slogctx.NewLogger(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ // Remove time for repeatable results ReplaceAttr: slogtest.RemoveTime, }))) log = log.With("a", "b") ctx := slogctx.WithLogger(context.Background(), log) // Grab logger from context and use // Note: this is a formatter aware method, not an slog.Attr method. slogctx.FromContext(ctx).With("foo", "bar").Infof("hello %s", "world") // Package level context loggers are also aware slogctx.ErrorContext(ctx, "asdf", slog.Bool("baz", true)) }
Output: level=INFO msg="hello world" a=b foo=bar level=ERROR msg=asdf a=b baz=true
func DefaultLogger ¶ added in v1.2.0
func DefaultLogger() *Logger
DefaultLogger returns a new logger that uses the default slog.Logger.
func FromContext ¶ added in v1.2.0
func New ¶ added in v1.2.0
New returns a new logger that wraps the given slog.Handler.
func NewLogger ¶ added in v1.2.0
NewLogger returns a new logger that wraps the given slog.Logger.
func (*Logger) Base ¶ added in v1.2.0
Base returns the underlying slog.Logger.
func (*Logger) DebugContextf ¶ added in v1.2.0
DebugContextf logs at LevelDebug with the given context, format and arguments.
func (*Logger) Debugf ¶ added in v1.2.0
Debugf logs at LevelDebug with the given format and arguments.
func (*Logger) ErrorContextf ¶ added in v1.2.0
ErrorContextf logs at LevelError with the given context, format and arguments.
func (*Logger) Errorf ¶ added in v1.2.0
Errorf logs at LevelError with the given format and arguments.
func (*Logger) Handler ¶ added in v1.2.1
Handler returns the underlying slog.Handler.
func (*Logger) InfoContextf ¶ added in v1.2.0
InfoContextf logs at LevelInfo with the given context, format and arguments.
func (*Logger) WarnContextf ¶ added in v1.2.0
WarnContextf logs at LevelWarn with the given context, format and arguments.
func (*Logger) With ¶ added in v1.2.0
With calls Logger.With on the logger.