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 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.
func Color ¶
func Color() Option
Color enables colorful log output. If the output writer set by WriteTo isn't a TTY, or messages are output AsJSON, enabling colors won't have an effect.
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 WithSource ¶
func WithSource() Option
WithSource enables source code positions in log messages.