Avatar Ilya Mateyko

go.astrophena.name/base Module

GitHub repository | Commit (26c5f09)

Base Go packages for my projects.

package logger

import "go.astrophena.name/base/logger"

Package logger provides a context-aware logger built on slog.

Index

Functions

func Debug

func Debug(ctx context.Context, msg string, attrs ...slog.Attr)

Debug logs a debug message.

func Error

func Error(ctx context.Context, msg string, attrs ...slog.Attr)

Error logs an error message.

func Info

func Info(ctx context.Context, msg string, attrs ...slog.Attr)

Info logs an info message.

func IsDefault

func IsDefault(l *Logger) bool

IsDefault returns true if l is the default Logger.

func LevelVar

func LevelVar(ctx context.Context) *slog.LevelVar

LevelVar retrieves the slog.LevelVar associated with the Logger in the context. If the context has no Logger, it returns a slog.LevelVar for a default Logger.

func Put

func Put(ctx context.Context, l *Logger) context.Context

Put returns a new context with the provided Logger.

func Warn

func Warn(ctx context.Context, msg string, attrs ...slog.Attr)

Warn logs a warning message.

Types

type Logger

type Logger struct {
	*slog.Logger
	Level *slog.LevelVar
	// contains filtered or unexported fields
}

Logger encapsulates an slog.Logger and allows attaching and detaching [slog.Handler]s at runtime. It also holds a slog.LevelVar that can be used to control the level of handlers that are created with it.

func Get

func Get(ctx context.Context) *Logger

Get retrieves the Logger from the context. If the context has no Logger, it returns a default Logger that discards all messages.

func New

func New(level *slog.LevelVar) *Logger

New creates a new Logger. The logger initially has no handlers. Its LevelVar is initialized to LevelInfo if level is nil.

func (*Logger) Attach

func (l *Logger) Attach(h slog.Handler)

Attach attaches a handler to the logger.

func (*Logger) Detach

func (l *Logger) Detach(h slog.Handler)

Detach detaches a handler from the logger.