Documentation
¶
Overview ¶
Package log defines a minimal, dependency-free logging port for gotd libraries.
A library writes structured records to a Logger and never logs through a global or imports a concrete logging framework. The consumer chooses the backend by passing an adapter: see the logslog and logzap subpackages for log/slog and go.uber.org/zap. When no logger is supplied, Nop discards every record.
The attribute model (Attr, Value) stores scalar values without boxing them into an interface, and the [Logger.Enabled] gate lets callers skip building attributes on hot paths when a level is disabled.
Index ¶
- type Attr
- func Any(key string, value any) Attr
- func Bool(key string, value bool) Attr
- func Duration(key string, value time.Duration) Attr
- func Error(err error) Attr
- func Float64(key string, value float64) Attr
- func Group(key string, attrs ...Attr) Attr
- func Int(key string, value int) Attr
- func Int32(key string, value int32) Attr
- func Int64(key string, value int64) Attr
- func NamedError(key string, err error) Attr
- func String(key, value string) Attr
- func Stringer(key string, value fmt.Stringer) Attr
- func Time(key string, value time.Time) Attr
- func Uint64(key string, value uint64) Attr
- type CallerSkipper
- type Helper
- func (h Helper) Debug(ctx context.Context, msg string, attrs ...Attr)
- func (h Helper) Enabled(ctx context.Context, level Level) bool
- func (h Helper) Error(ctx context.Context, msg string, attrs ...Attr)
- func (h Helper) Info(ctx context.Context, msg string, attrs ...Attr)
- func (h Helper) Logger() Logger
- func (h Helper) Named(name string) Helper
- func (h Helper) Warn(ctx context.Context, msg string, attrs ...Attr)
- func (h Helper) With(attrs ...Attr) Helper
- type Kind
- type Level
- type Logger
- type Namer
- type Value
- func (v Value) Any() any
- func (v Value) Bool() bool
- func (v Value) Duration() time.Duration
- func (v Value) Error() error
- func (v Value) Float64() float64
- func (v Value) Group() []Attr
- func (v Value) Int64() int64
- func (v Value) Kind() Kind
- func (v Value) String() string
- func (v Value) Time() time.Time
- func (v Value) Uint64() uint64
- type Wither
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attr ¶
Attr is a key-value pair attached to a log record.
func Any ¶
Any returns an Attr holding an arbitrary value. Prefer the typed constructors for scalars: Any boxes the value into an interface.
func Error ¶
Error returns an Attr with the conventional key "error". A nil err is kept as a nil-valued error Attr.
func Group ¶
Group returns an Attr whose value is the given child attributes. Adapters render it as a nested object. As a special case, an empty key inlines the children into the parent (like log/slog), matching zap's Inline.
func NamedError ¶
NamedError returns an error Attr under an explicit key. Unlike Error, which uses the conventional "error" key, this lets callers name the field.
type CallerSkipper ¶ added in v0.1.0
CallerSkipper is an optional Logger capability: returning a child Logger that attributes records to a caller skip frames further up the stack. Wrappers that sit between the call site and the adapter (such as Helper) use it so the adapter still reports the real caller. Adapters that compute the caller implement it by mapping onto a native mechanism (e.g. zap.AddCallerSkip); loggers that do not are returned unchanged by AddCallerSkip.
type Helper ¶
type Helper struct {
// contains filtered or unexported fields
}
Helper wraps a Logger with leveled convenience methods for call sites. It is a thin value type; adapters implement Logger, not Helper.
Build one with For; the zero Helper logs to Nop.
type Level ¶
type Level int
Level is the severity of a log record.
Values match log/slog (Debug=-4, Info=0, Warn=4, Error=8) so adapters convert by casting; this is an interop convenience, the values are defined here.
Severity levels.
type Logger ¶
type Logger interface {
// Enabled reports whether a record at level would be recorded. Callers use
// it to avoid building attributes on hot paths when logging is off.
Enabled(ctx context.Context, level Level) bool
// Log records one structured message. The attrs slice and its contents must
// not be retained after Log returns.
Log(ctx context.Context, level Level, msg string, attrs ...Attr)
}
Logger is the logging port a library writes structured records to.
Implementations must be safe for concurrent use by multiple goroutines.
var Nop Logger = nop{}
Nop is a Logger that discards every record. Its Enabled always returns false.
func AddCallerSkip ¶ added in v0.1.0
AddCallerSkip returns a Logger that attributes records skip frames further up the stack, using the logger's native mechanism when it implements CallerSkipper. It returns l unchanged when skip is 0 or l does not track caller depth.
func Named ¶
Named returns a child Logger tagged with name. It uses the logger's native Named when it implements Namer, otherwise it returns l unchanged (the name is advisory for backends without a naming concept). With an empty name it returns l unchanged.
type Namer ¶
Namer is an optional Logger capability: returning a child Logger tagged with a name. Adapters implement it to map onto a native mechanism (e.g. zap.Logger.Named); Named is a no-op for loggers that do not, since the name is advisory.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value holds an attribute value without boxing scalar kinds into an interface.
num carries int64/uint64/float64/bool/duration bits; s carries strings; a carries the boxed value for KindAny, KindTime and KindError. It is modeled on slog.Value but kept deliberately small and explicit.
func (Value) String ¶
String renders the value to a string. For KindString it returns the string unchanged, so Value also satisfies fmt.Stringer.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package logslog adapts a *slog.Logger to the gotd log.Logger port.
|
Package logslog adapts a *slog.Logger to the gotd log.Logger port. |
|
logzap
module
|
|
|
logzerolog
module
|