Logging
Logging
Overview
Lemmego provides a colorized structured logger built on Go’s log/slog package. It outputs JSON-formatted logs with color-coded levels for terminal readability.
Basic Usage
import "github.com/lemmego/api/logger"
// Get the default logger
log := logger.Default()
log.Info("server started", "port", 8080)
log.Warn("slow request", "duration", "2.5s")
log.Error("database connection failed", "error", err)Log Levels
The logger supports standard slog levels with color coding:
| Level | Color | Method |
|---|---|---|
| DEBUG | Green | log.Debug(msg, args...) |
| INFO | Cyan | log.Info(msg, args...) |
| WARN | Yellow | log.Warn(msg, args...) |
| ERROR | Red | log.Error(msg, args...) |
The minimum log level is controlled by APP_DEBUG:
APP_DEBUG=true # Shows DEBUG and above
APP_DEBUG=false # Shows INFO and aboveVerbose Logging
log := logger.Verbose()
// Includes source file and line number in log outputCustom Handler
handler := logger.NewHandler(&logger.Options{
Level: slog.LevelDebug,
})
slog.SetDefault(slog.New(handler))Observability Logger
For more advanced logging needs, the observability package provides a structured logger with rotation and field enrichment:
import "github.com/lemmego/api/observability"
logger, err := observability.NewLogger(observability.LoggerConfig{
Level: observability.LevelInfo,
Output: "stdout",
JSONOutput: true,
})
logger.Info(ctx, "user registered", map[string]any{
"user_id": user.ID,
"email": user.Email,
})
// Enriched loggers
logger.WithRequestID(reqID).Info(ctx, "processing request")
logger.WithUserID(userID).Warn(ctx, "rate limit approaching")Metrics
The observability package also includes a metrics collector:
metrics := observability.NewMetricsCollector(observability.MetricsConfig{
Enabled: true,
})
// Record HTTP request metrics
observability.RecordHTTPRequest("GET", "/api/users", 200, 45*time.Millisecond)
// Custom metrics
metrics.IncrementCounter("page_views", 1)
metrics.SetGauge("active_users", 42)
metrics.RecordHistogram("response_time_ms", 145)Metrics middleware automatically records HTTP request metrics:
app.Use(observability.MetricsMiddleware)