LoggerStructured Logging
Structured Chaining
The logger supports reusable structured loggers through `logger.With(...)` and the standard `slog` attribute system.
Structured chaining allows applications to attach shared metadata once and automatically include it in all subsequent log entries produced by that logger instance.
This is useful for:
- request-scoped logging
- service-specific loggers
- worker identifiers
- module separation
- tenant or user tracking
Example usage:
package main
import (
"log/slog"
"github.com/netlifeguru/logger"
)
func main() {
closer, err := logger.Init(logger.Config{
TerminalOutput: true,
})
if err != nil {
slog.Error(err.Error())
}
defer closer.Close()
apiLogger := slog.With(
slog.String("service", "api"),
slog.String("version", "v1"),
)
apiLogger.Info("server started")
userLogger := apiLogger.With(
slog.Int("user_id", 42),
)
userLogger.Info("user authenticated")
}Example output:
2026-05-11 12:51:26 [INFO] server started service=api version=v1
2026-05-11 12:51:26 [INFO] user authenticated service=api version=v1 user_id=42Example file output:
{
"time": "2026-05-11T12:52:05.952406+02:00",
"level": "INFO",
"msg": "server started",
"service": "api",
"version": "v1"
}
{
"time": "2026-05-11T12:52:05.953044+02:00",
"level": "INFO",
"msg": "user authenticated",
"service": "api",
"version": "v1",
"user_id": 42
}Attributes added through chained loggers are inherited automatically, making it easy to build hierarchical structured logging pipelines without repeating common metadata.