logger

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2024 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) logger.Logger

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

func FromSlog(l *slog.Logger) logger.Logger

FromSlog returns a new Logger instance from the provided slog.Logger.

func IntoContext

func IntoContext(ctx context.Context, log logger.Logger) context.Context

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

func Middleware(ctx context.Context) func(http.Handler) http.Handler

Middleware takes the logger from the context and adds it to the request context.

func NewContextWithLogger

func NewContextWithLogger(parent context.Context) (context.Context, context.CancelFunc)

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

func NewLogger(o ...logger.Options) logger.Logger

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

func NewNamedLogger(name string, o ...logger.Options) logger.Logger

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)

Types

type Logger

type Logger = logger.Logger

Logger is the interface for the logger. Its build on top of slog.Logger and extends it with additional logging methods.

type Options added in v0.3.1

type Options = logger.Options

Options is the optional configuration for the logger.

Jump to

Keyboard shortcuts

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