Documentation
¶
Index ¶
- Constants
- func ClientIPFromCtx(ctx context.Context) (string, bool)
- func New(opts Options) *slog.Logger
- func NewContextHandler(inner slog.Handler) slog.Handler
- func NewShortID() string
- func OperationFromCtx(ctx context.Context) (string, bool)
- func ParseLevel(s string) slog.Level
- func RequestIDFromCtx(ctx context.Context) (string, bool)
- func UserIDFromCtx(ctx context.Context) (int64, bool)
- func WithClientIP(ctx context.Context, ip string) context.Context
- func WithOperation(ctx context.Context, operation string) context.Context
- func WithRequestID(ctx context.Context, id string) context.Context
- func WithUserID(ctx context.Context, id int64) context.Context
- type Format
- type Options
Constants ¶
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 ¶
ClientIPFromCtx retrieves the client IP stored by WithClientIP. Returns empty string and false if not present.
func New ¶
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 ¶
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 ¶
OperationFromCtx retrieves the operation stored by WithOperation. Returns empty string and false if not present.
func ParseLevel ¶
ParseLevel converts a string (e.g. "debug", "info", "warn", "error") to slog.Level. Invalid or empty values default to slog.LevelInfo.
func RequestIDFromCtx ¶
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
UserIDFromCtx retrieves the user ID stored by WithUserID. Returns 0 and false if not present.
func WithClientIP ¶
WithClientIP stores the client IP string in the context for the slog handler to stamp automatically.
func WithOperation ¶
WithOperation stores the operation string in the context for the slog handler to stamp automatically.
func WithRequestID ¶
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.