mlog

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2025 License: MIT Imports: 18 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 IsDatePattern

func IsDatePattern(pattern string) bool

IsDatePattern checks if a file pattern contains date placeholders.

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      Level    `json:"level"`
	Path       string   `json:"path"`
	File       string   `json:"file"`
	TimeFormat string   `json:"time_format"`
	Format     string   `json:"format"`
	Stdout     bool     `json:"stdout"`
	AutoClean  int      `json:"auto_clean"`
	CtxKeys    []string `json:"ctx_keys"`
}

func ConfigFromMap added in v0.1.2

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

ConfigFromMap parses and returns config from given map.

func DefaultConfig

func DefaultConfig() Config

type Entry

type Entry struct {
	// log level
	Level Level
	// log message
	Message string
	// log fields
	Data map[string]interface{}
	// contains filtered or unexported fields
}

Entry represents a log entry.

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 (
	DebugLevel Level = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
	PanicLevel
)

func AllLevels added in v0.1.2

func AllLevels() []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() *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) SetAutoClean

func (l *Logger) SetAutoClean(autoClean int)

SetAutoClean sets the auto clean days.

func (*Logger) SetConfig

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

func (*Logger) SetConfigWithMap

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

SetConfigWithMap sets the logger configuration using a map.

func (*Logger) SetCtxKeys

func (l *Logger) SetCtxKeys(keys []string)

SetCtxKeys sets the context keys to extract.

func (*Logger) SetFile

func (l *Logger) SetFile(file string)

SetFile sets the log file name.

func (*Logger) SetFormat

func (l *Logger) SetFormat(format string)

SetFormat sets the log format.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets the logging level.

func (*Logger) SetPath

func (l *Logger) SetPath(path string)

SetPath sets the log file path.

func (*Logger) SetStdoutPrint

func (l *Logger) SetStdoutPrint(enabled bool)

SetStdoutPrint sets the stdout print.

func (*Logger) SetTimeFormat

func (l *Logger) SetTimeFormat(timeFormat string)

SetTimeFormat sets the log time format.

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`.

Jump to

Keyboard shortcuts

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