Documentation
¶
Index ¶
- Constants
- func CloseLogFiles() error
- func Configure(cfg *Config) error
- func Debug(ctx context.Context, msg string, fields ...ContextFields)
- func Debugf(ctx context.Context, msg string, args ...any)
- func Error(ctx context.Context, err error, msg string, fields ...ContextFields)
- func Errorf(ctx context.Context, err error, msg string, args ...any)
- func Fatal(ctx context.Context, err error, msg string, fields ...ContextFields)
- func Fatalf(ctx context.Context, err error, msg string, args ...any)
- func Info(ctx context.Context, msg string, fields ...ContextFields)
- func Infof(ctx context.Context, msg string, args ...any)
- func NewTraceID() string
- func Warn(ctx context.Context, msg string, fields ...ContextFields)
- func Warnf(ctx context.Context, msg string, args ...any)
- func WithField(ctx context.Context, key string, value interface{}) context.Context
- func WithFields(ctx context.Context, fields ContextFields) context.Context
- type Config
- type ContextFields
- type KV
- type Logger
- func (l *Logger) Debug(msg string, fields ...map[string]interface{})
- func (l *Logger) Debugf(msg string, fields ...any)
- func (l *Logger) Error(err error, msg string, fields ...map[string]interface{})
- func (l *Logger) Errorf(err error, msg string, fields ...any)
- func (l *Logger) Fatal(err error, msg string, fields ...map[string]interface{})
- func (l *Logger) Fatalf(err error, msg string, fields ...any)
- func (l *Logger) GetTraceID() string
- func (l *Logger) GetZerolog() zerolog.Logger
- func (l *Logger) Hostname() string
- func (l *Logger) Info(msg string, fields ...map[string]interface{})
- func (l *Logger) Infof(msg string, fields ...any)
- func (l *Logger) ModuleInfo() string
- func (l *Logger) Warn(msg string, fields ...map[string]interface{})
- func (l *Logger) Warnf(msg string, fields ...any)
- func (l *Logger) WithContext(ctx context.Context) context.Context
- func (l *Logger) WithField(key string, value interface{}) *Logger
- func (l *Logger) WithOutput(output io.Writer) *Logger
- func (l *Logger) WithTraceID(traceID string) *Logger
Constants ¶
const ( // LogContextKey is used to store/retrieve logger from context LogContextKey = "logger" LogTraceIDKey = "trace_id" LogModuleKey = "module" LogComponentKey = "component" LogTimestampFormat = time.RFC3339Nano LogCallerSkipFrames = 2 DefaultLogFile = "application.log" LogFmtPretty = "pretty" LogFmtJson = "json" )
Configuration constants
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
func Debug(ctx context.Context, msg string, fields ...ContextFields)
Debug logs a debug message with the logger from the context
func Error ¶
func Error(ctx context.Context, err error, msg string, fields ...ContextFields)
Error logs an error message with the logger from the context
func Fatal ¶
func Fatal(ctx context.Context, err error, msg string, fields ...ContextFields)
Fatal logs a fatal message with the logger from the context
func Info ¶
func Info(ctx context.Context, msg string, fields ...ContextFields)
Info logs an info message with the logger from the context
func NewTraceID ¶
func NewTraceID() string
NewTraceID generates a new trace ID for distributed tracing
func Warn ¶
func Warn(ctx context.Context, msg string, fields ...ContextFields)
Warn logs a warning message with the logger from the context
func WithField ¶
WithField adds a field to the logger in the context and returns the updated context
func WithFields ¶
func WithFields(ctx context.Context, fields ContextFields) context.Context
WithFields adds multiple fields to the logger in the context and returns the updated context
Types ¶
type Config ¶
type Config struct {
Level string `json:"level"`
Format string `json:"format"` // "pretty" or "json"
IncludeTimestamp bool `json:"includeTimestamp"`
IncludeCaller bool `json:"includeCaller"`
IncludeHostname bool `json:"includeHostname"`
CallerSkipFrames int `json:"callerSkipFrames"`
TimeFormat string `json:"timeFormat"` // Time format string
NoColor bool `json:"noColor"` // if true, disable color
OutputToFile bool `json:"outputToFile"` // Enable file output
FilePath string `json:"filePath"` // Path to log file
FileAppend bool `json:"fileAppend"` // Append to existing file
FilePermissions int `json:"filePermissions"` // File permissions (e.g., 0644)
FileFormat string `json:"fileFormat"` // file format, "pretty" or json
FileRotation bool `json:"fileRotation"` // Enable log rotation
MaxSizeMb int `json:"maxSizeMb"` // Max size in MB before rotation
MaxBackups int `json:"maxBackups"` // Max number of rotated files to keep
MaxAgeDays int `json:"maxAgeDays"` // Max age in days to keep rotated files
Compress bool `json:"compress"` // Compress rotated files
}
Config contains configuration for the logger
func DisableFileAppend ¶
DisableFileAppend disables appending to existing log files (will overwrite)
func EnableFileOutput ¶
EnableFileOutput enables file logging with the given file path
func NewDefaultConfig ¶
func NewDefaultConfig() *Config
NewDefaultConfig returns a default logging configuration
func SetFileFormat ¶
SetFileFormat sets the output format for file logging
func (*Config) Logger ¶ added in v0.4.0
Logger returns a new logger instance based on the configuration
func (*Config) ModuleLogger ¶ added in v0.4.0
ModuleLogger returns a new module logger based on the configuration
type ContextFields ¶
type ContextFields map[string]interface{}
ContextFields is a map of fields to add to log messages
func MergeContextFields ¶
func MergeContextFields(fieldSets ...ContextFields) ContextFields
MergeContextFields merges multiple context fields maps into a single map
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger wraps zerolog.Logger to provide consistent logging patterns
func ExtractLoggerFromContext ¶
ExtractLoggerFromContext extracts a logger from the given context If no logger is found, a new default logger is created
func FromContext ¶
FromContext retrieves a logger from the context If no logger is found, a new default logger is returned
func NewRequestContext ¶
NewRequestContext creates a new context with a logger that has a trace ID This is useful for tracking requests through multiple services
func NewWithComponent ¶
NewWithComponent creates a new logger with module and component information
func (*Logger) Error ¶
Error logs an error message with the given fields It automatically adds stack information
func (*Logger) Errorf ¶
Errorf logs an error message with the given fields It automatically adds stack information
func (*Logger) GetTraceID ¶
GetTraceID returns the trace ID associated with this logger
func (*Logger) GetZerolog ¶
GetZerolog returns the underlying zerolog.Logger
func (*Logger) WithContext ¶
WithContext adds the logger to the context
func (*Logger) WithOutput ¶
WithOutput use a custom output
func (*Logger) WithTraceID ¶
WithTraceID creates a new logger with the specified trace ID