Documentation
¶
Overview ¶
Package xlogger provides advanced logging functionalities built on top of zap.
Index ¶
- Constants
- func NewFxEventLogger(logger Logger) fxevent.Logger
- func RunWithTrace(requestID, correlationID string, fn func() error) error
- func RunWithTraceVoid(requestID, correlationID string, fn func())
- func TraceCorrelationID() string
- func TraceRequestID() string
- type Config
- func (c *Config) GetFormat() string
- func (c *Config) GetLevel() string
- func (c *Config) IsDebugLevel() bool
- func (c *Config) IsDevelopment() bool
- func (c *Config) IsErrorLevel() bool
- func (c *Config) IsInfoLevel() bool
- func (c *Config) IsJSONFormat() bool
- func (c *Config) IsTextFormat() bool
- func (c *Config) IsWarnLevel() bool
- type Field
- func Any(key string, value interface{}) Field
- func Bool(key string, value bool) Field
- func Duration(key string, value time.Duration) Field
- func Error(err error) Field
- func Float64(key string, value float64) Field
- func Int(key string, value int) Field
- func Int64(key string, value int64) Field
- func NamedError(key string, err error) Field
- func String(key, value string) Field
- func Time(key string, value time.Time) Field
- type FieldType
- type GORMLogger
- func (l *GORMLogger) Error(_ context.Context, msg string, data ...interface{})
- func (l *GORMLogger) Info(_ context.Context, msg string, data ...interface{})
- func (l *GORMLogger) LogMode(level gormlogger.LogLevel) gormlogger.Interface
- func (l *GORMLogger) SetIgnoreRecordNotFoundError(ignore bool) *GORMLogger
- func (l *GORMLogger) SetMaxPathLevels(levels int) *GORMLogger
- func (l *GORMLogger) SetSlowThreshold(threshold time.Duration) *GORMLogger
- func (l *GORMLogger) Trace(_ context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), ...)
- func (l *GORMLogger) Warn(_ context.Context, msg string, data ...interface{})
- type LogFormat
- type Logger
- type Option
- func WithCallerSkip(skip int) Option
- func WithDevelopment(dev bool) Option
- func WithDisableCaller(disable bool) Option
- func WithDisableStacktrace(disable bool) Option
- func WithFormat(format LogFormat) Option
- func WithLevel(level zapcore.Level) Option
- func WithLevelString(level string) Option
- func WithTimeFormat(format string) Option
- type ZapLogger
- func (l *ZapLogger) Debug(msg string, fields ...Field)
- func (l *ZapLogger) Error(msg string, fields ...Field)
- func (l *ZapLogger) Fatal(msg string, fields ...Field)
- func (l *ZapLogger) ForFxEvent() fxevent.Logger
- func (l *ZapLogger) ForGORM() *GORMLogger
- func (l *ZapLogger) ForInfra(component string) Logger
- func (l *ZapLogger) Info(msg string, fields ...Field)
- func (l *ZapLogger) Level() zapcore.Level
- func (l *ZapLogger) Panic(msg string, fields ...Field)
- func (l *ZapLogger) Sync() error
- func (l *ZapLogger) Warn(msg string, fields ...Field)
- func (l *ZapLogger) With(fields ...Field) Logger
Constants ¶
const (
ConsoleTimeLayout = "2006-01-02 15:04:05 -07:00"
)
Variables ¶
This section is empty.
Functions ¶
func NewFxEventLogger ¶
NewFxEventLogger creates a new FX event logger from the provided logger
func RunWithTrace ¶
RunWithTrace executes fn within a goroutine-local context that stores request and correlation identifiers for later retrieval.
func RunWithTraceVoid ¶
func RunWithTraceVoid(requestID, correlationID string, fn func())
RunWithTraceVoid executes fn within the trace context when no error propagation is required.
func TraceCorrelationID ¶
func TraceCorrelationID() string
TraceCorrelationID returns the goroutine-local correlation identifier.
func TraceRequestID ¶
func TraceRequestID() string
TraceRequestID returns the goroutine-local request identifier.
Types ¶
type Config ¶
type Config struct {
Level zapcore.Level // Minimum log level
Format LogFormat // Log format: FormatJSON or FormatText
Development bool // Development mode (pretty printing)
DisableCaller bool // Disable caller information
DisableStacktrace bool // Disable stacktrace in errors
TimeFormat string // Time format (empty for default)
CallerSkip int // Number of caller frames to skip
}
Config represents logger configuration options.
func DefaultLoggerConfig ¶
func DefaultLoggerConfig() *Config
DefaultLoggerConfig returns default logger configuration with INFO level and JSON format.
Default values:
- Level: INFO
- Format: FormatJSON
- Development: false
- DisableCaller: false
- DisableStacktrace: true
- CallerSkip: 1
Example:
cfg := xlogger.DefaultLoggerConfig() logger, err := xlogger.NewZapLogger(cfg)
func NewLoggerConfig ¶
NewLoggerConfig creates logger configuration with functional options. Starts with DefaultLoggerConfig and applies each option in order.
Example:
cfg := xlogger.NewLoggerConfig(
xlogger.WithLevel(zapcore.DebugLevel),
xlogger.WithFormat(xlogger.FormatText),
xlogger.WithDevelopment(true),
)
logger, err := xlogger.NewZapLogger(cfg)
func (*Config) IsDebugLevel ¶
IsDebugLevel returns true if the log level is debug.
func (*Config) IsDevelopment ¶
IsDevelopment returns true if development mode is enabled.
func (*Config) IsErrorLevel ¶
IsErrorLevel returns true if the log level is error.
func (*Config) IsInfoLevel ¶
IsInfoLevel returns true if the log level is info.
func (*Config) IsJSONFormat ¶
IsJSONFormat returns true if the format is JSON.
func (*Config) IsTextFormat ¶
IsTextFormat returns true if the format is text.
func (*Config) IsWarnLevel ¶
IsWarnLevel returns true if the log level is warn.
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field represents a structured log field with key-value pairs This abstraction allows for type-safe logging without coupling to zap
func NamedError ¶
NamedError creates an error field with a specific name
type GORMLogger ¶
type GORMLogger struct {
// contains filtered or unexported fields
}
GORMLogger implements gorm.logger.Interface using our Logger
func NewGORMLogger ¶
func NewGORMLogger(logger Logger) *GORMLogger
NewGORMLogger creates a new GORM logger adapter with sensible defaults
func (*GORMLogger) Error ¶
func (l *GORMLogger) Error(_ context.Context, msg string, data ...interface{})
Error implements gorm.logger.Interface
func (*GORMLogger) Info ¶
func (l *GORMLogger) Info(_ context.Context, msg string, data ...interface{})
Info implements gorm.logger.Interface
func (*GORMLogger) LogMode ¶
func (l *GORMLogger) LogMode(level gormlogger.LogLevel) gormlogger.Interface
LogMode implements gorm.logger.Interface
func (*GORMLogger) SetIgnoreRecordNotFoundError ¶
func (l *GORMLogger) SetIgnoreRecordNotFoundError(ignore bool) *GORMLogger
SetIgnoreRecordNotFoundError configures whether to ignore ErrRecordNotFound
func (*GORMLogger) SetMaxPathLevels ¶
func (l *GORMLogger) SetMaxPathLevels(levels int) *GORMLogger
SetMaxPathLevels configures maximum path levels to display (-1 = show "_", 0 = show full path)
func (*GORMLogger) SetSlowThreshold ¶
func (l *GORMLogger) SetSlowThreshold(threshold time.Duration) *GORMLogger
SetSlowThreshold configures slow query threshold
type LogFormat ¶
type LogFormat string
LogFormat represents the log output format.
type Logger ¶
type Logger interface {
// Core logging methods for different levels
Debug(msg string, fields ...Field)
Info(msg string, fields ...Field)
Warn(msg string, fields ...Field)
Error(msg string, fields ...Field)
// These methods will terminate the application after logging
Panic(msg string, fields ...Field)
Fatal(msg string, fields ...Field)
// Logger enhancement methods
With(fields ...Field) Logger
// Infrastructure optimization methods
ForInfra(component string) Logger
ForFxEvent() fxevent.Logger
ForGORM() *GORMLogger
// Logger configuration methods
Level() zapcore.Level
// Utility methods
Sync() error
}
Logger represents the main logging interface for structured logging This interface provides both application and infrastructure logging capabilities
type Option ¶ added in v1.1.0
type Option func(*Config)
Option is a function that modifies Config.
func WithCallerSkip ¶ added in v1.1.0
WithCallerSkip sets the number of caller frames to skip.
Example:
cfg := xlogger.NewLoggerConfig(
xlogger.WithCallerSkip(2),
)
func WithDevelopment ¶ added in v1.1.0
WithDevelopment enables or disables development mode.
Example:
cfg := xlogger.NewLoggerConfig(
xlogger.WithDevelopment(true),
)
func WithDisableCaller ¶ added in v1.1.0
WithDisableCaller disables caller information.
Example:
cfg := xlogger.NewLoggerConfig(
xlogger.WithDisableCaller(true),
)
func WithDisableStacktrace ¶ added in v1.1.0
WithDisableStacktrace disables stacktrace in errors.
Example:
cfg := xlogger.NewLoggerConfig(
xlogger.WithDisableStacktrace(false),
)
func WithFormat ¶ added in v1.1.0
WithFormat sets the log format.
Example:
cfg := xlogger.NewLoggerConfig(
xlogger.WithFormat(xlogger.FormatText),
)
func WithLevel ¶ added in v1.1.0
WithLevel sets the log level.
Example:
cfg := xlogger.NewLoggerConfig(
xlogger.WithLevel(zapcore.DebugLevel),
)
func WithLevelString ¶ added in v1.2.0
WithLevelString sets the log level from string. Supported values: "debug", "info", "warn", "error", "dpanic", "panic", "fatal" Invalid values are ignored and the default level (INFO) is used.
Example:
cfg := xlogger.NewLoggerConfig(
xlogger.WithLevelString("debug"),
)
func WithTimeFormat ¶ added in v1.1.0
WithTimeFormat sets the time format.
Example:
cfg := xlogger.NewLoggerConfig(
xlogger.WithTimeFormat("2006-01-02 15:04:05"),
)
type ZapLogger ¶
type ZapLogger struct {
// contains filtered or unexported fields
}
ZapLogger implements Logger interface using zap as the underlying logger
func NewZapLogger ¶
NewZapLogger creates a ZapLogger with full configuration support
func (*ZapLogger) ForFxEvent ¶
ForFxEvent returns a FX event logger that implements fxevent.Logger interface
func (*ZapLogger) ForGORM ¶
func (l *ZapLogger) ForGORM() *GORMLogger
ForGORM returns a pre-cached logger optimized for GORM
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
basic
command
Package main demonstrates the basic usage of the xlogger package.
|
Package main demonstrates the basic usage of the xlogger package. |
|
trace
command
Package main demonstrates trace context functionality in xlogger.
|
Package main demonstrates trace context functionality in xlogger. |