Documentation
¶
Overview ¶
Package logctx provides helpers for storing and retrieving *slog.Logger in context.Context, replacing the deprecated github.com/zitadel/logging.
Index ¶
- func EnableHTTPClient(client *http.Client, opts ...HTTPClientOption)
- func FromContext(ctx context.Context) (*slog.Logger, bool)
- func Middleware(opts ...MiddlewareOption) func(http.Handler) http.Handler
- func ToContext(ctx context.Context, logger *slog.Logger) context.Context
- type HTTPClientOption
- type MiddlewareOption
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 ¶
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
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.