The Attr type

July 2, 2026

Other than logger methods, the slog.Attr type is probably the thing you’ll use the most in your use of the slog package:

Types

type Attr

type Attr struct {
	Key   string
	Value Value
}

An Attr is a key-value pair.

This type has several type-specific constructors, too, but we’re going to do an abbrevaited look at them, because they’re highly repetitive.

func Any

func Any(key string, value any) Attr

Any returns an Attr for the supplied value. See AnyValue for how values are treated.

Any provides the general-purpose way to create an Attr for any type.

var foo struct  {
  Some   int
  Random string
  Fields float64
}

slog.Any("foo", foo{})
slog.Any("bool", true) // Functionally equivalent to using slog.Bool

Notably, slog.Any can be used for types that also have their own constructors, listed below. The only difference in using the type-specific constructors is that they don’t have to do any runtime type assertion.

func Bool

func Duration

func Float64


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Backward compatibility

We’ve already discussed when the default log/slog Logger also serves as the default log Logger. But what if you want to send log logs to a log/slog Logger, without using the global defaults? Introducing NewLogLogger! func NewLogLogger 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.


The global logger

I’m going to do something I haven’t done before in these stdlib tours, and that is skip over a huge section of the GoDoc. That’s becase a huge section here is entirely redundant with what comes later: func Debug func Debug(msg string, args ...any) Debug calls Logger.Debug on the default logger. And we have virtually identical entries for each of the following: DebugContext Error ErrorContext Info InfoContext Log LogAttrs Warn WarnContext Each of these is simply an alias to the identically named method on the default logger.


slog Constants

We’ve made it through the overview of the slog documentation. Now it’s time to get down and dirty! First up, constants! There are four, and they all define built-in attribute keys: Constants 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 = "time" // LevelKey is the key used by the built-in handlers for the level // of the log call.

Get daily content like this in your inbox!

Subscribe