slog

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, attrs ...Attr)

Debug calls Logger.Debug on the default logger.

func Error

func Error(msg string, attrs ...Attr)

Error calls Logger.Error on the default logger.

func Info

func Info(msg string, attrs ...Attr)

Info calls Logger.Info on the default logger.

func Log

func Log(level Level, msg string, attrs ...Attr)

Log calls Logger.Log on the default logger.

func SetDefault

func SetDefault(l *Logger)

SetDefault sets the default Logger which is used by the top-level functions Info, Debug and so on.

func Warn

func Warn(msg string, attrs ...Attr)

Warn calls Logger.Warn on the default logger.

Types

type Attr

type Attr struct {
	Key   string
	Value Value
}

Attr is a key-value pair.

func Bool

func Bool(key string, value bool) Attr

Bool returns an Attr for a bool value.

func Duration

func Duration(key string, value time.Duration) Attr

Duration returns an Attr for a time.Duration value.

func Float64

func Float64(key string, value float64) Attr

Float64 returns an Attr for a float64 value.

func Int

func Int(key string, value int) Attr

Int returns an Attr for an int value.

func Int64

func Int64(key string, value int64) Attr

Int64 returns an Attr for an int64 value.

func String

func String(key, value string) Attr

String returns an Attr for a string value.

func Time

func Time(key string, value time.Time) Attr

Time returns an Attr for a time.Time value.

func Uint64

func Uint64(key string, value uint64) Attr

Uint64 returns an Attr for a uint64 value.

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 Kind

type Kind int

Kind represents the type of a Value.

const (
	KindAny Kind = iota // reserved for future use
	KindBool
	KindDuration
	KindFloat64
	KindInt64
	KindString
	KindTime
	KindUint64
)

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.

const (
	LevelDebug Level = -4
	LevelInfo  Level = 0
	LevelWarn  Level = 4
	LevelError Level = 8
)

func (Level) String

func (l Level) String() string

String returns a name for the level.

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.

func Default

func Default() *Logger

Default returns the default Logger.

func New

func New(h Handler) Logger

New creates a new Logger with the given non-nil Handler.

func (*Logger) Debug

func (l *Logger) Debug(msg string, attrs ...Attr)

Debug logs at LevelDebug.

func (*Logger) Enabled

func (l *Logger) Enabled(level Level) bool

Enabled reports whether the logger handles records at the given level.

func (*Logger) Error

func (l *Logger) Error(msg string, attrs ...Attr)

Error logs at LevelError.

func (*Logger) Handler

func (l *Logger) Handler() Handler

Handler returns the logger's Handler.

func (*Logger) Info

func (l *Logger) Info(msg string, attrs ...Attr)

Info logs at LevelInfo.

func (*Logger) Log

func (l *Logger) Log(level Level, msg string, attrs ...Attr)

Log logs a message at the given level with the given attrs.

func (*Logger) Warn

func (l *Logger) Warn(msg string, attrs ...Attr)

Warn logs at LevelWarn.

type Record

type Record struct {
	Time    time.Time
	Message string
	Level   Level
	Attrs   []Attr
}

Record holds information about a log event.

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 BoolValue

func BoolValue(v bool) Value

BoolValue returns a Value for a bool.

func DurationValue

func DurationValue(v time.Duration) Value

DurationValue returns a Value for a time.Duration.

func Float64Value

func Float64Value(v float64) Value

Float64Value returns a Value for a floating-point number.

func Int64Value

func Int64Value(v int64) Value

Int64Value returns a Value for an int64.

func IntValue

func IntValue(v int) Value

IntValue returns a Value for an int.

func StringValue

func StringValue(v string) Value

StringValue returns a new Value for a string.

func TimeValue

func TimeValue(v time.Time) Value

TimeValue returns a Value for a time.Time. It discards the monotonic portion.

func Uint64Value

func Uint64Value(v uint64) Value

Uint64Value returns a Value for a uint64.

func (Value) Bool

func (v Value) Bool() bool

Bool returns the value as a bool. Panics if v is not a bool.

func (Value) Duration

func (v Value) Duration() time.Duration

Duration returns the value as a time.Duration. Panics if v is not a duration.

func (Value) Float64

func (v Value) Float64() float64

Float64 returns the value as a float64. Panics if v is not a float64.

func (Value) Int

func (v Value) Int() int

Int returns the value as an int. Panics if v is not a signed integer.

func (Value) Int64

func (v Value) Int64() int64

Int64 returns the value as an int64. Panics if v is not a signed integer.

func (Value) Kind

func (v Value) Kind() Kind

Kind returns the kind of the value.

func (Value) String

func (v Value) String() string

String returns the value as a string. Panics if v is not a string.

func (Value) Time

func (v Value) Time() time.Time

Time returns the value as a time.Time. Panics if v is not a valid UnixNano time.

func (Value) Uint64

func (v Value) Uint64() uint64

Uint64 returns the value as a uint64. Panics if v is not an unsigned integer.

Jump to

Keyboard shortcuts

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