Documentation
¶
Overview ¶
Package logger provides a customizable logging system for the application.
Index ¶
- Constants
- Variables
- func Debug(arguments ...any)
- func Debugf(format string, arguments ...any)
- func Error(arguments ...any)
- func Errorf(format string, arguments ...any)
- func Fatal(arguments ...any)
- func Fatalf(format string, arguments ...any)
- func Info(arguments ...any)
- func Infof(format string, arguments ...any)
- func NewContext(ctx context.Context, reqID string) context.Context
- func Panic(arguments ...any)
- func Panicf(format string, arguments ...any)
- func SetConfig(cfg *Config)
- func Warn(arguments ...any)
- func Warnf(format string, arguments ...any)
- type Config
- type Level
- type Log
- func (l *Log) Caller(frame int) Logger
- func (l *Log) Debug(arguments ...any)
- func (l *Log) Debugf(format string, arguments ...any)
- func (l *Log) Error(arguments ...any)
- func (l *Log) Errorf(format string, arguments ...any)
- func (l *Log) Fatal(arguments ...any)
- func (l *Log) Fatalf(format string, arguments ...any)
- func (l *Log) Info(arguments ...any)
- func (l *Log) Infof(format string, arguments ...any)
- func (l *Log) Panic(arguments ...any)
- func (l *Log) Panicf(format string, arguments ...any)
- func (l *Log) SetLevel(level Level) Logger
- func (l *Log) TraceID() string
- func (l *Log) Warn(arguments ...any)
- func (l *Log) Warnf(format string, arguments ...any)
- func (l *Log) WithContext(ctx ...context.Context) context.Context
- func (l *Log) WithError(err error) Logger
- func (l *Log) WithField(key string, value any) Logger
- func (l *Log) WithFields(fields map[string]any) Logger
- type Logger
Constants ¶
View Source
const (
// TraceIDKey key
TraceIDKey key = 0
)
Variables ¶
View Source
var DefaultGenRequestID = func() string { var b [12]byte binary.LittleEndian.PutUint32(b[:], pid) binary.LittleEndian.PutUint64(b[4:], uint64(time.Now().UnixNano())) return base64.URLEncoding.EncodeToString(b[:]) }
DefaultGenRequestID default generate request id
View Source
var DefaultLogLevel = TraceLevel
View Source
var DefaultLogTimeFormat = "2006-01-02 15:04:05.000000"
DefaultLogTimeFormat default log time format
View Source
var New = func(traceID ...string) Logger {
return newLogger(config, traceID...)
}
New return logger
View Source
var TraceID = "x-request-id"
TraceID is the key for the x-request-id header.
Functions ¶
func NewContext ¶
NewContext return context with reqid
Types ¶
type Config ¶
type Config struct {
// log level
LogLevel Level `json:"log_level" yaml:"log_level" mapstructure:"log_level"`
// Enable console logging
ConsoleLoggingEnabled bool `json:"console_logging_enabled" yaml:"console_logging_enabled" mapstructure:"console_logging_enabled"`
// EncodeLogsAsJSON makes the log framework log JSON
EncodeLogsAsJSON bool `json:"encode_logs_as_json" yaml:"encode_logs_as_json" mapstructure:"encode_logs_as_json"`
// FileLoggingEnabled makes the framework log to a file, the fields below can be skipped if this value is false!
FileLoggingEnabled bool `json:"file_logging_enabled" yaml:"file_logging_enabled" mapstructure:"file_logging_enabled"`
// Filename is the name of the logfile which will be placed inside the directory
Filename string `json:"filename" yaml:"filename" mapstructure:"filename"`
// MaxSize the max size in MB of the logfile before it's rolled
MaxSize int `json:"max_size" yaml:"max_size" mapstructure:"max_size"`
// MaxBackups the max number of rolled files to keep
MaxBackups int `json:"max_backups" yaml:"max_backups" mapstructure:"max_backups"`
// MaxAge the max age in days to keep a logfile
MaxAge int `json:"max_age" yaml:"max_age" mapstructure:"max_age"`
// contains filtered or unexported fields
}
Config for logging
type Level ¶
type Level string
Level type
const ( // DebugLevel defines debug log level. DebugLevel Level = "debug" // InfoLevel defines info log level. InfoLevel Level = "info" // WarnLevel defines warn log level. WarnLevel Level = "warn" // ErrorLevel defines error log level. ErrorLevel Level = "error" // FatalLevel defines fatal log level. FatalLevel Level = "fatal" // PanicLevel defines panic log level. PanicLevel Level = "panic" // NoLevel defines an absent log level. NoLevel Level = "no" // Disabled disables the logger. Disabled Level = "disabled" // TraceLevel defines trace log level. TraceLevel Level = "trace" )
func (Level) ZerologLevel ¶ added in v0.0.2
type Log ¶
type Log struct {
// contains filtered or unexported fields
}
Log implement Logger
func (*Log) WithContext ¶
WithContext return context with log
type Logger ¶
type Logger interface {
// STD log
Debug(arguments ...any)
Info(arguments ...any)
Warn(arguments ...any)
Error(arguments ...any)
Fatal(arguments ...any)
Panic(arguments ...any)
Debugf(format string, arguments ...any)
Infof(format string, arguments ...any)
Warnf(format string, arguments ...any)
Errorf(format string, arguments ...any)
Fatalf(format string, arguments ...any)
Panicf(format string, arguments ...any)
// Field logger
WithField(key string, value any) Logger
WithFields(fields map[string]any) Logger
WithError(err error) Logger
// Set level
SetLevel(level Level) Logger
// Caller skip frame count
Caller(frame int) Logger
// Trace ID
TraceID() string
// context
WithContext(ctx ...context.Context) context.Context
}
Logger logger methods
func NewWithConfig ¶
NewWithConfig return logger with config
func NewWithContext ¶
NewWithContext return logger with context
func NewWithoutCaller ¶
NewWithoutCaller new log without caller field
Click to show internal directories.
Click to hide internal directories.