The Field type represents a strongly typed key-value pair that avoids the interface boxing overhead associated with standard variadic arguments. Using Fields enables true zero allocation logging on the hot path.
Field type
type Field struct {
Key string
Str string
Any any
Int int64
Type FieldType
}
Represents a strongly typed key-value pair for efficient logging.
FieldType
Identifies the underlying data type of a strongly typed Field.
FieldType constants
StringType - Indicates a string field
IntType - Indicates an integer field
BoolType - Indicates a boolean field
ErrorType - Indicates an error field
TimeType - Indicates a time.Time field
DurationType - Indicates a time.Duration field
AnyType - Indicates an arbitrary interface field
ObjectType - Indicates a field implementing ObjectMarshaler
ArrayType - Indicates a field implementing ArrayMarshaler
IntsType - Indicates a slice of integers
StringsType - Indicates a slice of strings
TimesType - Indicates a slice of time.Time values
Field constructors
String
Constructs a Field containing a string value.
func String(key, val string) Field
Example
logger.InfoFields("user logged in",
velo.String("username", "alice"),
velo.String("ip", "192.168.1.1"),
)
Int
Constructs a Field containing an integer value.
func Int(key string, val int) Field
Example
logger.InfoFields("request processed",
velo.Int("status_code", 200),
velo.Int("response_size", 1024),
)
Int64
Constructs a Field containing a 64-bit integer value.
func Int64(key string, val int64) Field
Example
logger.InfoFields("processing complete",
velo.Int64("bytes_processed", 1073741824),
)
Bool
Constructs a Field containing a boolean value.
func Bool(key string, val bool) Field
Example
logger.InfoFields("authentication check",
velo.Bool("authenticated", true),
velo.Bool("admin", false),
)
Time
Constructs a Field containing a time.Time value.
func Time(key string, val time.Time) Field
Example
logger.InfoFields("event occurred",
velo.Time("timestamp", time.Now()),
velo.Time("scheduled_at", scheduledTime),
)
Duration
Constructs a Field containing a time.Duration value.
func Duration(key string, val time.Duration) Field
Example
start := time.Now()
// ... do work ...
logger.InfoFields("operation completed",
velo.Duration("elapsed", time.Since(start)),
)
Err
Constructs a Field containing an error value.
func Err(err error) Field
Automatically uses the key “error”.
Example
if err := db.Connect(); err != nil {
logger.ErrorFields("database connection failed", velo.Err(err))
}
Any
Constructs a Field containing an arbitrary interface value.
func Any(key string, val any) Field
Performance Note: Using Any incurs allocation overhead due to interface boxing. Prefer strongly typed constructors (like String or Int) when possible.
Example
logger.InfoFields("complex data",
velo.Any("config", map[string]string{"env": "production"}),
)
Object
Constructs a Field containing an ObjectMarshaler.
func Object(key string, val ObjectMarshaler) Field
The object implementing ObjectMarshaler
Use this to log complex structs with zero allocations.
Example
type User struct {
ID int
Name string
}
func (u User) MarshalLogObject(enc velo.ObjectEncoder) error {
enc.AddInt("id", u.ID)
enc.AddString("name", u.Name)
return nil
}
user := User{ID: 123, Name: "Alice"}
logger.InfoFields("user created", velo.Object("user", user))
Array
Constructs a Field containing an ArrayMarshaler.
func Array(key string, val ArrayMarshaler) Field
The array implementing ArrayMarshaler
Use this to log collections with zero allocations.
Example
type UserIDs []int
func (ids UserIDs) MarshalLogArray(enc velo.ArrayEncoder) error {
for _, id := range ids {
enc.AppendInt(id)
}
return nil
}
ids := UserIDs{1, 2, 3, 4, 5}
logger.InfoFields("processing users", velo.Array("user_ids", ids))
Slice field constructors
Ints
Constructs a Field containing a slice of integers.
func Ints(key string, val []int) Field
Example
logger.InfoFields("scores recorded",
velo.Ints("values", []int{95, 87, 92, 88}),
)
Strings
Constructs a Field containing a slice of strings.
func Strings(key string, val []string) Field
Example
logger.InfoFields("tags assigned",
velo.Strings("tags", []string{"important", "urgent", "review"}),
)
Times
Constructs a Field containing a slice of time.Time values.
func Times(key string, val []time.Time) Field
Example
timestamps := []time.Time{
time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC),
}
logger.InfoFields("events recorded",
velo.Times("event_times", timestamps),
)
Strongly typed fields provide the best performance:
// Fastest - zero allocations
logger.InfoFields("request",
velo.String("method", "GET"),
velo.Int("status", 200),
)
// Slower - uses reflection and allocations
logger.Info("request", "method", "GET", "status", 200)
// Slowest - multiple allocations from Any
logger.InfoFields("request",
velo.Any("method", "GET"),
velo.Any("status", 200),
)
For maximum performance in hot paths, always prefer strongly typed field constructors over loosely typed key-value pairs or the Any constructor.