mlog

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2025 License: MIT Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DefaultName = "default"
)

Variables

This section is empty.

Functions

func AddHook added in v0.1.7

func AddHook(hook Hook)

AddHook adds a hook to the logger.

func Close added in v0.1.7

func Close() error

Close closes the logger and its underlying resources.

func Debugf

func Debugf(ctx context.Context, format string, v ...any)

Debugf prints the logging content with [DEBU] header, custom format and newline.

func Debugw added in v0.1.7

func Debugw(ctx context.Context, msg string, fields ...Field)

Debugw prints the logging content with [DEBU] header, custom format and newline.

func Errorf

func Errorf(ctx context.Context, err error, format string, v ...any)

Errorf prints the logging content with [ERRO] header, custom format and newline.

func Errorw added in v0.1.7

func Errorw(ctx context.Context, err error, msg string, fields ...Field)

Errorw prints the logging content with [ERRO] header, custom format and newline.

func Fatalf

func Fatalf(ctx context.Context, err error, format string, v ...any)

Fatalf prints the logging content with [FATA] header, custom format and newline, then exit the current process.

func Fatalw added in v0.1.7

func Fatalw(ctx context.Context, err error, msg string, fields ...Field)

Fatalw prints the logging content with [FATA] header, custom format and newline, then exit the current process.

func Infof

func Infof(ctx context.Context, format string, v ...any)

Infof prints the logging content with [INFO] header, custom format and newline.

func Infow added in v0.1.7

func Infow(ctx context.Context, msg string, fields ...Field)

Info prints the logging content with [INFO] header and newline.

func Panicf

func Panicf(ctx context.Context, err error, format string, v ...any)

Panicf prints the logging content with [PANI] header, custom format and newline, then panics.

func Panicw added in v0.1.7

func Panicw(ctx context.Context, err error, msg string, fields ...Field)

func RemoveHook added in v0.1.9

func RemoveHook(hookName string)

RemoveHook removes a hook from the logger.

func SetCaller added in v0.1.9

func SetCaller(enabled bool)

SetCaller sets the caller.

func SetConfig

func SetConfig(config *Config) error

SetConfig sets the logger configuration.

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 SetFormat

func SetFormat(format string)

SetFormat sets the log format.

func SetLevel added in v0.1.7

func SetLevel(level Level)

SetLevel sets the log level.

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.

func SetStdout added in v0.1.7

func SetStdout(enabled bool)

SetStdout sets the stdout print.

func SetTimeFormat

func SetTimeFormat(timeFormat string)

SetTimeFormat sets the log time format.

func Warnf

func Warnf(ctx context.Context, format string, v ...any)

Warnf prints the logging content with [WARN] header, custom format and newline.

func Warnw added in v0.1.7

func Warnw(ctx context.Context, msg string, fields ...Field)

Warnw prints the logging content with [WARN] header, custom format and newline.

Types

type Config

type Config struct {
	// 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 is the add caller.
	// If true, the caller will be added to the log.
	Caller bool `mconv:"caller"`
	// 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

func ConfigFromMap(m map[string]any) (*Config, error)

ConfigFromMap parses and returns config from given map.

func (*Config) SetConfigWithMap added in v0.1.4

func (c *Config) SetConfigWithMap(configMap map[string]any) error

SetConfigWithMap sets the logger configuration using a map.

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry represents a log entry.

func (*Entry) AddField added in v0.1.8

func (e *Entry) AddField(field Field) *Entry

AddField adds a field to the entry.

func (*Entry) GetContext added in v0.1.8

func (e *Entry) GetContext() context.Context

GetContext returns the context of the entry.

func (*Entry) GetFields added in v0.1.8

func (e *Entry) GetFields() Fields

GetFields returns the fields of the entry.

func (*Entry) GetMsg added in v0.1.8

func (e *Entry) GetMsg() string

GetMsg returns the message of the entry.

func (*Entry) SetContext added in v0.1.8

func (e *Entry) SetContext(ctx context.Context) *Entry

SetContext sets the context of the entry.

func (*Entry) SetFields added in v0.1.9

func (e *Entry) SetFields(fields Fields) *Entry

SetFields sets the fields of the entry.

func (*Entry) SetMsg added in v0.1.8

func (e *Entry) SetMsg(msg string) *Entry

SetMsg sets the message of the entry.

type Field added in v0.1.7

type Field struct {
	Key       string
	Type      zapcore.FieldType
	Integer   int64
	String    string
	Interface any
}

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

func Any(key string, value any) Field

Any takes a key and an arbitrary value and chooses the best way to represent them as a field.

func Bool added in v0.1.7

func Bool(key string, val bool) Field

Bool constructs a field with a boolean value.

func Duration added in v0.1.7

func Duration(key string, val time.Duration) Field

Duration constructs a field with a time.Duration.

func Err added in v0.1.7

func Err(err error) Field

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 Float64 added in v0.1.7

func Float64(key string, val float64) Field

Float64 constructs a field with a float64 value.

func Int added in v0.1.7

func Int(key string, val int) Field

Int constructs a field with an int value.

func Int64 added in v0.1.7

func Int64(key string, val int64) Field

Int64 constructs a field with an int64 value.

func Skip added in v0.1.7

func Skip() Field

Skip constructs a no-op field, which is ignored by the logger.

func String added in v0.1.7

func String(key string, val string) Field

String constructs a field with a string value.

func Time added in v0.1.7

func Time(key string, val time.Time) Field

Time constructs a field with a time.Time value. It's formatted as a floating-point number of seconds since the Unix epoch.

func Uint added in v0.1.7

func Uint(key string, val uint) Field

Uint constructs a field with a uint value.

func Uint64 added in v0.1.7

func Uint64(key string, val uint64) Field

Uint64 constructs a field with a uint64 value.

type Fields added in v0.1.4

type Fields []Field

Fields is a slice of Field.

type Hook

type Hook interface {
	// Name returns the name of the hook.
	Name() string
	// Level returns the level of the hook.
	Levels() []Level
	// Fire is called when the log is written.
	Fire(entry *Entry)
}

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.

const (
	DebugLevel Level = Level(zapcore.DebugLevel)
	InfoLevel  Level = Level(zapcore.InfoLevel)
	WarnLevel  Level = Level(zapcore.WarnLevel)
	ErrorLevel Level = Level(zapcore.ErrorLevel)
	FatalLevel Level = Level(zapcore.FatalLevel)
	PanicLevel Level = Level(zapcore.PanicLevel)
)

func AllLevels added in v0.1.2

func AllLevels() []Level

func GetLevel added in v0.1.7

func GetLevel() Level

GetLevel returns the log level.

func ParseLevel added in v0.1.4

func ParseLevel(level string) (Level, error)

ParseLevel parses a string level and returns the Level value.

func (Level) String added in v0.1.7

func (l Level) String() string

String returns the string representation of the level.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is the struct for logging management.

func DefaultLogger

func DefaultLogger() *Logger

DefaultLogger returns the default logger.

func Instance

func Instance(name ...string) *Logger

Instance returns the logger instance with the specified name.

func New

func New(cfg ...*Config) *Logger

New creates a new Logger instance.

func With added in v0.1.7

func With(fields ...Field) *Logger

With returns a new logger with the added attributes.

func (*Logger) AddHook

func (l *Logger) AddHook(hook Hook) error

AddHook adds a hook to the logger.

func (*Logger) Close added in v0.1.7

func (l *Logger) Close() error

Close closes the logger and its underlying resources.

func (*Logger) Debugf

func (l *Logger) Debugf(ctx context.Context, format string, v ...any)

func (*Logger) Debugw added in v0.1.7

func (l *Logger) Debugw(ctx context.Context, msg string, fields ...Field)

func (*Logger) Errorf

func (l *Logger) Errorf(ctx context.Context, err error, format string, v ...any)

func (*Logger) Errorw added in v0.1.7

func (l *Logger) Errorw(ctx context.Context, err error, msg string, fields ...Field)

func (*Logger) Fatalf

func (l *Logger) Fatalf(ctx context.Context, err error, format string, v ...any)

func (*Logger) Fatalw added in v0.1.7

func (l *Logger) Fatalw(ctx context.Context, err error, msg string, fields ...Field)

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

GetLevel returns the logging level value.

func (*Logger) Infof

func (l *Logger) Infof(ctx context.Context, format string, v ...any)

func (*Logger) Infow added in v0.1.7

func (l *Logger) Infow(ctx context.Context, msg string, fields ...Field)

func (*Logger) Panicf

func (l *Logger) Panicf(ctx context.Context, err error, format string, v ...any)

func (*Logger) Panicw added in v0.1.7

func (l *Logger) Panicw(ctx context.Context, err error, msg string, fields ...Field)

func (*Logger) RemoveHook added in v0.1.7

func (l *Logger) RemoveHook(hookName string)

RemoveHook removes a hook from the logger.

func (*Logger) SetConfig

func (l *Logger) SetConfig(config *Config) error

SetConfig sets the logger configuration.

func (*Logger) SetConfigWithMap

func (l *Logger) SetConfigWithMap(configMap map[string]any) error

SetConfigWithMap sets the logger configuration using a map.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets the logging level.

func (*Logger) Warnf

func (l *Logger) Warnf(ctx context.Context, format string, v ...any)

func (*Logger) Warnw added in v0.1.7

func (l *Logger) Warnw(ctx context.Context, msg string, fields ...Field)

func (*Logger) With added in v0.1.7

func (l *Logger) With(fields ...Field) *Logger

With adds a field to the logger.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL