logctx

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package logctx provides helpers for storing and retrieving *slog.Logger in context.Context, replacing the deprecated github.com/zitadel/logging.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnableHTTPClient

func EnableHTTPClient(client *http.Client, opts ...HTTPClientOption)

EnableHTTPClient wraps an http.Client's transport to log request/response via slog. Must be called before the client is used; the client must not have a custom Transport.

func FromContext

func FromContext(ctx context.Context) (*slog.Logger, bool)

FromContext retrieves a *slog.Logger from the context.

func Middleware

func Middleware(opts ...MiddlewareOption) func(http.Handler) http.Handler

Middleware returns a chi-compatible middleware that:

  • Injects the logger into the request context
  • Generates a request ID (or uses the custom ID function)
  • Logs each request with method, path, status, and duration

func ToContext

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

ToContext stores a *slog.Logger in the context.

Types

type HTTPClientOption

type HTTPClientOption func(*loggingTransport)

HTTPClientOption configures the HTTP client logging transport.

func WithClientGroup

func WithClientGroup(group string) HTTPClientOption

WithClientGroup sets a group attribute on the logger used for HTTP client logging.

func WithClientLogger

func WithClientLogger(logger *slog.Logger) HTTPClientOption

WithClientLogger sets a custom logger for HTTP client logging.

type MiddlewareOption

type MiddlewareOption func(*middlewareConfig)

MiddlewareOption configures the logging middleware.

func WithGroup

func WithGroup(group string) MiddlewareOption

WithGroup sets a group attribute on the logger for the middleware.

func WithIDFunc

func WithIDFunc(f func() slog.Attr) MiddlewareOption

WithIDFunc sets a custom function to generate a request ID attribute.

func WithLogger

func WithLogger(logger *slog.Logger) MiddlewareOption

WithLogger sets the base logger for the middleware.

Jump to

Keyboard shortcuts

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