Documentation
¶
Overview ¶
Package slog provides structured logging, in which log records include a message, a severity level, and various other attributes expressed as key-value pairs.
It defines a type, Logger, which provides several methods such as Logger.Info and Logger.Error) for reporting events of interest.
Each Logger is associated with a Handler which processes the log records produced by the Logger (typically by writing them to standard error or a file).
A Logger output method creates a Record from the method arguments and passes it to the Handler, which decides how to handle it. There is a default Logger accessible through top-level functions (such as Info and Error) that call the corresponding Logger methods.
A log record consists of a time, a level, a message, and a set of key-value pairs, where the keys are strings and the values may be of any type. For example:
2024-01-15T10:30:00Z INFO hello world user=john count=42
The package provides top-level functions like Info and Error that call the corresponding methods of the Default Logger. The default logger uses a TextHandler that writes to os.Stderr and handles only records at or above LevelInfo.
Loosely based on the log/slog package.
Index ¶
- func Debug(msg string, attrs ...Attr)
- func Error(msg string, attrs ...Attr)
- func Info(msg string, attrs ...Attr)
- func Log(level Level, msg string, attrs ...Attr)
- func SetDefault(l *Logger)
- func Warn(msg string, attrs ...Attr)
- type Attr
- func Bool(key string, value bool) Attr
- func Duration(key string, value time.Duration) Attr
- func Float64(key string, value float64) Attr
- func Int(key string, value int) Attr
- func Int64(key string, value int64) Attr
- func String(key, value string) Attr
- func Time(key string, value time.Time) Attr
- func Uint64(key string, value uint64) Attr
- type Handler
- type Kind
- type Level
- type Logger
- func (l *Logger) Debug(msg string, attrs ...Attr)
- func (l *Logger) Enabled(level Level) bool
- func (l *Logger) Error(msg string, attrs ...Attr)
- func (l *Logger) Handler() Handler
- func (l *Logger) Info(msg string, attrs ...Attr)
- func (l *Logger) Log(level Level, msg string, attrs ...Attr)
- func (l *Logger) Warn(msg string, attrs ...Attr)
- type Record
- type TextHandler
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Log ¶
Log calls Logger.Log on the default logger.
Types ¶
type Attr ¶
Attr is a key-value pair.
type Handler ¶
type Handler interface {
// Enabled reports whether the handler handles records at the given level.
// The handler ignores records whose level is lower.
Enabled(level Level) bool
// Handle handles the Record.
// It will only be called when Enabled returns true.
// Logger discards any errors from Handle.
Handle(r Record) error
}
Handler handles log records produced by a Logger.
A typical handler may print log records to standard error, or write them to a file or database, or perhaps augment them with additional attributes and pass them on to another handler.
Users of the slog package should not invoke Handler methods directly. They should use the methods of Logger instead.
type Level ¶
type Level int
A Level is the importance or severity of a log event. The higher the level, the more important or severe the event.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger records structured information about each call to its Log, Debug, Info, Warn, and Error methods. For each call, it creates a Record and passes it to a Handler.
type TextHandler ¶
type TextHandler struct {
// contains filtered or unexported fields
}
TextHandler is a Handler that writes Records to an io.Writer as a sequence of key=value pairs separated by spaces and followed by a newline.
Each record is formatted as:
2024-01-15T10:30:00Z INFO hello world user=john count=42
Timestamp (RFC3339), level, message, then key=value pairs. String values with spaces are quoted.
func NewTextHandler ¶
func NewTextHandler(w io.Writer, level Level) TextHandler
NewTextHandler creates a TextHandler that writes to w and handles only records at or above the given level.
func (*TextHandler) Enabled ¶
func (h *TextHandler) Enabled(level Level) bool
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.
func (*TextHandler) Handle ¶
func (h *TextHandler) Handle(r Record) error
Handle formats the record and writes it to the writer.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value holds a value of a supported type.
func DurationValue ¶
DurationValue returns a Value for a time.Duration.
func Float64Value ¶
Float64Value returns a Value for a floating-point number.
func (Value) Duration ¶
Duration returns the value as a time.Duration. Panics if v is not a duration.