fields

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ErrorKey     = "error"
	TimestampKey = "ts"
)

Conventional keys used by helpers.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Key string

	Value any
	// contains filtered or unexported fields
}

Field is a portable structured field: a key plus a typed value. The unexported 'kind' enforces invariants via the constructors below.

func Any

func Any(k string, v any) Field

func Bool

func Bool(k string, v bool) Field

func Bools

func Bools(k string, v []bool) Field

func Dict

func Dict(k string, fields ...Field) Field

Dict groups sub-fields under a single key (zap: Object, slog: Group). NOTE: this does not copy the slice; pass a copy if you will mutate it.

func Duration

func Duration(k string, v time.Duration) Field

func Error

func Error(err error) Field

Error adds a non-nil error under the conventional key ("error"). If err is nil, returns a no-op.

func Errors

func Errors(k string, v []error) Field

func Float64

func Float64(k string, v float64) Field

func Float64s

func Float64s(k string, v []float64) Field

func From

func From(k string, v any) Field

From chooses a FieldKind for common types; otherwise returns Any.

func Hex

func Hex(k string, b []byte) Field

Hex encodes []byte as a lowercase hexadecimal string at the backend.

func Int

func Int(k string, v int) Field

func Int64

func Int64(k string, v int64) Field

func Int64s

func Int64s(k string, v []int64) Field

func Lazy

func Lazy(fn func() []Field) Field

Convenience for no-ctx callers. NOTE: The function should be fast and side-effect free.

func LazyFields

func LazyFields(fn func(context.Context) []Field) Field

LazyFields runs lazily at log time (only if enabled) and returns extra fields to append. NOTE: The function should be fast and side-effect free.

func NamedError

func NamedError(k string, err error) Field

NamedError adds a non-nil error with a custom key. If err is nil, returns a no-op.

func Nop

func Nop() Field

func RawJSON

func RawJSON(k string, json []byte) Field

RawJSON inserts pre-encoded JSON bytes under key. NOTE: Backends that don’t support raw JSON may encode it as a string or bytes (eg zap supports; slog may treat as []byte/string).

func String

func String(k, v string) Field

func Strings

func Strings(k string, v []string) Field

func TimeField

func TimeField(k string, v time.Time) Field

func Timestamp

func Timestamp(t time.Time) Field

Timestamp asks the backend to attach a timestamp field. If t is zero, backends should use time.Now(); otherwise use t. The key defaults to TimestampKey ("ts"); backends may honor Options.TimeLayout.

func TimestampAt

func TimestampAt(k string, t time.Time) Field

TimestampAt is the same as Timestamp but with a custom key.

func Uint

func Uint(k string, v uint) Field

func Uint64

func Uint64(k string, v uint64) Field

func Uint64s

func Uint64s(k string, v []uint64) Field

func (Field) IsSkip

func (f Field) IsSkip() bool

IsSkip reports whether the field should be emitted.

func (Field) IsZero

func (f Field) IsZero() bool

IsZero reports whether f is a no-op field.

func (Field) Kind

func (f Field) Kind() FieldKind

Kind returns the field's discriminant for backend mapping.

type FieldKind

type FieldKind uint8

FieldKind describes the concrete type carried by a Field's value.

const (
	FieldKindInvalid FieldKind = iota // zero / no-op
	FieldKindAny

	// Scalars
	FieldKindString
	FieldKindBool
	FieldKindInt64
	FieldKindUint64
	FieldKindFloat64
	FieldKindTime
	FieldKindDuration
	FieldKindError

	// Slices
	FieldKindStrings
	FieldKindBools
	FieldKindInt64s
	FieldKindUint64s
	FieldKindFloat64s
	FieldKindErrors

	// Special
	FieldKindDict       // sub-fields (Value is []Field)
	FieldKindRawJSON    // []byte that is already JSON
	FieldKindHexBytes   // []byte to render as hex string
	FieldKindLazyFields // lazy: func(context.Context) []Field
	FieldKindLazyValue  // lazy: func() []Field
	FieldKindTimestamp  // backend inserts current timestamp (or uses Value as time.Time if provided)
)

Jump to

Keyboard shortcuts

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