Overview
Field helper functions provide a convenient and type-safe way to create structured log fields. These functions are zero-allocation and designed for efficient logging.String
Creates a Field with a string value. This is the most common field type for log messages.The field key
The string value
Field with type StringType.
Example
Int
Creates a Field with an int value. Useful for numeric data like counts, IDs, ports, etc.The field key
The integer value
Field with type IntType.
Example
Int64
Creates a Field with an int64 value. Use this for large integers or when you need explicit 64-bit precision.The field key
The int64 value
Field with type Int64Type.
Example
Float64
Creates a Field with a float64 value. Useful for measurements, percentages, etc.The field key
The float64 value
Field with type Float64Type.
Example
Bool
Creates a Field with a bool value. Useful for flags and boolean state.The field key
The boolean value
Field with type BoolType.
Example
Err
Creates a Field with an error value. The key is automatically set to “error” for consistency. Errors are formatted specially by formatters.The error value
Field with type ErrorType and key “error”.
Example
Any
Creates a Field with an arbitrary value. Use this when the type doesn’t match the typed helpers. The formatter will usefmt.Sprintf("%v") to format the value.
The field key
The value of any type
Field with type AnyType.
Example
Performance
All field helper functions are designed for zero allocations and optimal performance:- Field creation: ~0.34 ns/op with 0 allocations
- Zero-allocation design: Fields are simple structs passed by value
- Type safety: Compile-time type checking for field values
Best Practices
- Use typed helpers: Prefer
String(),Int(), etc. overAny()for better performance and type safety - Consistent naming: Use snake_case for field keys (e.g.,
user_id,request_id) - Error handling: Always use
Err()for error values to ensure consistent formatting - Avoid sensitive data: Don’t log passwords, tokens, or other secrets in field values