Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Structured Fields

Events and contexts support typed field methods. All methods are safe to call on a nil receiver (disabled events are no-ops).

Event Fields

MethodSignatureDescription
AnyAny(key string, val any)Arbitrary value
AnysAnys(key string, vals []any)Arbitrary value slice
Base64Base64(key string, val []byte)Byte slice as base64 string
BoolBool(key string, val bool)Boolean field
BoolsBools(key string, vals []bool)Boolean slice field
BytesBytes(key string, val []byte)Byte slice - auto-detected as JSON with highlighting, otherwise string
ColumnColumn(key, path string, line, column int)Clickable file:line:column hyperlink
DictDict(key string, dict *Event)Nested fields with dot-notation keys
DurationDuration(key string, val time.Duration)Duration field
DurationsDurations(key string, vals []time.Duration)Duration slice field
ErrErr(err error)Attach error; Send uses it as message, Msg/Msgf add "error" field
ErrsErrs(key string, vals []error)Error slice as string slice (nil errors render as <nil>)
Float64Float64(key string, val float64)Float field
Floats64Floats64(key string, vals []float64)Float slice field
FuncFunc(fn func(*Event))Lazy field builder; callback skipped on nil (disabled) events
HexHex(key string, val []byte)Byte slice as hex string
IntInt(key string, val int)Integer field
Int64Int64(key string, val int64)64-bit integer field
IntsInts(key string, vals []int)Integer slice field
Ints64Ints64(key string, vals []int64)64-bit integer slice field
JSONJSON(key string, val any)Marshals val to JSON with syntax highlighting
LineLine(key, path string, line int)Clickable file:line hyperlink
LinkLink(key, url, text string)Clickable URL hyperlink
PathPath(key, path string)Clickable file/directory hyperlink
PercentPercent(key string, val float64, opts ...PercentOption)Percentage with gradient colour; accepts [PercentOption] values
QuantitiesQuantities(key string, vals []string)Quantity slice field
QuantityQuantity(key, val string)Quantity field (e.g. "10GB")
RawJSONRawJSON(key string, val []byte)Pre-serialized JSON bytes, emitted verbatim with syntax highlighting
StrStr(key, val string)String field
StringerStringer(key string, val fmt.Stringer)Calls String() (nil-safe)
StringersStringers(key string, vals []fmt.Stringer)Slice of fmt.Stringer values
StrsStrs(key string, vals []string)String slice field
TimeTime(key string, val time.Time)Time field
TimesTimes(key string, vals []time.Time)Time slice field
UintUint(key string, val uint)Unsigned integer field
Uint64Uint64(key string, val uint64)64-bit unsigned integer field
UintsUints(key string, vals []uint)Unsigned integer slice field
Uints64Uints64(key string, vals []uint64)64-bit unsigned integer slice field
URLURL(key, url string)Clickable URL hyperlink (URL as text)
WhenWhen(condition bool, fn func(*Event))Conditional field builder; fn called only when condition is true

Nested Fields (Dict)

Group related fields under a common key prefix using dot notation:

clog.Info().Dict("request", clog.Dict().
  Str("method", "GET").
  Int("status", 200),
).Msg("Handled")
// INF â„šī¸ Handled request.method=GET request.status=200

Works with sub-loggers too:

logger := clog.With().Dict("db", clog.Dict().
  Str("host", "localhost").
  Int("port", 5432),
).Logger()

Finalising Events

clog.Info().Str("k", "v").Msg("message")  // Log with message
clog.Info().Str("k", "v").Msgf("n=%d", 5) // Log with formatted message
clog.Info().Str("k", "v").Send()          // Log with empty message
clog.Error().Err(err).Send()              // Log with error as message (no error= field)
clog.Error().Err(err).Msg("failed")       // Log with message + error= field