Overview
Formatters control how Velo serializes log entries. The library provides two built-in formatters and supports custom formatter implementations through theEntry type.
Formatter type
TheFormatter type defines the serialization format for log entries.
Constants
Serializes log entries as human-readable, colorized text with key-value pairs.Output format:
Serializes log entries as structured JSON objects.Output format:
Using formatters
Setting the formatter
Specify the formatter when creating a logger:Text formatter example
The text formatter produces human-readable output with terminal colors:- Colorized log levels for better visibility
- Automatic quoting of values containing spaces or equals signs
- Custom styling support through the
DefaultStyles()system - Caller information formatted as
<file:line>
JSON formatter example
The JSON formatter produces structured output ideal for log aggregation:- Zero-allocation JSON encoding for maximum performance
- Bypasses standard library’s
json.Marshal - Proper escaping of special characters
- Supports all Go primitive types without reflection
- Unix timestamp formats (
unix,unix_milli) for time-series databases
Entry type
TheEntry type encapsulates all data associated with a single log event. Custom formatters can process this type to implement specialized serialization.
Fields
Timestamp when the log entry was created.
Loosely-typed key-value pairs attached to the log entry. Stored as alternating keys and values.
Strongly-typed fields created using helper functions like
String(), Int(), etc.Pre-serialized JSON data for fields that are known at logger creation time. Used internally for performance optimization.
Stack trace program counters when
ReportStacktrace is enabled.The primary log message.
Static prefix prepended to the message.
Formatted caller information (file and line number) when
ReportCaller is enabled.Layout string for formatting timestamps (e.g.,
"2006/01/02 15:04:05" or "unix").The formatter type that should serialize this entry.
The severity level of this log entry.
Custom formatters
While Velo doesn’t expose a public formatter interface, you can implement custom serialization by processing theEntry type directly:
Time formats
Both formatters support multiple timestamp formats:Performance notes
- The JSON formatter uses zero-allocation encoding that bypasses
json.Marshal - The text formatter uses pre-cached, styled level strings to minimize allocations
Entryobjects are pooled and reused to eliminate allocations- Pre-encoded JSON fields are calculated once at logger creation for static fields
Related
- Logger options - Configure formatters and other logger behavior
- Fields - Add structured data to log entries
- Levels - Control log severity