Overview
SlogHandler adapts a Velo logger to satisfy the standard library’s slog.Handler interface. This allows you to use Velo’s high-performance logging as the backend for any code that uses the log/slog package.
SlogHandler type
- A reference to the underlying Velo logger
- Accumulated attributes from
WithAttrs()calls - The current group name from
WithGroup()calls
Functions
NewSlogHandler
Creates a newslog.Handler backed by a Velo logger.
The Velo logger to use as the backend for slog operations.
SlogHandler that implements slog.Handler.
Methods
Enabled
Determines whether the handler should process log records at the specified level.The context (currently unused by this implementation).
The slog level to check.
true if the underlying Velo logger’s level allows this slog level.
Level mapping:
slog.LevelError→velo.ErrorLevelslog.LevelWarn→velo.WarnLevelslog.LevelInfo→velo.InfoLevelslog.LevelDebug→velo.DebugLevel
Handle
Processes a slog record, converting it to a Velo log entry.The context (currently unused by this implementation).
The log record to process.
nil. Velo handles errors internally.
Behavior:
- Converts slog attributes to Velo fields
- Applies any group prefixes to field keys
- Includes previously added attributes from
WithAttrs() - Logs using the underlying Velo logger’s
LogFields()method
WithAttrs
Creates a new handler that includes the specified attributes in every log entry.The attributes to include in all subsequent log entries.
SlogHandler with the attributes applied. If attrs is empty, returns the same handler.
Attribute conversion:
slog.String→velo.String()slog.Int64→velo.Int64()slog.Bool→velo.Bool()slog.Duration→velo.Int64()(nanoseconds)slog.Time→velo.String()(formatted)errortypes →velo.Err()- Other types →
velo.Any()
WithGroup
Creates a new handler that prefixes all subsequent attribute keys with the specified group name.The group name to use as a prefix.
SlogHandler with the group applied.
Group behavior:
- Groups are separated by dots (
.) - Multiple nested groups are concatenated:
group1.group2.fieldname - The group applies to all attributes added after this call
Usage examples
Basic usage
Replace the default slog handler with Velo:Using WithAttrs
Add persistent attributes to all logs:Using WithGroup
Organize attributes into logical groups:Nested groups
Groups can be nested for hierarchical organization:Integration with existing slog code
Use Velo as a drop-in replacement for better performance:Attribute type mapping
The handler automatically converts slog attribute types to Velo field types:| slog Type | Velo Type | Notes |
|---|---|---|
KindString | String() | Direct string value |
KindInt64 | Int64() | 64-bit integer |
KindBool | Bool() | Boolean value |
KindDuration | Int64() | Stored as nanoseconds |
KindTime | String() | Formatted as string |
KindAny (error) | Err() | Error types get special handling |
KindAny (other) | Any() | Generic type handling |
Performance considerations
- The handler creates minimal allocations by reusing field slices
- Attribute conversion is optimized for common types
- Velo’s zero-allocation JSON encoding applies to slog records
- Using async mode with Velo provides additional performance benefits
Limitations
- The
context.Contextparameter is currently unused - Group names are concatenated with dots; custom separators are not supported
- Some slog features (like
LogValuer) may have different performance characteristics
Related
- Logger - Create and configure Velo loggers
- Fields - Understanding Velo’s field types
- Async logging - Enable async mode for maximum performance