Documentation
¶
Overview ¶
Package log provides structured logging with correlation IDs.
Index ¶
- func CorrelationID(ctx context.Context) string
- func RequestID(ctx context.Context) string
- func SetDefaultLogger(l *Logger)
- func WithCorrelationID(ctx context.Context, id string) context.Context
- func WithRequestID(ctx context.Context, id string) context.Context
- type ContextKey
- type Logger
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Handler() slog.Handler
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) SetDefault()
- func (l *Logger) Slog() *slog.Logger
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) With(args ...any) *Logger
- func (l *Logger) WithContext(ctx context.Context) *Logger
- type TerminalHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CorrelationID ¶
CorrelationID extracts the correlation ID from context.
func SetDefaultLogger ¶
func SetDefaultLogger(l *Logger)
SetDefaultLogger sets the package-level default logger.
func WithCorrelationID ¶
WithCorrelationID adds a correlation ID to the context.
Types ¶
type ContextKey ¶
type ContextKey string
ContextKey is a type for context keys to avoid collisions.
const ( CorrelationIDKey ContextKey = "correlation_id" RequestIDKey ContextKey = "request_id" )
Context keys for logging.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger wraps slog.Logger with convenience methods.
func NewLoggerWithWriter ¶
NewLoggerWithWriter creates a Logger that writes to the specified writer.
func (*Logger) DebugContext ¶
DebugContext logs at debug level with context.
func (*Logger) ErrorContext ¶
ErrorContext logs at error level with context.
func (*Logger) InfoContext ¶
InfoContext logs at info level with context.
func (*Logger) SetDefault ¶
func (l *Logger) SetDefault()
SetDefault sets the global default slog logger.
func (*Logger) WarnContext ¶
WarnContext logs at warn level with context.
type TerminalHandler ¶
type TerminalHandler struct {
// contains filtered or unexported fields
}
TerminalHandler formats log records as coloured terminal output.
Output format:
15:04:05.000 INF server started port=8080
func (*TerminalHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level.
func (*TerminalHandler) Handle ¶
Handle formats a log record as coloured terminal output and writes it.