Documentation
¶
Index ¶
- Variables
- func FixedWidthCapitalColorLevelEncoder(cfg *Config) zapcore.LevelEncoder
- func FixedWidthCapitalLevelEncoder(cfg *Config) zapcore.LevelEncoder
- type Config
- type LevelFormat
- type LogFormat
- type Logger
- func (l *Logger) ClearFields()
- func (l *Logger) Config() *Config
- func (l *Logger) ConsoleLogger() *zap.SugaredLogger
- func (l *Logger) Debug(msg string, fields ...any)
- func (l *Logger) Error(msg string, fields ...any)
- func (l *Logger) Fields() []zap.Field
- func (l *Logger) FileLogger() *zap.SugaredLogger
- func (l *Logger) Info(msg string, fields ...any)
- func (l *Logger) Level() zapcore.Level
- func (l *Logger) LumberjackLogger() *lumberjack.Logger
- func (l *Logger) ServiceName() string
- func (l *Logger) SetLogLevel(level string) error
- func (l *Logger) Warn(msg string, fields ...any)
- func (l *Logger) WithContext(ctx context.Context) *Logger
- func (l *Logger) WithFields(fields ...any) *Logger
- func (l *Logger) WithService(serviceName string, options ...zap.Option) *Logger
- type Option
- func WithCallerSkip(skip int) Option
- func WithConsoleEncoder(encoder zapcore.Encoder) Option
- func WithCustomField(key string, value any) Option
- func WithCustomFields(fields ...any) Option
- func WithCustomLevelFormats(formats map[zapcore.Level]LevelFormat) Option
- func WithFileEncoder(encoder zapcore.Encoder) Option
- func WithServiceName(name string) Option
Constants ¶
This section is empty.
Variables ¶
var ( DefaultLevelFormats = map[zapcore.Level]LevelFormat{ zapcore.DebugLevel: {LevelStr: "DEBUG", Color: "\033[34m"}, zapcore.InfoLevel: {LevelStr: "INFO ", Color: "\033[36m"}, zapcore.WarnLevel: {LevelStr: "WARN ", Color: "\033[33m"}, zapcore.ErrorLevel: {LevelStr: "ERROR", Color: "\033[31m"}, } // DefaultConsoleEncoderConfig defines the default encoder configuration for console output. // It uses ISO8601 for time encoding, a fixed-width colored encoder for log levels, // and a short caller encoder to show the file and line number. DefaultConsoleEncoderConfig = zapcore.EncoderConfig{ TimeKey: "timestamp", LevelKey: "level", MessageKey: "message", EncodeTime: zapcore.ISO8601TimeEncoder, EncodeCaller: zapcore.ShortCallerEncoder, } // DefaultTextEncoderConfig defines the default encoder configuration for plain text log output. // It uses ISO8601 for time encoding, a fixed-width log level encoder (without color), // and a short caller encoder. DefaultTextEncoderConfig = zapcore.EncoderConfig{ TimeKey: "timestamp", LevelKey: "level", MessageKey: "message", EncodeTime: zapcore.ISO8601TimeEncoder, EncodeCaller: zapcore.ShortCallerEncoder, } // DefaultJSONEncoderConfig defines the default encoder configuration for JSON log output. // It uses ISO8601 for time encoding, a capitalized log level encoder, // and a short caller encoder to include caller information. DefaultJSONEncoderConfig = zapcore.EncoderConfig{ TimeKey: "timestamp", LevelKey: "level", MessageKey: "message", EncodeTime: zapcore.ISO8601TimeEncoder, EncodeLevel: zapcore.CapitalLevelEncoder, EncodeCaller: zapcore.ShortCallerEncoder, } // DefaultLoggerConfig provides the default configuration for a dslogger.Logger instance. // It sets default values for the log file path, file format, rotation policies, // encoder configurations for both console and file outputs, and other display options. DefaultLoggerConfig = Config{ LogFile: "app.log", LogFileFormat: LogFormatText, MaxSize: 10, MaxBackups: 5, MaxAge: 28, Compress: true, Level: "info", ConsoleConfig: DefaultConsoleEncoderConfig, FileConfig: DefaultTextEncoderConfig, FieldSeparator: ": ", ConsoleSeparator: " | ", ServiceNameDecorators: [2]string{"[", "]"}, LevelFormats: DefaultLevelFormats, } )
Functions ¶
func FixedWidthCapitalColorLevelEncoder ¶ added in v0.2.1
func FixedWidthCapitalColorLevelEncoder(cfg *Config) zapcore.LevelEncoder
FixedWidthCapitalColorLevelEncoder encodes the log level as a colored, fixed-width string. It uses ANSI escape codes to colorize the output.
func FixedWidthCapitalLevelEncoder ¶ added in v0.2.1
func FixedWidthCapitalLevelEncoder(cfg *Config) zapcore.LevelEncoder
FixedWidthCapitalLevelEncoder encodes the log level as a fixed-width string. This is used to ensure consistent alignment in console output.
Types ¶
type Config ¶
type Config struct {
LogFile string
LogFileFormat LogFormat
MaxSize int
MaxBackups int
MaxAge int
Compress bool
Level string
ConsoleConfig zapcore.EncoderConfig
FileConfig zapcore.EncoderConfig
ConsoleSeparator string
FieldSeparator string
ServiceNameDecorators [2]string
LevelFormats map[zapcore.Level]LevelFormat
}
Config holds configuration options for the Logger.
type LevelFormat ¶ added in v0.2.0
type LevelFormat struct {
LevelStr string // e.g. "DEBUG", "INFO", etc.
Color string // e.g. "\033[34m" for blue; empty string if no color.
}
LevelFormat holds the fixed-width text representation for a log level, as well as an optional ANSI color escape sequence.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a configurable logger that supports output to console and file. It uses zap for logging and lumberjack for file rotation.
func NewConsoleLogger ¶
NewConsoleLogger creates a new logger that outputs only to the console. It applies any provided functional options for additional configuration.
func NewLogger ¶
NewLogger creates a new logger that outputs to both the console and a file. It uses the provided configuration and applies any functional options.
func NewSimpleConsoleLogger ¶
NewSimpleConsoleLogger creates a logger that outputs only to the console using default configuration.
func NewSimpleLogger ¶
NewSimpleLogger creates a logger that outputs to both console and a text file using default configuration.
func (*Logger) ClearFields ¶ added in v0.2.0
func (l *Logger) ClearFields()
ClearFields clears all custom zap fields.
func (*Logger) ConsoleLogger ¶
func (l *Logger) ConsoleLogger() *zap.SugaredLogger
ConsoleLogger returns the underlying console logger instance.
func (*Logger) FileLogger ¶
func (l *Logger) FileLogger() *zap.SugaredLogger
FileLogger returns the underlying file logger instance.
func (*Logger) LumberjackLogger ¶
func (l *Logger) LumberjackLogger() *lumberjack.Logger
func (*Logger) ServiceName ¶
ServiceName returns the name of the service associated with the logger.
func (*Logger) SetLogLevel ¶
SetLogLevel updates the log level for both console and file loggers. It returns an error if updating any of the loggers fails.
func (*Logger) WithContext ¶ added in v0.2.0
WithContext returns a new logger instance that attaches common fields (request_id, trace_id) extracted from the provided context.
func (*Logger) WithFields ¶ added in v0.2.0
WithFields returns a new logger instance with the specified structured fields attached.
type Option ¶ added in v0.2.0
Option represents a functional option for configuring a Logger.
func WithCallerSkip ¶ added in v0.2.0
WithCallerSkip adjusts the caller skip level for accurate file/line reporting.
func WithConsoleEncoder ¶ added in v0.2.0
WithConsoleEncoder sets a custom zapcore.Encoder for the console logger.
func WithCustomField ¶ added in v0.2.0
WithCustomField is an option to attach a custom field to every log.
func WithCustomFields ¶ added in v0.2.0
WithCustomFields attaches multiple custom fields to every log entry.
func WithCustomLevelFormats ¶ added in v0.2.0
func WithCustomLevelFormats(formats map[zapcore.Level]LevelFormat) Option
WithCustomLevelFormats is an option that sets custom level formats on the logger's config.
func WithFileEncoder ¶ added in v0.2.0
WithFileEncoder sets a custom zapcore.Encoder for the file logger.
func WithServiceName ¶ added in v0.2.0
WithServiceName is an option to set the service name.