Documentation
¶
Overview ¶
Package xlog provides a thin wrapper around log/slog.
Index ¶
Constants ¶
const ErrorKey = "error"
Key used to denote error values.
Variables ¶
var ( String = slog.String Int64 = slog.Int64 Int = slog.Int Uint64 = slog.Uint64 Float64 = slog.Float64 Bool = slog.Bool Time = slog.Time Group = slog.Group Duration = slog.Duration Any = slog.Any )
Convenience slog.Attr generators. Allows cluttering imports (no need to import both log/slog and xlog).
var ErrNilWriter = errors.New("invalid writer: nil")
Functions ¶
Types ¶
type ErrorValue ¶
type ErrorValue struct {
// contains filtered or unexported fields
}
ErrorValue holds an error value.
func (ErrorValue) LogValue ¶
func (err ErrorValue) LogValue() slog.Value
LogValue implements slog.LogValuer.
func (ErrorValue) Value ¶
func (err ErrorValue) Value() slog.Value
Value extracts the error message.
type HandlerConfig ¶
type HandlerConfig struct {
// Output is the writer log records should be written to.
Output io.Writer
// Level is the minimum level to log. It is never nil.
Level slog.Leveler
// Source reports whether source positions should be included.
Source bool
}
HandlerConfig carries the resolved base configuration to a HandlerFactory. It gives handler provider packages (such as xlog/slogor) access to the settings shared by all handlers, without exposing xlog's internal option state.
type HandlerFactory ¶
type HandlerFactory func(*HandlerConfig) slog.Handler
A HandlerFactory constructs a slog.Handler from the resolved configuration.
type HandlerKind ¶
type HandlerKind uint8
HandlerKind classifies the output format a handler produces. It is used to detect conflicting handler options: requesting two different kinds (e.g. both text and JSON output) makes New return an error, while options of the same kind override each other (last one wins).
const ( // HandlerText denotes a human-readable text handler. HandlerText HandlerKind = iota + 1 // HandlerJSON denotes a machine-readable JSON handler. HandlerJSON // HandlerDiscard denotes a handler that mutes all output. HandlerDiscard )
type Logger ¶
type Logger interface {
// Debug writes log messages with DEBUG severity.
Debug(msg string, a ...slog.Attr)
// Info writes log messages with INFO severity.
Info(msg string, a ...slog.Attr)
// Warn writes log messages with WARN severity.
Warn(msg string, a ...slog.Attr)
// Error writes log messages with ERROR severity.
Error(msg string, a ...slog.Attr)
// Fatal writes log messages with ERROR severity, and then
// exits the whole program.
Fatal(msg string, a ...slog.Attr)
// With returns a Logger that includes the given attributes
// in each output operation.
With(a ...slog.Attr) Logger
}
A Logger allows writing messages with various severities.
func New ¶
New creates a new logger instance. By default, log messages are written to stdout, and the log level is INFO.
func NewDiscard ¶
func NewDiscard() Logger
NewDiscard produces basically the same logger as
xlog.New(xlog.Discard(), otheroptions...)
but without the option application overhead.
type Option ¶
type Option func(*options) error
An Option represents a functional configuration option. These are used to configure new logger instances.
func AsJSON ¶
func AsJSON() Option
AsJSON configures a JSONHandler, i.e. log messages will be printed as JSON string.
func AsText ¶
func AsText() Option
AsText configures a TextHandler, i.e. the message output is a simple list of key=value pairs with minimal quoting. It uses the handler from the standard library and therefore has no external dependencies.
For colorized, human-friendly text output, use the xlog/slogor sub-module, whose Colorized option is a drop-in replacement.
func Discard ¶
func Discard() Option
Discard mutes the logger. See also NewDiscard() for a simpler constructor.
func LeveledString ¶
LeveledString interprets s (see ParseLevel) and sets the log level. If s is unknown, the error will be revealed with New().
func UseHandler ¶
func UseHandler(kind HandlerKind, factory HandlerFactory) Option
UseHandler installs a handler produced by factory. The kind classifies the handler for conflict detection (see HandlerKind).
It is the extension point used by handler provider packages such as xlog/slogor, so that alternative backends can be plugged in without the core module depending on them.
func WithSource ¶
func WithSource() Option
WithSource enables source code positions in log messages.