Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var LevelNames = map[Level]string{ LevelTrace: "TRACE", LevelDebug: "DEBUG", LevelInfo: "INFO", LevelNotice: "NOTICE", LevelWarn: "WARN", LevelError: "ERROR", LevelPanic: "PANIC", LevelFatal: "FATAL", }
Functions ¶
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.
Types ¶
type Logger ¶
type Logger interface { // Debug logs at LevelDebug. Debug(msg string, args ...any) // Debugf logs at LevelDebug. // Arguments are handled in the manner of fmt.Printf. Debugf(msg string, args ...any) // DebugContext logs at LevelDebug with the given context. DebugContext(ctx context.Context, msg string, args ...any) // Info logs at LevelInfo. Info(msg string, args ...any) // Infof logs at LevelInfo. // Arguments are handled in the manner of fmt.Printf. Infof(msg string, args ...any) // InfoContext logs at LevelInfo with the given context. InfoContext(ctx context.Context, msg string, args ...any) // Warn logs at LevelWarn. Warn(msg string, args ...any) // Warnf logs at LevelWarn. // Arguments are handled in the manner of fmt.Printf. Warnf(msg string, args ...any) // WarnContext logs at LevelWarn with the given context. WarnContext(ctx context.Context, msg string, args ...any) // Error logs at LevelError. Error(msg string, args ...any) // Errorf logs at LevelError. // Arguments are handled in the manner of fmt.Printf. Errorf(msg string, args ...any) // ErrorContext logs at LevelError with the given context. ErrorContext(ctx context.Context, msg string, args ...any) // Panic logs at LevelPanic and then panics with the given message. Panic(msg string, args ...any) // Panicf logs at LevelPanic and then panics. // Arguments are handled in the manner of fmt.Printf. Panicf(msg string, args ...any) // PanicContext logs at LevelPanic with the given context and then panics with the given message. PanicContext(ctx context.Context, msg string, args ...any) // Fatal logs at LevelFatal and then calls os.Exit(1). Fatal(msg string, args ...any) // Fatalf logs at LevelFatal and then calls os.Exit(1). // Arguments are handled in the manner of fmt.Printf. Fatalf(msg string, args ...any) // FatalContext logs at LevelFatal with the given context and then calls os.Exit(1). FatalContext(ctx context.Context, msg string, args ...any) // With calls Logger.With on the default logger. With(args ...any) Logger // WithGroup returns a Logger that starts a group, if name is non-empty. // The keys of all attributes added to the Logger will be qualified by the given // name. (How that qualification happens depends on the [Handler.WithGroup] // method of the Logger's Handler.) // // If name is empty, WithGroup returns the receiver. WithGroup(name string) Logger // Log emits a log record with the current time and the given level and message. // The Record's Attrs consist of the Logger's attributes followed by // the Attrs specified by args. // // The attribute arguments are processed as follows: // - If an argument is an Attr, it is used as is. // - If an argument is a string and this is not the last argument, // the following argument is treated as the value and the two are combined // into an Attr. // - Otherwise, the argument is treated as a value with key "!BADKEY". Log(ctx context.Context, level Level, msg string, args ...any) // LogAttrs is a more efficient version of [Logger.Log] that accepts only Attrs. LogAttrs(ctx context.Context, level Level, msg string, attrs ...slog.Attr) // Handler returns l's Handler. Handler() slog.Handler // Enabled reports whether l emits log records at the given context and level. Enabled(ctx context.Context, level Level) bool // ToSlog returns the underlying slog.Logger. ToSlog() *slog.Logger }
Logger is a interface that provides logging methods.
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 based on the provided slog.Logger.
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)
type Options ¶ added in v0.3.1
type Options struct { // Level is the minimum log level. Level string // Format is the log format. Format string // OpenTelemetry is a flag to enable OpenTelemetry support. OpenTelemetry bool // Handler is the log handler. Handler slog.Handler }
Options is the optional configuration for the logger.