Documentation
¶
Overview ¶
Package abslog provides an abstraction layer for logging libraries, allowing seamless switching between different logging backends (Zap, Logrus) while maintaining a consistent API.
Index ¶
- Variables
- func GetCtxSeparator() string
- func ResetCtxKey()
- func ResetCtxSeparator()
- func SetCtxKey(key string)
- func SetCtxSeparator(separator string)
- func SetLogger(logger AbsLog)
- func SetLoggerType(typ LoggerType)
- type AbsLog
- type AbsLogBuilder
- type ContextKeyType
- type EncoderType
- type LogLevel
- type LoggerAdapter
- func (a *LoggerAdapter) Debug(args ...any)
- func (a *LoggerAdapter) Debugf(format string, args ...any)
- func (a *LoggerAdapter) Error(args ...any)
- func (a *LoggerAdapter) Errorf(format string, args ...any)
- func (a *LoggerAdapter) Fatal(args ...any)
- func (a *LoggerAdapter) Fatalf(format string, args ...any)
- func (a *LoggerAdapter) Info(args ...any)
- func (a *LoggerAdapter) Infof(format string, args ...any)
- func (a *LoggerAdapter) Panic(args ...any)
- func (a *LoggerAdapter) Panicf(format string, args ...any)
- func (a *LoggerAdapter) Warn(args ...any)
- func (a *LoggerAdapter) Warnf(format string, args ...any)
- type LoggerGen
- type LoggerType
Constants ¶
This section is empty.
Variables ¶
var Debug func(args ...any)
Debug logs a message at level Debug on the standard logger.
var DebugCtx func(ctx context.Context, args ...any)
var DebugCtxf func(ctx context.Context, format string, args ...any)
var Debugf func(format string, args ...any)
var Error func(args ...any)
Error logs a message at level Error on the standard logger.
var ErrorCtx func(ctx context.Context, args ...any)
var ErrorCtxf func(ctx context.Context, format string, args ...any)
var Errorf func(format string, args ...any)
var Fatal func(args ...any)
Fatal logs a message at level Fatal on the standard logger.
var FatalCtx func(ctx context.Context, args ...any)
var FatalCtxf func(ctx context.Context, format string, args ...any)
var Fatalf func(format string, args ...any)
var Info func(args ...any)
Info logs a message at level Info on the standard logger.
var InfoCtx func(ctx context.Context, args ...any)
var InfoCtxf func(ctx context.Context, format string, args ...any)
var Infof func(format string, args ...any)
var Panic func(args ...any)
Panic logs a message at level Panic on the standard logger.
var PanicCtx func(ctx context.Context, args ...any)
var PanicCtxf func(ctx context.Context, format string, args ...any)
var Panicf func(format string, args ...any)
var Warn func(args ...any)
Warn logs a message at level Warn on the standard logger.
var WarnCtx func(ctx context.Context, args ...any)
var WarnCtxf func(ctx context.Context, format string, args ...any)
var Warnf func(format string, args ...any)
Functions ¶
func GetCtxSeparator ¶
func GetCtxSeparator() string
GetCtxSeparator returns the current separator used between context values and log messages.
func ResetCtxSeparator ¶
func ResetCtxSeparator()
ResetCtxSeparator resets the context separator to its default value.
func SetCtxKey ¶
func SetCtxKey(key string)
SetCtxKey sets the key used to retrieve context values from context.Context. This allows customization of how context values are stored and retrieved. If key is empty or only whitespace, the default key "abslog" will be used.
func SetCtxSeparator ¶
func SetCtxSeparator(separator string)
SetCtxSeparator sets the string used to separate context values from log messages. If separator is empty or only whitespace, the default separator " -> " will be used.
func SetLogger ¶
func SetLogger(logger AbsLog)
SetLogger sets the provided AbsLog instance as the global logger, updating all global logging function variables.
func SetLoggerType ¶
func SetLoggerType(typ LoggerType)
SetLoggerType configures the global logger to use the specified logger type (ZapLogger or LogrusLogger) with default settings.
Types ¶
type AbsLog ¶
type AbsLog interface {
Debug(args ...any)
Debugf(format string, args ...any)
Info(args ...any)
Infof(format string, args ...any)
Warn(args ...any)
Warnf(format string, args ...any)
Error(args ...any)
Errorf(format string, args ...any)
Fatal(args ...any)
Fatalf(format string, args ...any)
Panic(args ...any)
Panicf(format string, args ...any)
}
AbsLog defines the interface for abstracted logging functionality. It provides methods for logging at different levels with optional formatting.
func NewLoggerAdapter ¶
func NewLoggerAdapter(logger interface {
Debug(args ...any)
Debugf(format string, args ...any)
Info(args ...any)
Infof(format string, args ...any)
Warn(args ...any)
Warnf(format string, args ...any)
Error(args ...any)
Errorf(format string, args ...any)
Fatal(args ...any)
Fatalf(format string, args ...any)
Panic(args ...any)
Panicf(format string, args ...any)
}) AbsLog
NewLoggerAdapter creates a new LoggerAdapter wrapping the provided logger.
type AbsLogBuilder ¶
type AbsLogBuilder interface {
LogLevel(level LogLevel) AbsLogBuilder
LoggerGen(generator LoggerGen) AbsLogBuilder
LoggerType(loggerType LoggerType) AbsLogBuilder
EncoderType(encoderType EncoderType) AbsLogBuilder
ContextKey(key string) AbsLogBuilder
BuildAndSetAsGlobal() AbsLog
Build() AbsLog
}
AbsLogBuilder is the interface that wraps the Builder methods to create a new AbsLog.
func GetAbsLogBuilder ¶
func GetAbsLogBuilder() AbsLogBuilder
GetAbsLogBuilder returns a new AbsLog builder.
type ContextKeyType ¶
type ContextKeyType string
contextKey is the current key used to store context values in context.Context
func GetCtxKey ¶
func GetCtxKey() ContextKeyType
GetCtxKey returns the current key used to retrieve context values from context.Context.
type EncoderType ¶
type EncoderType int8
EncoderType represents the format used for log output.
const ( // ConsoleEncoder formats logs for human-readable console output. ConsoleEncoder EncoderType = iota + 1 // JSONEncoder formats logs as JSON for structured logging. JSONEncoder )
Encoder type constants defining the output format for log messages.
type LogLevel ¶
type LogLevel int8
LogLevel represents the severity level of log messages.
const ( // DebugLevel is used for debug messages, typically only enabled during development. DebugLevel LogLevel = iota + 1 // InfoLevel is used for general informational messages. InfoLevel // WarnLevel is used for warning messages that indicate potential issues. WarnLevel // ErrorLevel is used for error messages that indicate failures. ErrorLevel // PanicLevel is used for panic messages that cause the application to panic. PanicLevel // FatalLevel is used for fatal messages that cause the application to exit. FatalLevel )
Log level constants defining the severity of log messages.
type LoggerAdapter ¶
type LoggerAdapter struct {
// contains filtered or unexported fields
}
LoggerAdapter adapts any logger that implements the basic logging methods to the AbsLog interface. This provides a consistent abstraction layer while handling type conversions.
func (*LoggerAdapter) Debug ¶
func (a *LoggerAdapter) Debug(args ...any)
Debug logs a message at debug level.
func (*LoggerAdapter) Debugf ¶
func (a *LoggerAdapter) Debugf(format string, args ...any)
Debugf logs a formatted message at debug level.
func (*LoggerAdapter) Error ¶
func (a *LoggerAdapter) Error(args ...any)
Error logs a message at error level.
func (*LoggerAdapter) Errorf ¶
func (a *LoggerAdapter) Errorf(format string, args ...any)
Errorf logs a formatted message at error level.
func (*LoggerAdapter) Fatal ¶
func (a *LoggerAdapter) Fatal(args ...any)
Fatal logs a message at fatal level and exits the program.
func (*LoggerAdapter) Fatalf ¶
func (a *LoggerAdapter) Fatalf(format string, args ...any)
Fatalf logs a formatted message at fatal level and exits the program.
func (*LoggerAdapter) Info ¶
func (a *LoggerAdapter) Info(args ...any)
Info logs a message at info level.
func (*LoggerAdapter) Infof ¶
func (a *LoggerAdapter) Infof(format string, args ...any)
Infof logs a formatted message at info level.
func (*LoggerAdapter) Panic ¶
func (a *LoggerAdapter) Panic(args ...any)
Panic logs a message at panic level and panics.
func (*LoggerAdapter) Panicf ¶
func (a *LoggerAdapter) Panicf(format string, args ...any)
Panicf logs a formatted message at panic level and panics.
func (*LoggerAdapter) Warn ¶
func (a *LoggerAdapter) Warn(args ...any)
Warn logs a message at warn level.
func (*LoggerAdapter) Warnf ¶
func (a *LoggerAdapter) Warnf(format string, args ...any)
Warnf logs a formatted message at warn level.
type LoggerGen ¶
type LoggerGen func(logLevel LogLevel, encoder EncoderType) AbsLog
LoggerGen is a function type that creates an AbsLog instance with the specified log level and encoder type.
type LoggerType ¶
type LoggerType int8
LoggerType represents the underlying logging library to use.
const ( // ZapLogger uses the Uber Zap logging library as the backend. ZapLogger LoggerType = iota + 1 // LogrusLogger uses the Sirupsen Logrus logging library as the backend. LogrusLogger )
Logger type constants defining which logging backend to use.