mlog

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2025 License: MIT Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DefaultName = "default"
)

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, v ...interface{})

Debug prints the logging content with [DEBU] header and newline.

func Debugf

func Debugf(ctx context.Context, format string, v ...interface{})

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

func Error

func Error(ctx context.Context, v ...interface{})

Error prints the logging content with [ERRO] header and newline. It also prints caller stack info if stack feature is enabled.

func Errorf

func Errorf(ctx context.Context, format string, v ...interface{})

Errorf prints the logging content with [ERRO] header, custom format and newline. It also prints caller stack info if stack feature is enabled.

func Fatal

func Fatal(ctx context.Context, v ...interface{})

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

func Fatalf

func Fatalf(ctx context.Context, format string, v ...interface{})

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

func Info

func Info(ctx context.Context, v ...interface{})

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

func Infof

func Infof(ctx context.Context, format string, v ...interface{})

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

func Panic

func Panic(ctx context.Context, v ...interface{})

Panic prints the logging content with [PANI] header and newline, then panics.

func Panicf

func Panicf(ctx context.Context, format string, v ...interface{})

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

func Print

func Print(ctx context.Context, v ...interface{})

Print prints `v` with newline using fmt.Sprintln. The parameter `v` can be multiple variables.

func Printf

func Printf(ctx context.Context, format string, v ...interface{})

Printf prints `v` with format `format` using fmt.Sprintf. The parameter `v` can be multiple variables.

func SetAutoClean

func SetAutoClean(days int)

SetAutoClean sets the number of days to keep log files.

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 SetFile

func SetFile(file string)

SetFile sets the log file name, supporting date patterns.

func SetFormat

func SetFormat(format string)

SetFormat sets the log format.

func SetPath

func SetPath(path string)

SetPath sets the log file path.

func SetStdoutPrint

func SetStdoutPrint(enabled bool)

SetStdoutPrint sets the stdout print.

func SetTimeFormat

func SetTimeFormat(timeFormat string)

SetTimeFormat sets the log time format.

func Warn

func Warn(ctx context.Context, v ...interface{})

Warn prints the logging content with [WARN] header and newline. It also prints caller stack info if stack feature is enabled.

func Warnf

func Warnf(ctx context.Context, format string, v ...interface{})

Warnf prints the logging content with [WARN] header, custom format and newline. It also prints caller stack info if stack feature is enabled.

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.
	Format string `mconv:"format"`
	// Path 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 {
	// log level
	Level Level
	// log message
	Message string
	// log fields
	Data map[string]interface{}
	// Context
	Context context.Context
	// contains filtered or unexported fields
}

Entry represents a log entry.

func (*Entry) Raw added in v0.1.4

func (e *Entry) Raw() *logrus.Entry

Raw returns the raw logrus entry.

type Fields added in v0.1.4

type Fields map[string]any

Fields is a map of string keys to any values.

type Hook

type Hook interface {
	// Levels returns the log levels that the hook applies to.
	Levels() []Level
	// Fire executes the hook when a log entry is written.
	Fire(entry *Entry) error
}

Hook defines the log hook interface.

type ILogger

type ILogger interface {
	Print(ctx context.Context, v ...any)                 // Print logs a message at level Info.
	Printf(ctx context.Context, format string, v ...any) // Printf logs a message at level Info.
	Debug(ctx context.Context, v ...any)                 // Debug logs a message at level Debug.
	Debugf(ctx context.Context, format string, v ...any) // Debugf logs a message at level Debug.
	Info(ctx context.Context, v ...any)                  // Info logs a message at level Info.
	Infof(ctx context.Context, format string, v ...any)  // Infof logs a message at level Info.
	Warn(ctx context.Context, v ...any)                  // Warn logs a message at level Warn.
	Warnf(ctx context.Context, format string, v ...any)  // Warnf logs a message at level Warn.
	Error(ctx context.Context, v ...any)                 // Error logs a message at level Error.
	Errorf(ctx context.Context, format string, v ...any) // Errorf logs a message at level Error.
	Fatal(ctx context.Context, v ...any)                 // Fatal logs a message at level Fatal.
	Fatalf(ctx context.Context, format string, v ...any) // Fatalf logs a message at level Fatal.
	Panic(ctx context.Context, v ...any)                 // Panic logs a message at level Panic.
	Panicf(ctx context.Context, format string, v ...any) // Panicf logs a message at level Panic.
}

ILogger is the interface for the logger.

type Level

type Level int
const (
	PanicLevel Level = iota
	FatalLevel
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
)

func AllLevels added in v0.1.2

func AllLevels() []Level

func ParseLevel added in v0.1.4

func ParseLevel(level string) (Level, error)

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 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 (*Logger) AddHook

func (l *Logger) AddHook(hook Hook)

AddHook adds a log hook.

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, v ...any)

Debug prints the logging content with [DEBUG] header and newline.

func (*Logger) Debugf

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

Debugf prints the logging content with [DEBUG] header and format `format`.

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, v ...any)

Error prints the logging content with [ERROR] header and newline.

func (*Logger) Errorf

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

Errorf prints the logging content with [ERROR] header and format `format`.

func (*Logger) Fatal

func (l *Logger) Fatal(ctx context.Context, v ...any)

Fatal prints the logging content with [FATAL] header and newline.

func (*Logger) Fatalf

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

Fatalf prints the logging content with [FATAL] header and format `format`.

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

GetLevel returns the logging level value.

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, v ...any)

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

func (*Logger) Infof

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

Infof prints the logging content with [INFO] header and format `format`.

func (*Logger) Panic

func (l *Logger) Panic(ctx context.Context, v ...any)

Panic prints the logging content with [PANIC] header and newline.

func (*Logger) Panicf

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

Panicf prints the logging content with [PANIC] header and format `format`.

func (*Logger) Print

func (l *Logger) Print(ctx context.Context, v ...any)

Print prints `v` with newline using fmt.Sprintln.

func (*Logger) Printf

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

Printf prints `v` with format `format` using fmt.Sprintf.

func (*Logger) RemoveHookByType

func (l *Logger) RemoveHookByType(hookType Hook)

RemoveHookByType removes hooks of a specific type. It compares the type of the hook with the provided hook type.

func (*Logger) SetConfig

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

func (*Logger) SetConfigWithMap

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

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets the logging level.

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, v ...any)

Warn prints the logging content with [WARN] header and newline.

func (*Logger) Warnf

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

Warnf prints the logging content with [WARN] header and format `format`.

func (*Logger) WithComponent added in v0.1.4

func (l *Logger) WithComponent(component string) *Logger

WithComponent adds a component field to the logger.

func (*Logger) WithField added in v0.1.4

func (l *Logger) WithField(key string, value any) *Logger

WithField adds a field to the logger.

func (*Logger) WithFields added in v0.1.4

func (l *Logger) WithFields(fields logrus.Fields) *Logger

WithFields adds multiple fields to the logger.

Jump to

Keyboard shortcuts

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