Documentation
¶
Overview ¶
Package log provides structured logging functionality with context support.
Index ¶
- Constants
- Variables
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func New(w io.Writer, loggerType string, level slog.Level, contextKeys map[string]any) *slog.Logger
- func SetDefault(l logger)
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- type DefaultSampler
- type Event
- func (e *Event) AddAttrs(attrs map[string]any)
- func (e *Event) AddError(err error)
- func (e *Event) AddStep(level slog.Level, name string)
- func (e *Event) Attr(key string) (any, bool)
- func (e *Event) Duration() time.Duration
- func (e *Event) Finish()
- func (e *Event) HasErrors() bool
- func (e *Event) Level() slog.Level
- func (e *Event) Name() string
- func (e *Event) SetLevel(level slog.Level)
- func (e *Event) ToAttrs() []slog.Attr
- type Sampler
- type SamplerFunc
- type TraceIDMiddleware
- type WideEventLogger
- type WideEventMiddleware
Constants ¶
const ( // DomainNameKey is the context key for domain name. DomainNameKey contextKey = "domainName" // TraceIDKey is the context key for trace ID. TraceIDKey contextKey = "traceId" // ServiceNameKey is the context key for service name. ServiceNameKey contextKey = "serviceName" // StartupTaskKey is the context key for startup task. StartupTaskKey contextKey = "startupTask" // UserIDKey is the context key for user ID. UserIDKey contextKey = "userId" // WorkerIDKey is the context key worker of queue processor. WorkerIDKey contextKey = "workerId" )
const (
// WideEventKey is the default context key for request-wide events.
WideEventKey contextKey = "wideEvent"
)
Variables ¶
var Logger logger = New(os.Stdout, "text", slog.LevelInfo, nil) //nolint:gochecknoglobals
Logger is the default logger instance used by package-level logging functions.
Functions ¶
func DebugContext ¶
DebugContext logs a message at Debug level with context.
func ErrorContext ¶
ErrorContext logs a message at Error level with context.
func InfoContext ¶
InfoContext logs a message at Info level with context.
func New ¶
New creates a new slog.Logger with the specified type (json/text), log level, and additional context keys to include.
func SetDefault ¶
func SetDefault(l logger)
SetDefault sets the default logger used by the package-level logging functions.
Types ¶
type DefaultSampler ¶
type DefaultSampler struct {
// contains filtered or unexported fields
}
DefaultSampler samples by error, duration, status code, and random keep rate.
func NewDefaultSampler ¶
func NewDefaultSampler(slowThreshold time.Duration, keepHTTPStatusAtLeast int, randomKeepRate float64) *DefaultSampler
NewDefaultSampler creates a rule-based sampler.
func (*DefaultSampler) ShouldSample ¶
func (s *DefaultSampler) ShouldSample(_ context.Context, e *Event) bool
ShouldSample decides if event should be logged.
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event is a mutable wide event.
func EventFromContext ¶
EventFromContext returns a wide event from context when present.
type SamplerFunc ¶
SamplerFunc is a function adapter for Sampler.
func (SamplerFunc) ShouldSample ¶
func (f SamplerFunc) ShouldSample(ctx context.Context, e *Event) bool
ShouldSample implements Sampler.
type TraceIDMiddleware ¶
type TraceIDMiddleware struct {
// contains filtered or unexported fields
}
TraceIDMiddleware adds a trace ID to request context and response headers.
func NewTraceIDMiddleware ¶
func NewTraceIDMiddleware(contextKey any, header string) *TraceIDMiddleware
NewTraceIDMiddleware returns a new TraceID middleware. If key is nil, TraceIDKey is used. If header is empty, "Platforma-Trace-Id" is used.
type WideEventLogger ¶
type WideEventLogger struct {
// contains filtered or unexported fields
}
WideEventLogger writes wide events with tail sampling.
func NewWideEventLogger ¶
func NewWideEventLogger(w io.Writer, s Sampler, loggerType string, contextKeys map[string]any) *WideEventLogger
NewWideEventLogger creates a wide-event logger.
func (*WideEventLogger) WriteEvent ¶
func (l *WideEventLogger) WriteEvent(ctx context.Context, e *Event)
WriteEvent finalizes event duration and conditionally writes it.
type WideEventMiddleware ¶
type WideEventMiddleware struct {
// contains filtered or unexported fields
}
WideEventMiddleware creates and writes a request-wide event.
func NewWideEventMiddleware ¶
func NewWideEventMiddleware(logger *WideEventLogger, eventName string, contextKey any) *WideEventMiddleware
NewWideEventMiddleware creates middleware that stores a wide event in request context and writes it after request processing.