Documentation ¶
Index ¶
- func FromContext(ctx context.Context) logger.Logger
- func FromSlog(l *slog.Logger) logger.Logger
- func IntoContext(ctx context.Context, log logger.Logger) context.Context
- func Middleware(ctx context.Context) func(http.Handler) http.Handler
- func NewContextWithLogger(parent context.Context) (context.Context, context.CancelFunc)
- func NewLogger(o ...logger.Options) logger.Logger
- func NewNamedLogger(name string, o ...logger.Options) logger.Logger
- type Logger
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext extracts the slog.Logger from the provided context. If the context does not have a logger, it returns a new logger with the default configuration. This function is useful for retrieving loggers from context in different parts of an application.
func FromSlog ¶ added in v0.3.0
FromSlog returns a new Logger instance from the provided slog.Logger.
func IntoContext ¶
IntoContext embeds the provided slog.Logger into the given context and returns the modified context. This function is used for passing loggers through context, allowing for context-aware logging.
func Middleware ¶
Middleware takes the logger from the context and adds it to the request context.
func NewContextWithLogger ¶
NewContextWithLogger creates a new context based on the provided parent context. It embeds a logger into this new context, which is a child of the logger from the parent context. The child logger inherits settings from the parent. Returns the child context and its cancel function to cancel the new context.
Note: If no logger is found in the parent context, a new logger with the default configuration is embedded into the new context.
func NewLogger ¶
NewLogger creates a new Logger instance with optional configurations. The logger can be customized by passing an Options struct which allows for setting the log level, format, OpenTelemetry support, and a custom handler. If no Options are provided, default settings are applied based on environment variables or internal defaults.
Example:
opts := logger.Options{Level: "INFO", Format: "JSON", OpenTelemetry: true} log := logger.NewLogger(opts) log.Info("Hello, world!")
func NewNamedLogger ¶
NewNamedLogger creates a new Logger instance with the provided name and optional configurations. This function allows for the same level of customization as NewLogger, with the addition of setting a logger name.
Example:
opts := logger.Options{Level: "DEBUG", Format: "TEXT"} log := logger.NewNamedLogger("myServiceLogger", opts)