LoggerStructured Logging
Context Logging
The logger supports context-aware logging through the standard `context.Context` integration provided by `log/slog`.
Context logging is useful for propagating request-scoped or operation-scoped information across HTTP handlers, background workers, database operations, and distributed services.
Supported methods include:
- InfoContext: Logs informational messages with context propagation
- WarnContext: Logs warning messages related to recoverable or unexpected situations
- ErrorContext: Logs errors and failure-related events with contextual information
- DebugContext: Logs detailed debugging information useful during development
- NoticeContext: Logs important operational events intended to remain highly visible
Example usage:
package main
import (
"context"
"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()
ctx := context.Background()
slog.InfoContext(ctx, "request started")
slog.WarnContext(ctx, "slow database query")
logger.NoticeContext(ctx, "background sync completed")
}Example output:
{
"time": "2026-05-11T12:50:26.854582+02:00",
"level": "INFO",
"msg": "request started"
}
{
"time": "2026-05-11T12:50:26.855051+02:00",
"level": "WARN",
"msg": "slow database query"
}Context-aware logging helps:
- propagate request lifecycle information
- correlate logs across operations
- integrate with tracing systems
- support middleware-driven architectures
- improve observability in concurrent applications
Because the logger is fully compatible with log/slog, existing slog context-based workflows continue to work without modification.