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
- func Ctx(ctx context.Context) *zerolog.Logger
- func GetRequestID(ctx context.Context) string
- func GetSpanID(ctx context.Context) string
- func GetTraceID(ctx context.Context) string
- func HTTPMiddleware(logger *Logger) func(http.Handler) http.Handler
- func StreamServerInterceptor(logger *Logger) grpc.StreamServerInterceptor
- func UnaryServerInterceptor(logger *Logger) grpc.UnaryServerInterceptor
- func WithLogger(ctx context.Context, logger *Logger) context.Context
- func WithRequestID(ctx context.Context, requestID string) context.Context
- func WithSpanID(ctx context.Context, spanID string) context.Context
- func WithTraceContext(ctx context.Context, traceID, spanID string) context.Context
- func WithTraceID(ctx context.Context, traceID string) context.Context
- type Logger
- func (l *Logger) Debug() *zerolog.Event
- func (l *Logger) Error() *zerolog.Event
- func (l *Logger) Fatal() *zerolog.Event
- func (l *Logger) GetZerolog() *zerolog.Logger
- func (l *Logger) Info() *zerolog.Event
- func (l *Logger) Level() zerolog.Level
- func (l *Logger) Panic() *zerolog.Event
- func (l *Logger) SetLevel(level zerolog.Level)
- func (l *Logger) Warn() *zerolog.Event
- func (l *Logger) With() zerolog.Context
- func (l *Logger) WithComponent(component string) *Logger
- func (l *Logger) WithFields(fields map[string]interface{}) *Logger
- func (l *Logger) WithServiceName(serviceName string) *Logger
Constants ¶
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 ¶
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 ¶
GetRequestID retrieves the request ID from the context.
func GetTraceID ¶
GetTraceID retrieves the trace ID from the context.
func HTTPMiddleware ¶
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 ¶
WithLogger adds a logger to the context.
func WithRequestID ¶
WithRequestID adds a request ID to the context.
func WithSpanID ¶
WithSpanID adds a span ID to the context.
func WithTraceContext ¶
WithTraceContext adds both trace and span IDs to the context. This is a convenience function for adding trace context in one call.
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 ¶
FromContext extracts a logger from the context. If no logger is found, it returns a default logger.
func New ¶
New creates a new Logger instance from the provided configuration. It configures the log level, output format (JSON/console), and output destination.
func (*Logger) Fatal ¶
Fatal returns a fatal level event. The application will exit with status 1 after logging the event.
func (*Logger) GetZerolog ¶
GetZerolog returns the underlying zerolog.Logger for advanced use cases.
func (*Logger) Panic ¶
Panic returns a panic level event. The application will panic after logging the event.
func (*Logger) WithComponent ¶
WithComponent returns a new logger with a component field set. This is useful for identifying which package/component generated the log.
func (*Logger) WithFields ¶
WithFields returns a new logger with multiple fields set.
func (*Logger) WithServiceName ¶
WithServiceName returns a new logger with the service name field set.