logging

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package logging provides structured logging with zerolog for trace context propagation. It supports configurable log levels, output formats (JSON/console), and automatic extraction of trace/span IDs from context for distributed tracing correlation.

Example usage:

cfg := config.LogConfig{
    Level:  "info",
    Format: "json",
    Output: "stdout",
}
logger := logging.New(cfg)
logger.Info().Str("user_id", "123").Msg("user logged in")

Index

Constants

View Source
const (
	// TraceID is the field name for distributed trace ID (W3C trace context).
	TraceID = "trace_id"

	// SpanID is the field name for current span ID within a trace.
	SpanID = "span_id"

	// ServiceName is the field name for the service generating the log.
	ServiceName = "service_name"

	// Timestamp is the field name for when the log was created.
	Timestamp = "timestamp"

	// Level is the field name for log level (debug, info, warn, error).
	Level = "level"

	// Message is the field name for the log message.
	Message = "message"

	// Error is the field name for error information.
	Error = "error"

	// RequestID is the field name for HTTP request ID.
	RequestID = "request_id"

	// Method is the field name for HTTP method.
	Method = "method"

	// Path is the field name for HTTP path.
	Path = "path"

	// StatusCode is the field name for HTTP status code.
	StatusCode = "status_code"

	// Duration is the field name for operation duration.
	Duration = "duration_ms"

	// UserID is the field name for authenticated user ID.
	UserID = "user_id"

	// Component is the field name for the component/package generating the log.
	Component = "component"
)

Standard field names for structured logging. These constants ensure consistent field naming across all services.

Variables

This section is empty.

Functions

func Ctx

func Ctx(ctx context.Context) *zerolog.Logger

Ctx returns a zerolog.Context that can be used to add fields to a log entry. This is a convenience function for getting a context-aware logger.

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID retrieves the request ID from the context.

func GetSpanID

func GetSpanID(ctx context.Context) string

GetSpanID retrieves the span ID from the context.

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID retrieves the trace ID from the context.

func HTTPMiddleware

func HTTPMiddleware(logger *Logger) func(http.Handler) http.Handler

HTTPMiddleware is an HTTP middleware that logs request and response details. It automatically generates a request ID if one doesn't exist and logs: - Request start (method, path, request_id) - Request end (method, path, status, duration, request_id)

func StreamServerInterceptor

func StreamServerInterceptor(logger *Logger) grpc.StreamServerInterceptor

StreamServerInterceptor returns a gRPC stream server interceptor that logs stream operations.

func UnaryServerInterceptor

func UnaryServerInterceptor(logger *Logger) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a gRPC unary server interceptor that logs RPC calls. It logs: - RPC start (method) - RPC end (method, status, duration)

func WithLogger

func WithLogger(ctx context.Context, logger *Logger) context.Context

WithLogger adds a logger to the context.

func WithRequestID

func WithRequestID(ctx context.Context, requestID string) context.Context

WithRequestID adds a request ID to the context.

func WithSpanID

func WithSpanID(ctx context.Context, spanID string) context.Context

WithSpanID adds a span ID to the context.

func WithTraceContext

func WithTraceContext(ctx context.Context, traceID, spanID string) context.Context

WithTraceContext adds both trace and span IDs to the context. This is a convenience function for adding trace context in one call.

func WithTraceID

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID adds a trace ID to the context.

Types

type Logger

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

Logger provides structured logging with trace context support. It wraps zerolog.Logger to provide a consistent interface across the CQI infrastructure.

func FromContext

func FromContext(ctx context.Context) *Logger

FromContext extracts a logger from the context. If no logger is found, it returns a default logger.

func New

func New(cfg config.LogConfig) *Logger

New creates a new Logger instance from the provided configuration. It configures the log level, output format (JSON/console), and output destination.

func (*Logger) Debug

func (l *Logger) Debug() *zerolog.Event

Debug returns a debug level event.

func (*Logger) Error

func (l *Logger) Error() *zerolog.Event

Error returns an error level event.

func (*Logger) Fatal

func (l *Logger) Fatal() *zerolog.Event

Fatal returns a fatal level event. The application will exit with status 1 after logging the event.

func (*Logger) GetZerolog

func (l *Logger) GetZerolog() *zerolog.Logger

GetZerolog returns the underlying zerolog.Logger for advanced use cases.

func (*Logger) Info

func (l *Logger) Info() *zerolog.Event

Info returns an info level event.

func (*Logger) Level

func (l *Logger) Level() zerolog.Level

Level returns the current log level.

func (*Logger) Panic

func (l *Logger) Panic() *zerolog.Event

Panic returns a panic level event. The application will panic after logging the event.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level zerolog.Level)

SetLevel changes the log level.

func (*Logger) Warn

func (l *Logger) Warn() *zerolog.Event

Warn returns a warning level event.

func (*Logger) With

func (l *Logger) With() zerolog.Context

With returns a logger with additional context fields.

func (*Logger) WithComponent

func (l *Logger) WithComponent(component string) *Logger

WithComponent returns a new logger with a component field set. This is useful for identifying which package/component generated the log.

func (*Logger) WithFields

func (l *Logger) WithFields(fields map[string]interface{}) *Logger

WithFields returns a new logger with multiple fields set.

func (*Logger) WithServiceName

func (l *Logger) WithServiceName(serviceName string) *Logger

WithServiceName returns a new logger with the service name field set.

Jump to

Keyboard shortcuts

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