Keys for "built-in" attributes.
const ( // TimeKey is the key used by the built-in handlers for the time // when the log method is called. The associated Value is a [time.Time]. TimeKey = slog.TimeKey // LevelKey is the key used by the built-in handlers for the level // of the log call. The associated value is a [Level]. LevelKey = slog.LevelKey // MessageKey is the key used by the built-in handlers for the // message of the log call. The associated value is a string. MessageKey = slog.MessageKey // SourceKey is the key used by the built-in handlers for the source file // and line of the log call. The associated value is a string. SourceKey = slog.SourceKey )
The following list is sorted alphabetically, but it's also important that KindAny is 0 so that a zero Value represents nil.
const ( KindAny = slog.KindAny KindBool = slog.KindBool KindDuration = slog.KindDuration KindFloat64 = slog.KindFloat64 KindInt64 = slog.KindInt64 KindString = slog.KindString KindTime = slog.KindTime KindUint64 = slog.KindUint64 KindGroup = slog.KindGroup KindLogValuer = slog.KindLogValuer )
func Debug(msg string, args ...any)
Debug calls Logger.Debug on the default logger.
func DebugContext(ctx context.Context, msg string, args ...any)
DebugContext calls Logger.DebugContext on the default logger.
func Error(msg string, args ...any)
Error calls Logger.Error on the default logger.
func ErrorContext(ctx context.Context, msg string, args ...any)
ErrorContext calls Logger.ErrorContext on the default logger.
func Info(msg string, args ...any)
Info calls Logger.Info on the default logger.
func InfoContext(ctx context.Context, msg string, args ...any)
InfoContext calls Logger.InfoContext on the default logger.
func Log(ctx context.Context, level Level, msg string, args ...any)
Log calls Logger.Log on the default logger.
func LogAttrs(ctx context.Context, level Level, msg string, attrs ...Attr)
LogAttrs calls Logger.LogAttrs on the default logger.
func NewLogLogger(h Handler, level Level) *log.Logger
NewLogLogger returns a new log.Logger such that each call to its Output method dispatches a Record to the specified handler. The logger acts as a bridge from the older log API to newer structured logging handlers.
func SetDefault(l *Logger)
SetDefault makes l the default Logger. After this call, output from the log package's default Logger (as with log.Print, etc.) will be logged at LevelInfo using l's Handler.
func Warn(msg string, args ...any)
Warn calls Logger.Warn on the default logger.
func WarnContext(ctx context.Context, msg string, args ...any)
WarnContext calls Logger.WarnContext on the default logger.
An Attr is a key-value pair.
type Attr = slog.Attr
func Any(key string, value any) Attr
Any returns an Attr for the supplied value. See [Value.AnyValue] for how values are treated.
func Bool(key string, v bool) Attr
Bool returns an Attr for a bool.
func Duration(key string, v time.Duration) Attr
Duration returns an Attr for a time.Duration.
func Float64(key string, v float64) Attr
Float64 returns an Attr for a floating-point number.
func Group(key string, args ...any) Attr
Group returns an Attr for a Group Value. The first argument is the key; the remaining arguments are converted to Attrs as in [Logger.Log].
Use Group to collect several key-value pairs under a single key on a log line, or as the result of LogValue in order to log a single value as multiple Attrs.
func Int(key string, value int) Attr
Int converts an int to an int64 and returns an Attr with that value.
func Int64(key string, value int64) Attr
Int64 returns an Attr for an int64.
func String(key, value string) Attr
String returns an Attr for a string value.
func Time(key string, v time.Time) Attr
Time returns an Attr for a time.Time. It discards the monotonic portion.
func Uint64(key string, v uint64) Attr
Uint64 returns an Attr for a uint64.
A 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.
Any of the Handler's methods may be called concurrently with itself or with other methods. It is the responsibility of the Handler to manage this concurrency.
Users of the slog package should not invoke Handler methods directly. They should use the methods of Logger instead.
type Handler = slog.Handler
HandlerOptions are options for a TextHandler or JSONHandler. A zero HandlerOptions consists entirely of default values.
type HandlerOptions = slog.HandlerOptions
JSONHandler is a Handler that writes Records to an io.Writer as line-delimited JSON objects.
type JSONHandler = slog.JSONHandler
func NewJSONHandler(w io.Writer, opts *HandlerOptions) *JSONHandler
NewJSONHandler creates a JSONHandler that writes to w, using the given options. If opts is nil, the default options are used.
Kind is the kind of a Value.
type Kind = slog.Kind
A Level is the importance or severity of a log event. The higher the level, the more important or severe the event.
type Level = slog.Level
Level numbers are inherently arbitrary, but we picked them to satisfy three constraints. Any system can map them to another numbering scheme if it wishes.
First, we wanted the default level to be Info, Since Levels are ints, Info is the default value for int, zero.
Second, we wanted to make it easy to use levels to specify logger verbosity. Since a larger level means a more severe event, a logger that accepts events with smaller (or more negative) level means a more verbose logger. Logger verbosity is thus the negation of event severity, and the default verbosity of 0 accepts all events at least as severe as INFO.
Third, we wanted some room between levels to accommodate schemes with named levels between ours. For example, Google Cloud Logging defines a Notice level between Info and Warn. Since there are only a few of these intermediate levels, the gap between the numbers need not be large. Our gap of 4 matches OpenTelemetry's mapping. Subtracting 9 from an OpenTelemetry level in the DEBUG, INFO, WARN and ERROR ranges converts it to the corresponding slog Level range. OpenTelemetry also has the names TRACE and FATAL, which slog does not. But those OpenTelemetry levels can still be represented as slog Levels by using the appropriate integers.
Names for common levels.
const ( LevelDebug Level = slog.LevelDebug LevelInfo Level = slog.LevelInfo LevelWarn Level = slog.LevelWarn LevelError Level = slog.LevelError )
A LevelVar is a Level variable, to allow a Handler level to change dynamically. It implements Leveler as well as a Set method, and it is safe for use by multiple goroutines. The zero LevelVar corresponds to LevelInfo.
type LevelVar = slog.LevelVar
A Leveler provides a Level value.
As Level itself implements Leveler, clients typically supply a Level value wherever a Leveler is needed, such as in HandlerOptions. Clients who need to vary the level dynamically can provide a more complex Leveler implementation such as *LevelVar.
type Leveler = slog.Leveler
A LogValuer is any Go value that can convert itself into a Value for logging.
This mechanism may be used to defer expensive operations until they are needed, or to expand a single value into a sequence of components.
type LogValuer = slog.LogValuer
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.
To create a new Logger, call New or a Logger method that begins "With".
type Logger = slog.Logger
func Default() *Logger
Default returns the default Logger.
func New(h Handler) *Logger
New creates a new Logger with the given non-nil Handler.
func With(args ...any) *Logger
With calls Logger.With on the default logger.
A Record holds information about a log event. Copies of a Record share state. Do not modify a Record after handing out a copy to it. Call NewRecord to create a new Record. Use [Record.Clone] to create a copy with no shared state.
type Record = slog.Record
func NewRecord(t time.Time, level Level, msg string, pc uintptr) Record
NewRecord creates a Record from the given arguments. Use [Record.AddAttrs] to add attributes to the Record.
NewRecord is intended for logging APIs that want to support a Handler as a backend.
Source describes the location of a line of source code.
type Source = slog.Source
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.
type TextHandler = slog.TextHandler
func NewTextHandler(w io.Writer, opts *HandlerOptions) *TextHandler
NewTextHandler creates a TextHandler that writes to w, using the given options. If opts is nil, the default options are used.
A Value can represent any Go value, but unlike type any, it can represent most small values without an allocation. The zero Value corresponds to nil.
type Value = slog.Value
func AnyValue(v any) Value
AnyValue returns a Value for the supplied value.
If the supplied value is of type Value, it is returned unmodified.
Given a value of one of Go's predeclared string, bool, or (non-complex) numeric types, AnyValue returns a Value of kind String, Bool, Uint64, Int64, or Float64. The width of the original numeric type is not preserved.
Given a time.Time or time.Duration value, AnyValue returns a Value of kind KindTime or KindDuration. The monotonic time is not preserved.
For nil, or values of all other types, including named types whose underlying type is numeric, AnyValue returns a value of kind KindAny.
func BoolValue(v bool) Value
BoolValue returns a Value for a bool.
func DurationValue(v time.Duration) Value
DurationValue returns a Value for a time.Duration.
func Float64Value(v float64) Value
Float64Value returns a Value for a floating-point number.
func GroupValue(as ...Attr) Value
GroupValue returns a new Value for a list of Attrs. The caller must not subsequently mutate the argument slice.
func Int64Value(v int64) Value
Int64Value returns a Value for an int64.
func IntValue(v int) Value
IntValue returns a Value for an int.
func StringValue(value string) Value
StringValue returns a new Value for a string.
func TimeValue(v time.Time) Value
TimeValue returns a Value for a time.Time. It discards the monotonic portion.
func Uint64Value(v uint64) Value
Uint64Value returns a Value for a uint64.