logger

package
v0.0.0-...-c60ea24 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelDebug = Level(slog.LevelDebug)
	LevelInfo  = Level(slog.LevelInfo)
	LevelWarn  = Level(slog.LevelWarn)
	LevelError = Level(slog.LevelError)
)

Common log levels wrapped into our custom Level type.

Variables

This section is empty.

Functions

func NewStdLogger

func NewStdLogger(logger *Logger, level Level) *log.Logger

NewStdLogger creates a standard library log.Logger using the underlying slog handler. Useful for compatibility with packages expecting the old log.Logger API.

Types

type EventFn

type EventFn func(ctx context.Context, r Record)

EventFn defines a function type for handling log events. It receives the context and the log record, enabling async or external processing.

type Events

type Events struct {
	Debug EventFn // Called for debug-level logs
	Info  EventFn // Called for info-level logs
	Warn  EventFn // Called for warning-level logs
	Error EventFn // Called for error-level logs
}

Events holds callbacks for different log levels. This allows assigning custom behavior for each log level independently.

type Level

type Level slog.Level

Level is a custom log level type based on slog.Level. It allows extending or customizing logging behavior while staying compatible with slog.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is a structured logging wrapper around slog.Handler. It supports trace ID injection, service name tagging, and custom event hooks.

func New

func New(w io.Writer, minLevel Level, serviceName string, traceIDFn TraceIDFn) *Logger

New creates a Logger with the given output, log level, service name, and optional trace ID function.

func NewWithEvents

func NewWithEvents(w io.Writer, minLevel Level, serviceName string, traceIDFn TraceIDFn, events Events) *Logger

NewWithEvents creates a Logger with custom event hooks for different log levels.

func NewWithHandler

func NewWithHandler(h slog.Handler) *Logger

NewWithHandler wraps an existing slog.Handler in a Logger.

func (*Logger) BuildInfo

func (log *Logger) BuildInfo(ctx context.Context)

BuildInfo logs the Go build information of the current binary. Useful for debugging and version tracking in production.

func (*Logger) Debug

func (log *Logger) Debug(ctx context.Context, msg string, args ...any)

Debug logs a debug-level message.

func (*Logger) Debugc

func (log *Logger) Debugc(ctx context.Context, caller int, msg string, args ...any)

Debugc logs a debug-level message with a custom caller skip depth.

func (*Logger) Error

func (log *Logger) Error(ctx context.Context, msg string, args ...any)

Error logs an error-level message.

func (*Logger) Errorc

func (log *Logger) Errorc(ctx context.Context, caller int, msg string, args ...any)

Errorc logs an error-level message with a custom caller skip depth.

func (*Logger) Info

func (log *Logger) Info(ctx context.Context, msg string, args ...any)

Info logs an info-level message.

func (*Logger) Infoc

func (log *Logger) Infoc(ctx context.Context, caller int, msg string, args ...any)

Infoc logs an info-level message with a custom caller skip depth.

func (*Logger) Warn

func (log *Logger) Warn(ctx context.Context, msg string, args ...any)

Warn logs a warning-level message.

func (*Logger) Warnc

func (log *Logger) Warnc(ctx context.Context, caller int, msg string, args ...any)

Warnc logs a warning-level message with a custom caller skip depth.

type Record

type Record struct {
	Time       time.Time      // Timestamp of the log event
	Message    string         // Main log message
	Level      Level          // Log level
	Attributes map[string]any // Additional structured attributes
}

Record represents a structured log entry. Unlike slog.Record, this struct can be easily stored, serialized, or sent over the network.

type TraceIDFn

type TraceIDFn func(ctx context.Context) string

TraceIDFn defines a function type for extracting a trace ID from the context. Useful for correlating logs in distributed systems.

Jump to

Keyboard shortcuts

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