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: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LevelNames = map[Level]string{
	LevelTrace:  "TRACE",
	LevelDebug:  "DEBUG",
	LevelInfo:   "INFO",
	LevelNotice: "NOTICE",
	LevelWarn:   "WARN",
	LevelError:  "ERROR",
	LevelPanic:  "PANIC",
	LevelFatal:  "FATAL",
}

Functions

func IntoContext

func IntoContext(ctx context.Context, log 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(ctx 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.

Types

type Level

type Level = slog.Level

Level is the type of log levels.

const (
	LevelTrace  Level = slog.Level(-8)
	LevelDebug  Level = slog.LevelDebug
	LevelInfo   Level = slog.LevelInfo
	LevelNotice Level = slog.Level(2)
	LevelWarn   Level = slog.LevelWarn
	LevelError  Level = slog.LevelError
	LevelPanic  Level = slog.Level(12)
	LevelFatal  Level = slog.Level(16)
)

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

func FromContext(ctx context.Context) 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

FromSlog returns a new Logger instance based on the provided slog.Logger.

func NewLogger

func NewLogger(o ...Options) 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 ...Options) 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)

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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