logging

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AttrKeyOperation = "operation"
	AttrKeyComponent = "component"
	AttrKeyError     = "error"
	AttrKeyRequestID = "request_id"
	AttrKeyClientIP  = "client_ip"
	AttrKeyUserID    = "user_id"
	AttrKeyCount     = "count"
	AttrKeyHeaders   = "headers"
	AttrKeyIPCount   = "ip_count"
)

Variables

This section is empty.

Functions

func ClientIPFromCtx

func ClientIPFromCtx(ctx context.Context) (string, bool)

ClientIPFromCtx retrieves the client IP stored by WithClientIP. Returns empty string and false if not present.

func New

func New(opts Options) *slog.Logger

New creates a slog.Logger from the given options. Format "json" uses slog.JSONHandler; otherwise tint is used with ReplaceAttr for colored keys. When using tint, color is controlled by the Color option (set via LOG_COLOR env var).

func NewContextHandler

func NewContextHandler(inner slog.Handler) slog.Handler

NewContextHandler wraps inner with a handler that reads request-scoped values from context (request_id, client_ip, operation, user_id) and stamps them on every log record when present.

func NewShortID

func NewShortID() string

NewShortID returns a 6-character lowercase hex string derived from 3 random bytes. It is suitable for use as a short correlation ID in log entries.

func OperationFromCtx

func OperationFromCtx(ctx context.Context) (string, bool)

OperationFromCtx retrieves the operation stored by WithOperation. Returns empty string and false if not present.

func ParseLevel

func ParseLevel(s string) slog.Level

ParseLevel converts a string (e.g. "debug", "info", "warn", "error") to slog.Level. Invalid or empty values default to slog.LevelInfo.

func RequestIDFromCtx

func RequestIDFromCtx(ctx context.Context) (string, bool)

RequestIDFromCtx retrieves the request/flow ID stored by WithRequestID. Returns the empty string and false if no ID is present.

func UserIDFromCtx added in v0.4.0

func UserIDFromCtx(ctx context.Context) (int64, bool)

UserIDFromCtx retrieves the user ID stored by WithUserID. Returns 0 and false if not present.

func WithClientIP

func WithClientIP(ctx context.Context, ip string) context.Context

WithClientIP stores the client IP string in the context for the slog handler to stamp automatically.

func WithOperation

func WithOperation(ctx context.Context, operation string) context.Context

WithOperation stores the operation string in the context for the slog handler to stamp automatically.

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

WithRequestID stores a request or flow ID string in the context. The custom slog handler reads this value and stamps it on every log record.

func WithUserID added in v0.4.0

func WithUserID(ctx context.Context, id int64) context.Context

WithUserID stores the authenticated user's ID in the context for the slog handler to stamp automatically on every record produced during the request.

Types

type Format

type Format string
const JSONFormat Format = "json"

type Options

type Options struct {
	Level  slog.Level
	Format Format
	Color  bool // Enable colored output for tint format (ignored for JSON format)
}

Options configures logger creation. use Level (e.g. info) and Format (e.g. json).

Jump to

Keyboard shortcuts

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