Skip to main content

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.
func String(key, val string) Field
key
string
required
The field key
val
string
required
The string value
Returns: A Field with type StringType.

Example

logger.Info("User logged in",
    go_logs.String("username", "john"),
    go_logs.String("ip", "192.168.1.1"))

Int

Creates a Field with an int value. Useful for numeric data like counts, IDs, ports, etc.
func Int(key string, val int) Field
key
string
required
The field key
val
int
required
The integer value
Returns: A Field with type IntType.

Example

logger.Info("Request processed",
    go_logs.Int("status_code", 200),
    go_logs.Int("response_time_ms", 45))

Int64

Creates a Field with an int64 value. Use this for large integers or when you need explicit 64-bit precision.
func Int64(key string, val int64) Field
key
string
required
The field key
val
int64
required
The int64 value
Returns: A Field with type Int64Type.

Example

logger.Info("File size",
    go_logs.Int64("bytes", 1536299520))

Float64

Creates a Field with a float64 value. Useful for measurements, percentages, etc.
func Float64(key string, val float64) Field
key
string
required
The field key
val
float64
required
The float64 value
Returns: A Field with type Float64Type.

Example

logger.Info("Memory usage",
    go_logs.Float64("cpu_percent", 45.7),
    go_logs.Float64("memory_gb", 1.23))

Bool

Creates a Field with a bool value. Useful for flags and boolean state.
func Bool(key string, val bool) Field
key
string
required
The field key
val
bool
required
The boolean value
Returns: A Field with type BoolType.

Example

logger.Info("Feature flags",
    go_logs.Bool("debug_mode", true),
    go_logs.Bool("cache_enabled", false))

Err

Creates a Field with an error value. The key is automatically set to “error” for consistency. Errors are formatted specially by formatters.
func Err(err error) Field
err
error
required
The error value
Returns: A Field with type ErrorType and key “error”.

Example

logger.Error("Database connection failed",
    go_logs.Err(err),
    go_logs.String("host", "localhost"))

Any

Creates a Field with an arbitrary value. Use this when the type doesn’t match the typed helpers. The formatter will use fmt.Sprintf("%v") to format the value.
func Any(key string, val interface{}) Field
key
string
required
The field key
val
interface{}
required
The value of any type
Returns: A Field with type AnyType.

Example

type User struct {
    Name string
    Age  int
}

logger.Info("Complex data",
    go_logs.Any("user", User{Name: "John", Age: 30}))

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

  1. Use typed helpers: Prefer String(), Int(), etc. over Any() for better performance and type safety
  2. Consistent naming: Use snake_case for field keys (e.g., user_id, request_id)
  3. Error handling: Always use Err() for error values to ensure consistent formatting
  4. Avoid sensitive data: Don’t log passwords, tokens, or other secrets in field values

Build docs developers (and LLMs) love