log

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package log defines the logging interface used across the radius library.

The library never imports a concrete logging implementation. All packages obtain a Logger via log.Default and call its methods. Applications that embed the library call log.SetDefault at startup to inject their own logger (slog, logrus, zap, etc.).

The interface signature mirrors log/slog.Logger so that the slog adapter (see slog.go) has zero overhead and callers can use the familiar key-value pair convention:

log.Default.With("func", "transport.UDPTransport.ReadPacket").
    Info("socket closed", "src", src.String())

The default logger is a NopLogger that discards everything. This keeps the library silent unless the application explicitly opts in.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefault

func SetDefault(l Logger)

SetDefault replaces the package-level Default logger. Intended to be called once at program startup; concurrent reads of Default during a concurrent SetDefault are not synchronized.

Types

type Logger

type Logger interface {
	// Debug logs a message at debug level with optional key-value pairs.
	Debug(msg string, args ...any)
	// Info logs a message at info level.
	Info(msg string, args ...any)
	// Warn logs a message at warn level.
	Warn(msg string, args ...any)
	// Error logs a message at error level.
	Error(msg string, args ...any)
	// With returns a new Logger that always includes the given key-value
	// pairs in every subsequent log record. The returned Logger shares no
	// mutable state with the original; both may be used concurrently.
	With(args ...any) Logger
}

Logger is the structured logging interface used by the radius library. Implementations must be safe for concurrent use.

The args parameter of each method follows the slog convention: a sequence of key-value pairs, where keys are strings and values are arbitrary. Implementations may format values however they like; slog adapters forward args verbatim, while logrus/zap adapters typically unpack each pair into their native field representation.

var Default Logger = NopLogger{}

Default is the Logger used by every package in the library. It is initialized to a NopLogger that discards all output. Applications should call SetDefault at startup to inject a real logger.

Default is safe for concurrent reads. SetDefault must not be called concurrently with reads; applications should set it exactly once at program start before any goroutine starts using the library.

func GetDefault

func GetDefault() Logger

GetDefault returns the current Default logger. Prefer storing the logger in a local variable over calling this repeatedly.

func NewSlogAdapter

func NewSlogAdapter(l *slog.Logger) Logger

NewSlogAdapter returns a Logger backed by l. If l is nil, the returned Logger discards all output.

type NopLogger

type NopLogger struct{}

NopLogger discards all log records. It is the zero value of Default so that the library is silent unless an application opts in.

func (NopLogger) Debug

func (NopLogger) Debug(string, ...any)

Debug implements Logger.

func (NopLogger) Error

func (NopLogger) Error(string, ...any)

Error implements Logger.

func (NopLogger) Info

func (NopLogger) Info(string, ...any)

Info implements Logger.

func (NopLogger) Warn

func (NopLogger) Warn(string, ...any)

Warn implements Logger.

func (NopLogger) With

func (n NopLogger) With(...any) Logger

With implements Logger.

type SlogAdapter

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

SlogAdapter wraps a *slog.Logger to satisfy the Logger interface. Because the Logger interface mirrors slog.Logger's method signatures, each call is a direct forward with no allocation.

A nil *slog.Logger is treated as a NopLogger to avoid nil-pointer dereferences in callers that obtain a logger from third-party code.

func (SlogAdapter) Debug

func (s SlogAdapter) Debug(msg string, args ...any)

Debug implements Logger.

func (SlogAdapter) Error

func (s SlogAdapter) Error(msg string, args ...any)

Error implements Logger.

func (SlogAdapter) Info

func (s SlogAdapter) Info(msg string, args ...any)

Info implements Logger.

func (SlogAdapter) Warn

func (s SlogAdapter) Warn(msg string, args ...any)

Warn implements Logger.

func (SlogAdapter) With

func (s SlogAdapter) With(args ...any) Logger

With implements Logger. The returned Logger shares the underlying *slog.Logger after it has been augmented with args.

Jump to

Keyboard shortcuts

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