logger

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package logger provides logging functionality with zerolog adapter

Package logger provides filtering capabilities for sensitive data in log output.

Package logger defines the logging interface used throughout the application. It provides a contract for structured logging implementations.

Index

Constants

View Source
const DefaultMaskValue = "***"

DefaultMaskValue es el valor utilizado para enmascarar datos sensibles

View Source
const (
	// DefaultMaxDepth is the default maximum recursion depth for filtering
	DefaultMaxDepth = 8
)

Variables

This section is empty.

Functions

func AddAMQPElapsed

func AddAMQPElapsed(ctx context.Context, nanos int64)

AddAMQPElapsed adds elapsed nanoseconds to the AMQP elapsed time in the context

func AddDBElapsed

func AddDBElapsed(ctx context.Context, nanos int64)

AddDBElapsed adds elapsed nanoseconds to the database elapsed time in the context

func GetAMQPCounter

func GetAMQPCounter(ctx context.Context) int64

GetAMQPCounter returns the current AMQP message count from the context

func GetAMQPElapsed

func GetAMQPElapsed(ctx context.Context) int64

GetAMQPElapsed returns the current AMQP elapsed time in nanoseconds from the context

func GetDBCounter

func GetDBCounter(ctx context.Context) int64

GetDBCounter returns the current database operation count from the context

func GetDBElapsed

func GetDBElapsed(ctx context.Context) int64

GetDBElapsed returns the current database elapsed time in nanoseconds from the context

func IncrementAMQPCounter

func IncrementAMQPCounter(ctx context.Context)

IncrementAMQPCounter increments the AMQP message counter in the context

func IncrementDBCounter

func IncrementDBCounter(ctx context.Context)

IncrementDBCounter increments the database operation counter in the context

func WithAMQPCounter

func WithAMQPCounter(ctx context.Context) context.Context

WithAMQPCounter creates a new context with an AMQP message counter and elapsed time tracker

func WithDBCounter

func WithDBCounter(ctx context.Context) context.Context

WithDBCounter creates a new context with a database operation counter and elapsed time tracker

func WithSeverityHook added in v0.13.0

func WithSeverityHook(ctx context.Context, hook func(zerolog.Level)) context.Context

WithSeverityHook attaches a severity hook to the context. The hook is used by the logging adapter to propagate WARN/ERROR logs back to request middleware for routing.

Types

type FilterConfig

type FilterConfig struct {
	// SensitiveFields contains field names that should be masked in logs
	SensitiveFields []string
	// MaskValue is the value used to replace sensitive data (default: "***")
	MaskValue string
}

FilterConfig defines the configuration for sensitive data filtering

func DefaultFilterConfig

func DefaultFilterConfig() *FilterConfig

DefaultFilterConfig returns a default configuration with common sensitive field names

type LogEvent

type LogEvent interface {
	Msg(msg string)
	Msgf(format string, args ...any)
	Err(err error) LogEvent
	Str(key, value string) LogEvent
	Int(key string, value int) LogEvent
	Int64(key string, value int64) LogEvent
	Uint64(key string, value uint64) LogEvent
	Dur(key string, d time.Duration) LogEvent
	Interface(key string, i any) LogEvent
	Bytes(key string, val []byte) LogEvent
}

LogEvent represents a structured log event that can be built with fields and sent. It provides methods for adding various field types and sending the final log message.

type LogEventAdapter

type LogEventAdapter struct {
	// contains filtered or unexported fields
}

LogEventAdapter adapts zerolog events to our logger interface

func (*LogEventAdapter) Bytes

func (lea *LogEventAdapter) Bytes(key string, val []byte) LogEvent

Bytes adds a byte slice field to the log event

func (*LogEventAdapter) Dur

func (lea *LogEventAdapter) Dur(key string, d time.Duration) LogEvent

Dur adds a duration field to the log event

func (*LogEventAdapter) Err

func (lea *LogEventAdapter) Err(err error) LogEvent

Err adds an error to the log event

func (*LogEventAdapter) Int

func (lea *LogEventAdapter) Int(key string, value int) LogEvent

Int adds an integer field to the log event

func (*LogEventAdapter) Int64

func (lea *LogEventAdapter) Int64(key string, value int64) LogEvent

Int64 adds an int64 field to the log event

func (*LogEventAdapter) Interface

func (lea *LogEventAdapter) Interface(key string, i any) LogEvent

Interface adds an any field to the log event

func (*LogEventAdapter) Msg

func (lea *LogEventAdapter) Msg(msg string)

Msg logs the message

func (*LogEventAdapter) Msgf

func (lea *LogEventAdapter) Msgf(format string, args ...any)

Msgf logs a formatted message

func (*LogEventAdapter) Str

func (lea *LogEventAdapter) Str(key, value string) LogEvent

Str adds a string field to the log event

func (*LogEventAdapter) Uint64

func (lea *LogEventAdapter) Uint64(key string, value uint64) LogEvent

Uint64 adds a uint64 field to the log event

type Logger

type Logger interface {
	Info() LogEvent
	Error() LogEvent
	Debug() LogEvent
	Warn() LogEvent
	Fatal() LogEvent
	WithContext(ctx any) Logger
	WithFields(fields map[string]any) Logger
}

Logger defines the contract for structured logging throughout the application. It provides methods for creating log events at different severity levels and for contextual logging.

type OTelBridge added in v0.13.0

type OTelBridge struct {
	// contains filtered or unexported fields
}

OTelBridge converts zerolog JSON output to OpenTelemetry log records. It implements io.Writer to intercept zerolog's output stream.

func NewOTelBridge added in v0.13.0

func NewOTelBridge(provider *sdklog.LoggerProvider) *OTelBridge

NewOTelBridge creates a new bridge that converts zerolog logs to OTel log records.

func (*OTelBridge) Write added in v0.13.0

func (b *OTelBridge) Write(p []byte) (n int, err error)

Write implements io.Writer by parsing zerolog JSON and emitting OTel log records.

type OTelProvider added in v0.13.0

type OTelProvider interface {
	// LoggerProvider returns the configured logger provider.
	// Returns nil if logging is disabled.
	LoggerProvider() *sdklog.LoggerProvider

	// ShouldDisableStdout returns true if stdout should be disabled when OTLP is enabled.
	// This method is implemented via type assertion to avoid exposing internal config.
	ShouldDisableStdout() bool
}

OTelProvider is a minimal interface for accessing OpenTelemetry logger provider and configuration. This interface allows the logger package to integrate with observability without creating circular dependencies.

type SensitiveDataFilter

type SensitiveDataFilter struct {
	// contains filtered or unexported fields
}

SensitiveDataFilter implements zerolog.Hook to filter sensitive data from logs

func NewSensitiveDataFilter

func NewSensitiveDataFilter(config *FilterConfig) *SensitiveDataFilter

NewSensitiveDataFilter creates a new filter with the given configuration

func (*SensitiveDataFilter) FilterFields

func (f *SensitiveDataFilter) FilterFields(fields map[string]any) map[string]any

FilterFields filters a map of fields for sensitive data

func (*SensitiveDataFilter) FilterString

func (f *SensitiveDataFilter) FilterString(key, value string) string

FilterString filters sensitive data from string values

func (*SensitiveDataFilter) FilterValue

func (f *SensitiveDataFilter) FilterValue(key string, value any) any

FilterValue filters sensitive data from any values

func (*SensitiveDataFilter) Run

Run implements zerolog.Hook interface to filter sensitive data

type ZeroLogger

type ZeroLogger struct {
	// contains filtered or unexported fields
}

ZeroLogger wraps zerolog.Logger to implement the Logger interface. It provides structured logging functionality with configurable output formatting.

func New

func New(level string, pretty bool) *ZeroLogger

New creates a new ZeroLogger instance with the specified log level and formatting options. If pretty is true, output will be formatted for human readability.

func NewWithFilter

func NewWithFilter(level string, pretty bool, filterConfig *FilterConfig) *ZeroLogger

NewWithFilter creates a new ZeroLogger instance with custom filter configuration. This allows applications to customize which fields are considered sensitive.

func (*ZeroLogger) Debug

func (l *ZeroLogger) Debug() LogEvent

Debug creates a debug-level log event

func (*ZeroLogger) Error

func (l *ZeroLogger) Error() LogEvent

func (*ZeroLogger) Fatal

func (l *ZeroLogger) Fatal() LogEvent

Fatal creates a fatal-level log event

func (*ZeroLogger) Info

func (l *ZeroLogger) Info() LogEvent

Info creates an info-level log event

func (*ZeroLogger) Warn

func (l *ZeroLogger) Warn() LogEvent

Warn creates a warning-level log event

func (*ZeroLogger) WithContext

func (l *ZeroLogger) WithContext(ctx any) Logger

WithContext returns a logger with context information attached. It follows a two-phase approach for maximum flexibility:

Phase 1 (Explicit): If the context contains an explicit zerolog logger (set via zerolog.Ctx), that logger takes precedence. This maintains backward compatibility with existing code that uses zerolog's context pattern.

Phase 2 (Automatic): If no explicit logger is found, automatically extracts trace_id and span_id from the OpenTelemetry span context and adds them as fields. This enables automatic trace correlation without requiring explicit logger management in every handler.

This hybrid approach provides:

  • Deterministic behavior: same context always produces same logger
  • Backward compatibility: existing zerolog.Ctx usage continues to work
  • Automatic correlation: trace IDs appear in logs without boilerplate

func (*ZeroLogger) WithFields

func (l *ZeroLogger) WithFields(fields map[string]any) Logger

WithFields returns a logger with additional fields attached to all log entries.

func (*ZeroLogger) WithOTelProvider added in v0.13.0

func (l *ZeroLogger) WithOTelProvider(provider OTelProvider) *ZeroLogger

WithOTelProvider attaches an OpenTelemetry logger provider for OTLP log export. Returns the same logger if provider is nil/disabled, or creates a new logger with dual output (stdout + OTLP) or OTLP-only based on the provider's configuration.

IMPORTANT: OTLP export requires JSON mode (pretty=false). This method fails fast with a panic if pretty mode is active, ensuring configuration errors are caught during initialization rather than silently degrading observability.

Configuration conflict detection:

  • If logger is created with pretty=true AND OTLP export is enabled, panics with clear error message directing user to fix their configuration.

Output modes:

  • DisableStdout=false (default): logs go to both stdout and OTLP (useful for dev)
  • DisableStdout=true: logs only go to OTLP (production efficiency)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL