Documentation
¶
Overview ¶
Package logging provides centralized logging functionality for services.
This package wraps the zap logging library and adds features like trace ID extraction from context and context-aware logging methods. It is designed to be used throughout the application to provide consistent, structured logging with proper context information.
Key features:
- Context-aware logging with automatic trace ID extraction
- Support for different log levels (Debug, Info, Warn, Error, Fatal)
- Configurable for both development (console output) and production (JSON output) environments
- Integration with OpenTelemetry for distributed tracing
- Structured logging with additional fields
The package provides two main components:
- ContextLogger: A wrapper around zap.Logger that automatically extracts trace information from context and includes it in log entries
- Logger interface: An abstraction that can be implemented by different logging backends
Example usage:
// Create a new logger
zapLogger, err := logging.NewLogger("info", true)
if err != nil {
panic(err)
}
defer zapLogger.Sync()
// Create a context logger
logger := logging.NewContextLogger(zapLogger)
// Log with context
ctx := context.Background()
logger.Info(ctx, "Application started", zap.String("app_name", "example"))
// Log an error
if err := someOperation(); err != nil {
logger.Error(ctx, "Operation failed", zap.Error(err))
}
The package is designed to be used as a dependency by other packages in the application, providing a consistent logging interface throughout the codebase.
Package logging provides centralized logging functionality for services.
Package logging provides centralized logging functionality for services. It wraps the zap logging library and adds features like trace ID extraction from context and context-aware logging methods. This package is part of the infrastructure layer and provides logging capabilities to all other layers of the application.
Index ¶
- func NewLogger(level string, development bool) (*zap.Logger, error)
- func WithTraceID(ctx context.Context, logger *zap.Logger) *zap.Logger
- type ContextLogger
- func (l *ContextLogger) Debug(ctx context.Context, msg string, fields ...zap.Field)
- func (l *ContextLogger) Error(ctx context.Context, msg string, fields ...zap.Field)
- func (l *ContextLogger) Fatal(ctx context.Context, msg string, fields ...zap.Field)
- func (l *ContextLogger) Info(ctx context.Context, msg string, fields ...zap.Field)
- func (l *ContextLogger) Sync() error
- func (l *ContextLogger) Warn(ctx context.Context, msg string, fields ...zap.Field)
- func (l *ContextLogger) With(ctx context.Context) *zap.Logger
- type Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewLogger ¶
NewLogger creates a new zap logger configured based on the provided level and environment. It sets up appropriate encoders and log levels for either development or production use. Parameters:
- level: The minimum log level as a string (e.g., "debug", "info", "warn", "error")
- development: Whether to use development mode with console output (true) or production mode with JSON output (false)
Returns:
- *zap.Logger: A configured zap logger instance
- error: An error if logger creation fails
func WithTraceID ¶
WithTraceID adds trace ID and span ID to the logger from the provided context. This enables correlation between logs and traces for distributed tracing. Parameters:
- ctx: The context containing trace information
- logger: The base logger to enhance with trace information
Returns:
- *zap.Logger: A new logger with trace ID and span ID fields added if available
Types ¶
type ContextLogger ¶
type ContextLogger struct {
// contains filtered or unexported fields
}
ContextLogger is a logger that includes context information in log entries. It wraps a zap.Logger and provides methods that accept a context parameter, automatically extracting and including trace information in log entries.
func NewContextLogger ¶
func NewContextLogger(base *zap.Logger) *ContextLogger
NewContextLogger creates a new context-aware logger wrapping the provided base logger. If base is nil, a no-op logger will be used to prevent nil pointer panics. Parameters:
- base: The base zap logger to wrap
Returns:
- *ContextLogger: A new context logger instance
func (*ContextLogger) Debug ¶
Debug logs a debug-level message with context information. Parameters:
- ctx: The context containing trace information
- msg: The message to log
- fields: Additional fields to include in the log entry
func (*ContextLogger) Error ¶
Error logs an error-level message with context information. Parameters:
- ctx: The context containing trace information
- msg: The message to log
- fields: Additional fields to include in the log entry
func (*ContextLogger) Fatal ¶
Fatal logs a fatal-level message with context information. This will terminate the program after logging the message. Parameters:
- ctx: The context containing trace information
- msg: The message to log
- fields: Additional fields to include in the log entry
func (*ContextLogger) Info ¶
Info logs an info-level message with context information. Parameters:
- ctx: The context containing trace information
- msg: The message to log
- fields: Additional fields to include in the log entry
func (*ContextLogger) Sync ¶
func (l *ContextLogger) Sync() error
Sync flushes any buffered log entries to their destination. This should be called before program termination to ensure all logs are written. Returns:
- error: An error if flushing fails
func (*ContextLogger) Warn ¶
Warn logs a warning-level message with context information. Parameters:
- ctx: The context containing trace information
- msg: The message to log
- fields: Additional fields to include in the log entry
func (*ContextLogger) With ¶
func (l *ContextLogger) With(ctx context.Context) *zap.Logger
With returns a logger with trace information from the given context. Parameters:
- ctx: The context containing trace information
Returns:
- *zap.Logger: A logger with trace ID and span ID fields added if available
type Logger ¶
type Logger = interfaces.Logger
Logger is an alias for interfaces.Logger for backward compatibility
Directories
¶
| Path | Synopsis |
|---|---|
|
Package interfaces defines the interfaces for the logging package.
|
Package interfaces defines the interfaces for the logging package. |
|
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |