Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

ctxlog

v0.0.0-20261201013705-9f9696e572b3
cloudeng.io/logging/ctxlog

Package name and synopsis are indexed at v0.0.0-20261201234437-eeaa7e01491e. Documentation and version details below use v0.0.0-20261201013705-9f9696e572b3.

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

15 matches

Top ↑

Package ctxlog provides a context key and functions for logging to a context.

Functions

CaptureLog

func CaptureLog(ctx context.Context, level slog.Level)

CaptureLog redirects the standard library's default logger to write through the slog logger stored in ctx at the given level. Callers that use log.Print / log.Printf / log.Println will appear in the structured log stream alongside slog output.

log's own date/time prefix flags are cleared because slog records the timestamp independently. The previous flags and output are not restored; call this once at program startup.

Debug

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

Error

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

Info

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

Log

func Log(ctx context.Context, level slog.Level, msg string, args ...any)

LogDepth

func LogDepth(ctx context.Context, logger *slog.Logger, level slog.Level, depth int, msg string, args ...any)

LogDepth logs a message at the specified level with the caller information adjusted by the provided depth.

Logger

func Logger(ctx context.Context) *slog.Logger

Logger returns the logger from the given context. If no logger is set, it returns a discard logger.

Example

{

	ctx := context.Background()
	buf := &bytes.Buffer{}
	ctx = ctxlog.NewJSONLogger(ctx, buf, nil)

	logger := ctxlog.Logger(ctx)
	logger.Info("hello world", "user", "alice")

	ctx = ctxlog.WithAttributes(ctx, "requestID", "123")
	logger = ctxlog.Logger(ctx)
	logger.Info("processing request")

}

NewJSONLogger

func NewJSONLogger(ctx context.Context, w io.Writer, opts *slog.HandlerOptions) context.Context

NewJSONLogger returns a new context with a JSON logger.

Example

{

	ctx := context.Background()
	buf := &bytes.Buffer{}
	opts := &slog.HandlerOptions{
		Level: slog.LevelDebug,
	}
	ctx = ctxlog.NewJSONLogger(ctx, buf, opts)

	logger := ctxlog.Logger(ctx)
	logger.Debug("debug message")
	logger.Info("info message")

}

NewLogLogger

func NewLogLogger(ctx context.Context, level slog.Level) *log.Logger

NewLogLogger returns a new standard library logger that logs to the provided context's logger at the specified level.

Warn

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

WithAttributes

func WithAttributes(ctx context.Context, attributes ...any) context.Context

WithAttributes returns a new context with the embedded logger updated with the given logger attributes.

WithLogger

func WithLogger(ctx context.Context, logger *slog.Logger) context.Context

WithLogger returns a new context with the given logger.

Dependency graph