mklog

package module
v0.0.0-...-504bc65 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 12 Imported by: 0

README

Module mklog

The mklog module provides a toolkit for logging in Go applications with the ability to configure various aspects of log output and formatting.

Key Features:

  • Configuration Flexibility: The module offers a configuration mechanism that allows tuning logging parameters such as debug mode, time format, console output, file output, and more.

  • Log Format Choice: Various log formats are supported, including Plain Text, JSON, XML, YAML, as well as the ability to define a custom format.

  • Ease of Use: A simple interface and the ability to initialize the logger via a configuration file simplify the process of integrating logging into an application.

  • Logging Levels Support: The module provides logging levels such as Debug, Trace, Info, Warning, Error, and Fatal, along with the ability to create custom levels, allowing detailed control over which messages are recorded in the log.

  • Error Stack: The ability to log detailed error information, including call stacks, when using corresponding methods.

  • File-Based Configuration: Easy setup of logging parameters through a configuration file in YAML, JSON, and XML formats, or by using a custom configuration.

This module ensures a reliable and flexible logging mechanism for your application, helping you efficiently monitor and analyze its operation.

Possible Configuration Settings for the mklog Module include:

The module allows you to:

  • Console Output: Enable or disable log output to the console.

    • Debug Mode: Enable or disable debug mode.
    • Date Format: Set the date format for log messages.
    • Detailed Error Output: Enable or disable detailed error output, which includes error details in the stack.
    • Log Formatter Configuration: Set the log formatter for the logger instance.
    • User-Defined Formatter: Set a user-defined log formatter, providing users the ability to define their own format.
    • Set User-Defined Log Level Names: Set custom log level names as provided by the user.
  • File Logging: Enable or disable logging to a file and set the file path and name for recording.

    • Log with Date in File Names: Enable or disable adding the date to log file names.
    • Date Format in File Names: Set the date format used in log file names.
    • Set Storage Path for Files: Set the default path for log files, either using the default or explicitly specifying a path.
    • Set Log File Type: Specify the file type (extension) for logs.

Usage

Instructions on how to use the mklog module can be found in the corresponding wiki of the project.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DateFormat          string `yaml:"dateFormat" json:"dateFormat" xml:"dateFormat"`
	IsConsoleOutput     bool   `yaml:"isConsoleOutput" json:"isConsoleOutput" xml:"isConsoleOutput"`
	DetailedErrorOutput bool   `yaml:"detailedErrorOutput" json:"detailedErrorOutput" xml:"detailedErrorOutput"`

	LogDateFile       bool   `yaml:"logDateFile" json:"logDateFile" xml:"logDateFile"`
	LogDateFileFormat string `yaml:"logDateFileFormat" json:"logDateFileFormat" xml:"logDateFileFormat"`
	LogFilePath       string `yaml:"logFilePath" json:"logFilePath" xml:"logFilePath"`
	LogFileName       string `yaml:"logFileName" json:"logFileName" xml:"logFileName"`
	LogFileType       string `yaml:"logFileType" json:"logFileType" xml:"logFileType"`

	LogFormat  string `json:"logFormat" yaml:"logFormat" xml:"logFormat"`
	UserFormat string `json:"userFormat" yaml:"userFormat" xml:"userFormat"`
	// contains filtered or unexported fields
}

Config is a struct that represents the configuration settings for logging.

type ConfigProvider

type ConfigProvider interface {
	IsDebugMode() bool         // IsDebugMode returns whether debug mode is enabled.
	DateFormat() string        // DateFormat returns the format for log timestamps.
	IsConsoleOutput() bool     // IsConsoleOutput returns whether console output is enabled.
	DetailedErrorOutput() bool // DetailedErrorOutput returns whether detailed error output is enabled.
	LogDateFile() bool         // LogDateFile returns whether log date file is enabled.
	LogDateFileFormat() string // LogDateFileFormat returns the format for the log date file timestamps.
	LogFilePath() string       // LogFilePath returns the path for log files.
	LogFileName() string       // LogFileName returns the name for log files.
	LogFileType() string       // LogFileType returns the type (extension) for log files.
	LogFormat() string         // LogFormat returns the desired log format (e.g., PlainText, JSON, XML, UserDefined).
	UserFormat() string        // UserFormat returns the custom log format if LogFormat is UserDefined.

}

ConfigProvider is an interface that defines methods to retrieve configuration parameters for logging.

type Debugger

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

Debugger is a logging utility that provides various configuration options.

func NewDebugLogger

func NewDebugLogger(moduleName string, submodules ...string) *Debugger

NewDebugLogger creates a new instance of Debugger with the specified module name and optional submodules. Default parameters:

  • logLevel: InfoLevel
  • logFormatter: Default log formatter set to PlainTextFormatter with the date format "02.01.2006"
  • Signal notification: Debugger is notified about OS interrupt signals
  • log.logToFile: Log to file is initially disabled
  • log.logDateFileFormat: Default log file date format set to "02.01.2006"

func SetByConfig

func SetByConfig(path, fileName, moduleName string, submodules ...string) (*Debugger, error)

SetByConfig initializes a Debugger instance using configuration settings from a file. It takes the file path, file name, module name, and submodules (optional) as parameters. The configuration file format is determined by the file extension. Supported formats: YAML (.yaml, .yml), JSON (.json), XML (.xml). Returns a Debugger instance and an error if any.

func SetByConfigProvider

func SetByConfigProvider(provider ConfigProvider, moduleName string, submodules ...string) (*Debugger, error)

SetByConfigProvider initializes a Debugger instance using the provided ConfigProvider.

func (*Debugger) CreateLogFile

func (d *Debugger) CreateLogFile() error

CreateLogFile creates the log file based on the configured settings.

func (*Debugger) Custom

func (d *Debugger) Custom(logLevel LogLevel, msg string, args ...interface{})

Custom outputs a custom log message at the specified log level. It includes the specified message and error and respects the custom log level names.

func (*Debugger) CustomDebug

func (d *Debugger) CustomDebug(logLevel LogLevel, msg string, args ...interface{})

CustomDebug outputs a custom log message if debug mode. It includes the specified message and error and respects the custom log level names.

func (*Debugger) Debug

func (d *Debugger) Debug(msg string, args ...interface{})

Debug outputs a log message at the Debug level with the specified message and error (if any). It prints the log message to the console and, if enabled, to the log file.

func (*Debugger) DebugDetailed

func (d *Debugger) DebugDetailed(msg string, err error)

DebugDetailed outputs a detailed log message at the Debug level. It includes the specified message and error with additional stack trace information. DebugDetailed logs are printed to the console and, if enabled, to the log file.

func (*Debugger) Error

func (d *Debugger) Error(msg string, args ...interface{})

Error outputs a log message at the Error level with the specified message and error (if any). It prints the log message to the console and, if enabled, to the log file.

func (*Debugger) ErrorDetailed

func (d *Debugger) ErrorDetailed(msg string, err error)

ErrorDetailed outputs a detailed log message at the Error level. It includes the specified message and error with additional stack trace information. ErrorDetailed logs are printed to the console and, if enabled, to the log file.

func (*Debugger) Fatal

func (d *Debugger) Fatal(msg string, args ...interface{})

Fatal outputs a log message at the Fatal level with the specified message and error (if any). It prints the log message to the console and, if enabled, to the log file.

func (*Debugger) FatalDetailed

func (d *Debugger) FatalDetailed(msg string, err error)

FatalDetailed outputs a detailed log message at the Fatal level. It includes the specified message and error with additional stack trace information. FatalDetailed logs are printed to the console and, if enabled, to the log file.

func (*Debugger) GetSignalChannel

func (d *Debugger) GetSignalChannel() chan os.Signal

GetSignalChannel returns the signal channel used for interrupt signals.

func (*Debugger) Info

func (d *Debugger) Info(msg string, args ...interface{})

Info outputs a log message at the Info level with the specified message and error (if any). It prints the log message to the console and, if enabled, to the log file.

func (*Debugger) InfoDetailed

func (d *Debugger) InfoDetailed(msg string, err error)

InfoDetailed outputs a detailed log message at the Info level. It includes the specified message and error with additional stack trace information. InfoDetailed logs are printed to the console and, if enabled, to the log file.

func (*Debugger) SetConsoleOutput

func (d *Debugger) SetConsoleOutput(mode bool) *Debugger

SetConsoleOutput enables or disables console output for log messages.

func (*Debugger) SetCustomLogLevelNames

func (d *Debugger) SetCustomLogLevelNames(customLogLevelNames map[LogLevel]string) *Debugger

SetCustomLogLevelNames sets custom log level names provided by the user.

func (*Debugger) SetDateFormat

func (d *Debugger) SetDateFormat(format string) *Debugger

SetDateFormat sets the date format for log messages.

func (*Debugger) SetDebugMode

func (d *Debugger) SetDebugMode(mode bool) *Debugger

SetDebugMode enables or disables debug mode for the Debugger instance.

func (*Debugger) SetDefaultLogPath

func (d *Debugger) SetDefaultLogPath() *Debugger

func (*Debugger) SetDetailedErrorOutput

func (d *Debugger) SetDetailedErrorOutput(enabled bool) *Debugger

SetDetailedErrorOutput enables or disables detailed error output.

func (*Debugger) SetIsLogFile

func (d *Debugger) SetIsLogFile(isLog bool) *Debugger

SetLogFile enables or disables logging to a file and sets the file path and name.

func (*Debugger) SetLogDate

func (d *Debugger) SetLogDate(isLogDate bool) *Debugger

SetLogDate enables or disables the inclusion of the log date in file names.

func (*Debugger) SetLogDateFileNameFormat

func (d *Debugger) SetLogDateFileNameFormat(_type string) *Debugger

func (*Debugger) SetLogDateFormat

func (d *Debugger) SetLogDateFormat(format string) *Debugger

SetLogDateFormat sets the date format for log file names.

func (*Debugger) SetLogFileName

func (d *Debugger) SetLogFileName(filename string) *Debugger

func (*Debugger) SetLogFileType

func (d *Debugger) SetLogFileType(_type string) *Debugger

SetLogFileType sets the log file type.

func (*Debugger) SetLogFormatter

func (d *Debugger) SetLogFormatter(formatter LogFormatter) *Debugger

SetLogFormatter sets the log formatter for the Debugger instance.

func (*Debugger) SetLogPath

func (d *Debugger) SetLogPath(filename string) *Debugger

func (*Debugger) SetUserDefinedFormatter

func (d *Debugger) SetUserDefinedFormatter(formatFunc UserDefinedFormatterFunc) *Debugger

SetUserDefinedFormatter sets a user-defined log formatter function and date format.

func (*Debugger) Trace

func (d *Debugger) Trace(msg string, args ...interface{})

Trace outputs a log message at the Trace level with the specified message and error (if any). It prints the log message to the console and, if enabled, to the log file. Trace logs are only printed if the debug mode is enabled, and the log level is set to Trace.

func (*Debugger) TraceDetailed

func (d *Debugger) TraceDetailed(msg string, err error)

TraceDetailed outputs a detailed log message at the Trace level. It includes the specified message and error with additional stack trace information. TraceDetailed logs are only printed if the debug mode is enabled, and the log level is set to Trace.

func (*Debugger) Warning

func (d *Debugger) Warning(msg string, args ...interface{})

Warning outputs a log message at the Warning level with the specified message and error (if any). It prints the log message to the console and, if enabled, to the log file.

func (*Debugger) WarningDetailed

func (d *Debugger) WarningDetailed(msg string, err error)

WarningDetailed outputs a detailed log message at the Warning level. It includes the specified message and error with additional stack trace information. WarningDetailed logs are printed to the console and, if enabled, to the log file.

type DetailedError

type DetailedError struct {
	Err       error  // Original error.
	StackInfo string // Stack trace information.
}

DetailedError represents an error with additional stack trace information.

func NewDetailedError

func NewDetailedError(err error) DetailedError

NewDetailedError creates a new DetailedError with the given error and captures the stack trace.

func (DetailedError) Error

func (de DetailedError) Error() string

Error returns the string representation of the DetailedError, including the original error and stack trace.

type JSONFormatter

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

JSONFormatter is a LogFormatter implementation that formats log messages in JSON.

func (JSONFormatter) Format

func (f JSONFormatter) Format(logMessage string, logLevel string, moduleName string, submodules []string, timestamp string) string

Format formats the log message in JSON.

type LogFormatter

type LogFormatter interface {
	// Format formats the log message using the specified parameters and returns the formatted log string.
	Format(logMessage string, logLevel string, moduleName string, submodules []string, timestamp string) string
}

LogFormatter is an interface that defines the methods required to format log messages.

type LogLevel

type LogLevel int

LogLevel represents the severity levels of log messages.

const (
	DebugLevel   LogLevel = iota // DebugLevel represents debug log messages.
	InfoLevel                    // InfoLevel represents informational log messages.
	WarningLevel                 // WarningLevel represents warning log messages.
	ErrorLevel                   // ErrorLevel represents error log messages.
	FatalLevel                   // FatalLevel represents fatal log messages.
	TraceLevel                   // TraceLevel represents trace log messages.
)

type PlainTextFormatter

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

PlainTextFormatter is a LogFormatter implementation that formats log messages in plain text.

func (PlainTextFormatter) Format

func (f PlainTextFormatter) Format(logMessage string, logLevel string, moduleName string, submodules []string, timestamp string) string

Format formats the log message in plain text.

type UserDefinedFormatter

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

UserDefinedFormatter is a LogFormatter implementation that allows users to define their log message formatting.

func (UserDefinedFormatter) Format

func (f UserDefinedFormatter) Format(logMessage string, logLevel string, moduleName string, submodules []string, timestamp string) string

Format formats the log message using a user-defined function.

type UserDefinedFormatterFunc

type UserDefinedFormatterFunc func(logMessage string, logLevel string, moduleName string, submodules []string, timestamp string) string

UserDefinedFormatterFunc is a function type for user-defined log message formatting.

type XMLFormatter

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

XMLFormatter is a LogFormatter implementation that formats log messages in XML.

func (XMLFormatter) Format

func (f XMLFormatter) Format(logMessage string, logLevel string, moduleName string, submodules []string, timestamp string) string

Format formats the log message in XML.

type YAMLFormatter

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

YAMLFormatter is a LogFormatter implementation that formats log messages in YAML.

func (YAMLFormatter) Format

func (f YAMLFormatter) Format(logMessage string, logLevel string, moduleName string, submodules []string, timestamp string) string

Format formats the log message in YAML.

Jump to

Keyboard shortcuts

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