LoggerJSON Logging
Structured JSON Logging
The logger writes structured log entries as JSON, making logs easy to parse, search, and process in production environments.
Each log entry is stored as a single JSON object containing standardized fields such as timestamp, level, and message.
Example log entry:
{
"time": "2026-05-11T11:08:42.135294+02:00",
"level": "INFO",
"msg": "server started"
}JSON logging is enabled automatically when file logging is active.
package main
import (
"log/slog"
"github.com/netlifeguru/logger"
)
func main() {
closer, err := logger.Init(logger.Config{
Dir: "./logs",
MinLevel: slog.LevelInfo,
})
if err != nil {
slog.Error(err.Error())
}
defer closer.Close()
slog.Info("server started")
slog.Warn("high memory usage")
slog.Error("database connection failed")
}Generated log file:
logs/2026-05-11-0001.logStructured JSON logs are useful for:
- centralized log aggregation
- observability platforms
- production monitoring
- analytics pipelines
- machine parsing and indexing
Additional structured fields can be attached directly through the standard slog API:
slog.Info("user authenticated",
slog.Int("user_id", 42),
slog.String("role", "admin"),
)File output:
{
"time": "2026-05-11T12:23:38.592038+02:00",
"level": "INFO",
"msg": "user authenticated",
"user_id": 42,
"role": "admin"
}Terminal output
2026-05-11 12:23:38 [INFO] user authenticated user_id=42 role=admin