Documentation
¶
Index ¶
- Constants
- func AddHook(hook Hook)
- func Close() error
- func Debugf(ctx context.Context, format string, v ...any)
- func Debugw(ctx context.Context, msg string, fields ...Field)
- func Errorf(ctx context.Context, err error, format string, v ...any)
- func Errorw(ctx context.Context, err error, msg string, fields ...Field)
- func Fatalf(ctx context.Context, err error, format string, v ...any)
- func Fatalw(ctx context.Context, err error, msg string, fields ...Field)
- func Infof(ctx context.Context, format string, v ...any)
- func Infow(ctx context.Context, msg string, fields ...Field)
- func Panicf(ctx context.Context, err error, format string, v ...any)
- func Panicw(ctx context.Context, err error, msg string, fields ...Field)
- func RemoveHook(hookName string)
- func SetCaller(enabled bool)
- func SetConfig(config *Config) error
- func SetCtxKeys(keys []string)
- func SetDefaultLogger(l *Logger)
- func SetFilepath(path string)
- func SetFormat(format string)
- func SetLevel(level Level)
- func SetMaxAge(maxAge int)
- func SetMaxBackups(maxBackups int)
- func SetMaxSize(maxSize int)
- func SetStdout(enabled bool)
- func SetTimeFormat(timeFormat string)
- func Warnf(ctx context.Context, format string, v ...any)
- func Warnw(ctx context.Context, msg string, fields ...Field)
- type Config
- type CtxKey
- type Entry
- func (e *Entry) AddField(field Field) *Entry
- func (e *Entry) GetContext() context.Context
- func (e *Entry) GetFields() Fields
- func (e *Entry) GetMsg() string
- func (e *Entry) SetContext(ctx context.Context) *Entry
- func (e *Entry) SetFields(fields Fields) *Entry
- func (e *Entry) SetMsg(msg string) *Entry
- type Field
- func Any(key string, value any) Field
- func Bool(key string, val bool) Field
- func Duration(key string, val time.Duration) Field
- func Err(err error) Field
- func Float64(key string, val float64) Field
- func Int(key string, val int) Field
- func Int64(key string, val int64) Field
- func Skip() Field
- func String(key string, val string) Field
- func Time(key string, val time.Time) Field
- func Uint(key string, val uint) Field
- func Uint64(key string, val uint64) Field
- type Fields
- type Hook
- type ILogger
- type Level
- type Logger
- func (l *Logger) AddHook(hook Hook) error
- func (l *Logger) Close() error
- func (l *Logger) Debugf(ctx context.Context, format string, v ...any)
- func (l *Logger) Debugw(ctx context.Context, msg string, fields ...Field)
- func (l *Logger) Errorf(ctx context.Context, err error, format string, v ...any)
- func (l *Logger) Errorw(ctx context.Context, err error, msg string, fields ...Field)
- func (l *Logger) Fatalf(ctx context.Context, err error, format string, v ...any)
- func (l *Logger) Fatalw(ctx context.Context, err error, msg string, fields ...Field)
- func (l *Logger) GetConfig() *Config
- func (l *Logger) GetLevel() Level
- func (l *Logger) Infof(ctx context.Context, format string, v ...any)
- func (l *Logger) Infow(ctx context.Context, msg string, fields ...Field)
- func (l *Logger) Panicf(ctx context.Context, err error, format string, v ...any)
- func (l *Logger) Panicw(ctx context.Context, err error, msg string, fields ...Field)
- func (l *Logger) RemoveHook(hookName string)
- func (l *Logger) SetConfig(config *Config) error
- func (l *Logger) SetConfigWithMap(configMap map[string]any) error
- func (l *Logger) SetLevel(level Level)
- func (l *Logger) Warnf(ctx context.Context, format string, v ...any)
- func (l *Logger) Warnw(ctx context.Context, msg string, fields ...Field)
- func (l *Logger) With(fields ...Field) *Logger
Constants ¶
const (
DefaultName = "default"
)
Variables ¶
This section is empty.
Functions ¶
func Close ¶ added in v0.1.7
func Close() error
Close closes the logger and its underlying resources.
func Debugw ¶ added in v0.1.7
Debugw prints the logging content with [DEBU] header, custom format and newline.
func Errorw ¶ added in v0.1.7
Errorw prints the logging content with [ERRO] header, custom format and newline.
func Fatalf ¶
Fatalf prints the logging content with [FATA] header, custom format and newline, then exit the current process.
func Fatalw ¶ added in v0.1.7
Fatalw prints the logging content with [FATA] header, custom format and newline, then exit the current process.
func Panicf ¶
Panicf prints the logging content with [PANI] header, custom format and newline, then panics.
func RemoveHook ¶ added in v0.1.9
func RemoveHook(hookName string)
RemoveHook removes a hook from the logger.
func SetCtxKeys ¶
func SetCtxKeys(keys []string)
SetCtxKeys sets the context keys to extract values from.
func SetDefaultLogger ¶
func SetDefaultLogger(l *Logger)
SetDefaultLogger sets the default logger for package glog. Note that there might be concurrent safety issue if calls this function in different goroutines.
func SetFilepath ¶ added in v0.1.7
func SetFilepath(path string)
SetFilepath sets the log file path.
func SetMaxAge ¶ added in v0.1.7
func SetMaxAge(maxAge int)
SetMaxAge sets the max age of the log file.
func SetMaxBackups ¶ added in v0.1.7
func SetMaxBackups(maxBackups int)
SetMaxBackups sets the max backups of the log file.
func SetMaxSize ¶ added in v0.1.7
func SetMaxSize(maxSize int)
SetMaxSize sets the max size of the log file.
Types ¶
type Config ¶
type Config struct {
// Writer is the custom writer for the logger.
Writer io.Writer `mconv:"-"`
// ServiceName is the service name.
ServiceName string `mconv:"service_name"`
// Level is the log level.
Level Level `mconv:"level"`
// TimeFormat is the log time format.
TimeFormat string `mconv:"time_format"`
// Format is the log format. Only support "json" and "text".
Format string `mconv:"format"`
// Caller controls whether the caller’s file and line number are included in logs.
// If true, the caller’s file and line number will be added to the log entries.
Caller bool `mconv:"caller"`
// Development is the development mode.
// If true, the logger will be in development mode.
// It will print the error stack trace.
Development bool `mconv:"development"`
// Filepath is the log file path.
// e.g., /var/log/app.log or /var/log/app.{YYYYmmdd}.log
Filepath string `mconv:"filepath"`
// MaxSize is the maximum size in megabytes of the log file before it gets rotated.
// It is only applicable for 'size' rotation type.
MaxSize int `mconv:"max_size"` // (MB)
// MaxBackups is the maximum number of old log files to retain.
// It is only applicable for 'size' rotation type.
MaxBackups int `mconv:"max_backups"` // (files)
// MaxAge is the maximum number of days to retain old log files.
// It is applicable for both 'size' and 'date' rotation types.
MaxAge int `mconv:"max_age"` // (days)
// Stdout is the stdout print.
Stdout bool `mconv:"stdout"`
// CtxKeys is the context keys to extract.
CtxKeys []string `mconv:"ctx_keys"`
}
func ConfigFromMap ¶ added in v0.1.2
ConfigFromMap parses and returns config from given map.
type Entry ¶
type Entry struct {
// contains filtered or unexported fields
}
Entry represents a log entry.
func (*Entry) GetContext ¶ added in v0.1.8
GetContext returns the context of the entry.
func (*Entry) SetContext ¶ added in v0.1.8
SetContext sets the context of the entry.
type Field ¶ added in v0.1.7
Field is a key-value pair used for structured logging. It is a type-safe and optimized representation of a logging field, designed to be structurally compatible with a popular high-performance logging library's internal field type for zero-overhead conversion.
func Any ¶ added in v0.1.7
Any takes a key and an arbitrary value and chooses the best way to represent them as a field.
func Err ¶ added in v0.1.7
Err constructs a field with an error. If the error is nil, a no-op field is returned, which is ignored by the logger.
func Skip ¶ added in v0.1.7
func Skip() Field
Skip constructs a no-op field, which is ignored by the logger.
func Time ¶ added in v0.1.7
Time constructs a field with a time.Time value. It's formatted as a floating-point number of seconds since the Unix epoch.
type ILogger ¶
type ILogger interface {
Debugf(ctx context.Context, format string, v ...any) // Debugf logs a message at level Debug.
Debugw(ctx context.Context, msg string, fields ...Field) // Debugw logs a message at level Debug.
Infof(ctx context.Context, format string, v ...any) // Infof logs a message at level Info.
Infow(ctx context.Context, msg string, fields ...Field) // Infow logs a message at level Info.
Warnf(ctx context.Context, format string, v ...any) // Warnf logs a message at level Warn.
Warnw(ctx context.Context, msg string, fields ...Field) // Warnw logs a message at level Warn.
Errorf(ctx context.Context, err error, format string, v ...any) // Errorf logs a message at level Error.
Errorw(ctx context.Context, err error, msg string, fields ...Field) // Errorw logs a message at level Error.
Fatalf(ctx context.Context, err error, format string, v ...any) // Fatalf logs a message at level Fatal.
Fatalw(ctx context.Context, err error, msg string, fields ...Field) // Fatalw logs a message at level Fatal.
Panicf(ctx context.Context, err error, format string, v ...any) // Panicf logs a message at level Panic.
Panicw(ctx context.Context, err error, msg string, fields ...Field) // Panicw logs a message at level Panic.
}
ILogger is the interface for the logger.
type Level ¶
type Level int8
Level is the log level.
func ParseLevel ¶ added in v0.1.4
ParseLevel parses a string level and returns the Level value.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the struct for logging management.
func NewWithZap ¶ added in v0.2.0
NewWithZap creates a new Logger instance using an existing zap.Logger.
This constructor is intended for advanced users or those who need customizations that are not supported by the standard New() constructor. When using this function:
1. You are responsible for configuring the zap.Logger (output, format, rotation, etc.) 2. You must manage the lifecycle of any resources associated with your zap.Logger 3. File rotation and cleanup are handled by your zap.Logger configuration, not by mlog 4. The provided config is used only for mlog-specific features (hooks, context keys, etc.)
For most use cases, the standard New() constructor is recommended as it provides integrated file management, rotation, and cleanup functionality.
Example:
zapLogger := zap.New(core) // Your custom zap logger
logger := mlog.NewWithZap(zapLogger, &mlog.Config{
CtxKeys: []string{"trace_id", "user_id"},
})
defer logger.Close() // This only calls zapLogger.Sync()
func (*Logger) GetConfig ¶ added in v0.2.0
GetConfig returns the current configuration of the logger.
func (*Logger) RemoveHook ¶ added in v0.1.7
RemoveHook removes a hook from the logger.
func (*Logger) SetConfigWithMap ¶
SetConfigWithMap sets the logger configuration using a map.