Documentation ¶
Index ¶
- Constants
- Variables
- type AttrFormat
- type FileOption
- type FileSizeMode
- type FileTimeMode
- type LEVELTYPE
- type LevelOption
- type LogContext
- type Logging
- func Debug(v ...any) *Logging
- func Debugf(format string, v ...any) *Logging
- func Error(v ...any) *Logging
- func Errorf(format string, v ...any) *Logging
- func Fatal(v ...any) *Logging
- func Fatalf(format string, v ...any) *Logging
- func GetStaticLogger() *Logging
- func Info(v ...any) *Logging
- func Infof(format string, v ...any) *Logging
- func NewLogger() (log *Logging)
- func SetConsole(on bool) *Logging
- func SetFormat(format _FORMAT) *Logging
- func SetFormatter(formatter string) *Logging
- func SetGzipOn(is bool) (l *Logging)
- func SetLevel(level LEVELTYPE) *Logging
- func SetLevelOption(level LEVELTYPE, option *LevelOption) *Logging
- func SetOption(option *Option) *Logging
- func SetRollingByTime(fileDir, fileName string, mode _MODE_TIME) (l *Logging, err error)
- func SetRollingDaily(fileDir, fileName string) (l *Logging, err error)
- func SetRollingFile(fileDir, fileName string, maxFileSize int64, unit _UNIT) (l *Logging, err error)
- func SetRollingFileLoop(fileDir, fileName string, maxFileSize int64, unit _UNIT, maxFileNum int) (l *Logging, err error)
- func Warn(v ...any) *Logging
- func Warnf(format string, v ...any) *Logging
- func (t *Logging) Debug(v ...any) *Logging
- func (t *Logging) Debugf(format string, v ...any) *Logging
- func (t *Logging) Error(v ...any) *Logging
- func (t *Logging) Errorf(format string, v ...any) *Logging
- func (t *Logging) Fatal(v ...any) *Logging
- func (t *Logging) Fatalf(format string, v ...any) *Logging
- func (t *Logging) Info(v ...any) *Logging
- func (t *Logging) Infof(format string, v ...any) *Logging
- func (t *Logging) SetConsole(_isConsole bool) *Logging
- func (t *Logging) SetFormat(format _FORMAT) *Logging
- func (t *Logging) SetFormatter(formatter string) *Logging
- func (t *Logging) SetGzipOn(is bool) *Logging
- func (t *Logging) SetLevel(level LEVELTYPE) *Logging
- func (t *Logging) SetLevelOption(level LEVELTYPE, option *LevelOption) *Logging
- func (t *Logging) SetOption(option *Option) *Logging
- func (t *Logging) SetRollingByTime(fileDir, fileName string, mode _MODE_TIME) (l *Logging, err error)
- func (t *Logging) SetRollingDaily(fileDir, fileName string) (*Logging, error)
- func (t *Logging) SetRollingFile(fileDir, fileName string, maxFileSize int64, unit _UNIT) (l *Logging, err error)
- func (t *Logging) SetRollingFileLoop(fileDir, fileName string, maxFileSize int64, unit _UNIT, maxBackup int) (l *Logging, err error)
- func (t *Logging) Warn(v ...any) *Logging
- func (t *Logging) Warnf(format string, v ...any) *Logging
- func (t *Logging) Write(bs []byte) (n int, err error)
- func (t *Logging) WriteBin(bs []byte) (bakfn string, err error)
- type Option
Constants ¶
const ( KB _UNIT = 1 << (iota * 10) MB GB TB )
const ( MODE_HOUR _MODE_TIME = 1 MODE_DAY _MODE_TIME = 2 MODE_MONTH _MODE_TIME = 3 )
const ( // FORMAT_NANO // // no format, Only log content is printed // 无其他格式,只打印日志内容 FORMAT_NANO _FORMAT = 64 // FORMAT_LONGFILENAME // // full file name and line number // 长文件名(文件绝对路径)及行数 FORMAT_LONGFILENAME = _FORMAT(8) // FORMAT_SHORTFILENAME // // final file name element and line number // 短文件名及行数 FORMAT_SHORTFILENAME = _FORMAT(16) // FORMAT_RELATIVEFILENAME // // relative file name element and line number // 相对路径文件名及行数 FORMAT_RELATIVEFILENAME = _FORMAT(256) // FORMAT_DATE // // the date in the local time zone: 2009/01/23 // 日期时间精确到天 FORMAT_DATE = _FORMAT(1) // FORMAT_TIME // // the time in the local time zone: 01:23:23 // 时间精确到秒 FORMAT_TIME = _FORMAT(2) // FORMAT_MICROSECONDS // // microsecond resolution: 01:23:23.123123. // 时间精确到微秒 FORMAT_MICROSECONDS = _FORMAT(4) // FORMAT_LEVELFLAG // //Log level flag. e.g. [DEBUG],[INFO],[WARN],[ERROR],[FATAL] // 日志级别表示 FORMAT_LEVELFLAG = _FORMAT(32) // FORMAT_FUNC // // the func of caller // 调用的函数名 FORMAT_FUNC = _FORMAT(128) )
const (
VERSION string = "0.1.2"
)
Variables ¶
var TIME_DEVIATION time.Duration
Functions ¶
This section is empty.
Types ¶
type AttrFormat ¶ added in v0.1.2
type AttrFormat struct { // SetLevelFmt defines a function to format log levels. // This function receives a log level of type LEVELTYPE and returns a formatted string. // The string represents the level prefix in log entries, such as "DEBUG:" for debug level or "FATAL:" for fatal level. // // Example: // SetLevelFmt: func(level LEVELTYPE) string { // if level == LEVEL_DEBUG { // return "DEBUG:" // } // return "INFO:" // } SetLevelFmt func(level LEVELTYPE) string // SetTimeFmt defines a function to format timestamps for log entries. // This function returns three strings representing different components of a timestamp. // It allows custom handling of dates, times, or other time-based information. // // Example: // SetTimeFmt: func() (string, string, string) { // currentTime := time.Now().Format("2006-01-02 15:04:05") // return currentTime, "", "" // } SetTimeFmt func() (string, string, string) // SetBodyFmt defines a function to customize the format of log message bodies. // This function receives the log level and the message body in byte slice format, allowing // modifications such as adding colors, handling line breaks, or appending custom suffixes. // // Example: // SetBodyFmt: func(level LEVELTYPE, msg []byte) []byte { // if level == LEVEL_ERROR { // return append([]byte("ERROR MESSAGE: "), msg...) // } // return msg // } SetBodyFmt func(level LEVELTYPE, msg []byte) []byte }
AttrFormat defines a set of customizable formatting functions for log entries. This structure allows users to specify custom formats for log levels, timestamps, and message bodies, enabling highly flexible log formatting.
Example usage:
attrFormat := &AttrFormat{ SetLevelFmt: func(level LEVELTYPE) string { switch level { case LEVEL_DEBUG: return "DEBUG:" case LEVEL_INFO: return "INFO :" case LEVEL_WARN: return "WARN :" case LEVEL_ERROR: return "ERROR:" case LEVEL_FATAL: return "FATAL:" default: return "UNKNOWN:" } }, SetTimeFmt: func() (string, string, string) { now := time.Now().Format("2006-01-02 15:04:05") return now, "", "" }, SetBodyFmt: func(level LEVELTYPE, msg []byte) []byte { switch level { case LEVEL_DEBUG: return append([]byte("\033[34m"), append(msg, '\033', '[', '0', 'm')...) // Blue for DEBUG case LEVEL_INFO: return append([]byte("\033[32m"), append(msg, '\033', '[', '0', 'm')...) // Green for INFO case LEVEL_WARN: return append([]byte("\033[33m"), append(msg, '\033', '[', '0', 'm')...) // Yellow for WARN case LEVEL_ERROR: return append([]byte("\033[31m"), append(msg, '\033', '[', '0', 'm')...) // Red for ERROR case LEVEL_FATAL: return append([]byte("\033[41m"), append(msg, '\033', '[', '0', 'm')...) // Red background for FATAL default: return msg } }, }
type FileOption ¶ added in v0.0.3
type FileSizeMode ¶ added in v0.0.3
func (*FileSizeMode) Compress ¶ added in v0.0.3
func (f *FileSizeMode) Compress() bool
func (*FileSizeMode) Cutmode ¶ added in v0.0.3
func (f *FileSizeMode) Cutmode() _CUTMODE
func (*FileSizeMode) FilePath ¶ added in v0.0.3
func (f *FileSizeMode) FilePath() string
func (*FileSizeMode) MaxBuckup ¶ added in v0.0.3
func (f *FileSizeMode) MaxBuckup() int
func (*FileSizeMode) MaxSize ¶ added in v0.0.3
func (f *FileSizeMode) MaxSize() uint64
func (*FileSizeMode) TimeMode ¶ added in v0.0.3
func (f *FileSizeMode) TimeMode() _MODE_TIME
type FileTimeMode ¶ added in v0.0.3
func (*FileTimeMode) Compress ¶ added in v0.0.3
func (f *FileTimeMode) Compress() bool
func (*FileTimeMode) Cutmode ¶ added in v0.0.3
func (f *FileTimeMode) Cutmode() _CUTMODE
func (*FileTimeMode) FilePath ¶ added in v0.0.3
func (f *FileTimeMode) FilePath() string
func (*FileTimeMode) MaxBuckup ¶ added in v0.0.3
func (f *FileTimeMode) MaxBuckup() int
func (*FileTimeMode) MaxSize ¶ added in v0.0.3
func (f *FileTimeMode) MaxSize() uint64
func (*FileTimeMode) TimeMode ¶ added in v0.0.3
func (f *FileTimeMode) TimeMode() _MODE_TIME
type LEVELTYPE ¶ added in v0.1.2
type LEVELTYPE int8
const ( // LEVEL_ALL is the lowest level,If the log level is this level, logs of other levels can be printed // 日志级别:ALL 打印所有日志 LEVEL_ALL LEVELTYPE = iota // LEVEL_DEBUG debug log level // 日志级别:DEBUG 小于INFO LEVEL_DEBUG // LEVEL_INFO info log level // 日志级别:INFO 小于 WARN LEVEL_INFO // LEVEL_WARN warn log level // 日志级别:WARN 小于 ERROR LEVEL_WARN // LEVEL_ERROR error log level // 日志级别:ERROR 小于 FATAL LEVEL_ERROR // LEVEL_FATAL fatal log level // 日志级别:FATAL 小于 OFF LEVEL_FATAL // LEVEL_OFF means none of the logs can be printed // 日志级别:off 不打印任何日志 LEVEL_OFF )
type LevelOption ¶ added in v0.1.0
type LevelOption struct { Format _FORMAT // Log format. Formatter string // Formatting string for customizing the log output format. }
type LogContext ¶ added in v0.1.0
type Logging ¶
type Logging struct {
// contains filtered or unexported fields
}
Logging is the primary data structure for configuring and managing logging behavior.
func Debug ¶
Debug logs a message at the DEBUG level using the default logging instance. Accepts any number of arguments to format the log entry.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: A Logging instance for possible further usage.
func Debugf ¶ added in v0.1.2
Debugf logs a formatted message at the DEBUG level using the default logging instance. Takes a format string followed by variadic arguments to be formatted.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: A Logging instance for possible further usage.
func Error ¶
Error logs a message at the ERROR level using the default logging instance. Accepts any number of arguments to format the log entry.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: A Logging instance for possible further usage.
func Errorf ¶ added in v0.1.2
Errorf logs a formatted message at the ERROR level using the default logging instance. Takes a format string followed by variadic arguments to be formatted.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: A Logging instance for possible further usage.
func Fatal ¶
Fatal logs a message at the FATAL level using the default logging instance and may terminate the application. Accepts any number of arguments to format the log entry.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: A Logging instance for possible further usage.
func Fatalf ¶ added in v0.1.2
Fatalf logs a formatted message at the FATAL level using the default logging instance and may terminate the application. Takes a format string followed by variadic arguments to be formatted.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: A Logging instance for possible further usage.
func GetStaticLogger ¶
func GetStaticLogger() *Logging
GetStaticLogger return the default log object 获得全局Logger对象
func Info ¶
Info logs a message at the INFO level using the default logging instance. Accepts any number of arguments to format the log entry.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: A Logging instance for possible further usage.
func Infof ¶ added in v0.1.2
Infof logs a formatted message at the INFO level using the default logging instance. Takes a format string followed by variadic arguments to be formatted.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: A Logging instance for possible further usage.
func NewLogger ¶
func NewLogger() (log *Logging)
NewLogger creates and returns a new instance of the Logging struct. This function initializes a Logging object with default values or specific configurations as needed.
func SetConsole ¶
SetConsole print logs on the console or not. default true
func SetFormat ¶
func SetFormat(format _FORMAT) *Logging
SetFormat sets the logging format to the specified format type.
设置打印格式: FORMAT_LEVELFLAG | FORMAT_SHORTFILENAME | FORMAT_DATE | FORMAT_TIME
Parameters:
- format: The desired format for log entries, represented by the _FORMAT type.
Returns:
- *Logging: A Logging instance to enable method chaining.
func SetFormatter ¶ added in v0.0.3
SetFormatter specifies a custom format string for the logging output.
设置输出格式,默认: "{level}{time} {file} {message}\n"
Parameters:
- formatter: A string defining the format for log entries, allowing custom log entry layouts.
Returns:
- *Logging: A Logging instance to enable method chaining.
func SetGzipOn ¶
SetGzipOn when set true, the specified backup file of both SetRollingFile and SetRollingFileLoop will be save as a compressed file
Use SetOption() instead.
func SetLevel ¶
SetLevel sets the logging level to the specified level type.
设置控制台日志级别,默认 LEVEL_ALL, 其他: LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARN
Parameters:
- level: The logging level (e.g., LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARN), represented by the LEVELTYPE type.
Returns:
- *Logging: A Logging instance to enable method chaining.
func SetLevelOption ¶ added in v0.1.0
func SetLevelOption(level LEVELTYPE, option *LevelOption) *Logging
func SetOption ¶ added in v0.0.3
SetOption 配置对象
e.g.
SetOption(&Option{Level: LEVEL_DEBUG, Console: true, FileOption: &FileSizeMode{Filename: "test.log", Maxsize: 500, Maxbuckup: 3, IsCompress: false}})
func SetRollingByTime ¶
SetRollingByTime like SetRollingDaily,but supporte hourly backup ,dayly backup and monthly backup mode : MODE_HOUR MODE_DAY MODE_MONTH
Use SetOption() instead.
func SetRollingDaily ¶
SetRollingDaily yesterday's log data is backed up to a specified log file each day Parameters:
- fileDir :directory where log files are stored, If it is the current directory, you also can set it to ""
- fileName : log file name
Use SetOption() instead.
func SetRollingFile ¶
func SetRollingFile(fileDir, fileName string, maxFileSize int64, unit _UNIT) (l *Logging, err error)
SetRollingFile when the log file(fileDir+`\`+fileName) exceeds the specified size(maxFileSize), it will be backed up with a specified file name Parameters:
- fileDir :directory where log files are stored, If it is the current directory, you also can set it to ""
- fileName : log file name
- maxFileSize : maximum size of a log file
- unit : size unit : KB,MB,GB,TB
Use SetOption() instead.
func SetRollingFileLoop ¶
func SetRollingFileLoop(fileDir, fileName string, maxFileSize int64, unit _UNIT, maxFileNum int) (l *Logging, err error)
SetRollingFileLoop like SetRollingFile,but only keep (maxFileNum) current files - maxFileNum : the number of files that are retained
Use SetOption() instead.
func Warn ¶
Warn logs a message at the WARN level using the default logging instance. Accepts any number of arguments to format the log entry.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: A Logging instance for possible further usage.
func Warnf ¶ added in v0.1.2
Warnf logs a formatted message at the WARN level using the default logging instance. Takes a format string followed by variadic arguments to be formatted.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: A Logging instance for possible further usage.
func (*Logging) Debug ¶
Debug logs a message at the DEBUG level. Accepts any number of arguments to format the log entry. Returns the Logging instance for method chaining.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: The current Logging instance for chaining.
func (*Logging) Debugf ¶ added in v0.1.2
Debugf logs a formatted message at the DEBUG level. Takes a format string followed by variadic arguments to be formatted. Returns the Logging instance for method chaining.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: The current Logging instance for chaining.
func (*Logging) Error ¶
Error logs a message at the ERROR level. Accepts any number of arguments to format the log entry. Returns the Logging instance for method chaining.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: The current Logging instance for chaining.
func (*Logging) Errorf ¶ added in v0.1.2
Errorf logs a formatted message at the ERROR level. Takes a format string followed by variadic arguments to be formatted. Returns the Logging instance for method chaining.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: The current Logging instance for chaining.
func (*Logging) Fatal ¶
Fatal logs a message at the FATAL level and may terminate the application. Accepts any number of arguments to format the log entry. Returns the Logging instance for method chaining.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: The current Logging instance for chaining.
func (*Logging) Fatalf ¶ added in v0.1.2
Fatalf logs a formatted message at the FATAL level and may terminate the application. Takes a format string followed by variadic arguments to be formatted. Returns the Logging instance for method chaining.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: The current Logging instance for chaining.
func (*Logging) Info ¶
Info logs a message at the INFO level. Accepts any number of arguments to format the log entry. Returns the Logging instance for method chaining.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: The current Logging instance for chaining.
func (*Logging) Infof ¶ added in v0.1.2
Infof logs a formatted message at the INFO level. Takes a format string followed by variadic arguments to be formatted. Returns the Logging instance for method chaining.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: The current Logging instance for chaining.
func (*Logging) SetConsole ¶
SetConsole sets the flag to determine whether log messages should also be output to the console. This method modifies the _isConsole field of the Logging struct and returns a pointer to the Logging instance for method chaining.
func (*Logging) SetFormat ¶
SetFormat sets the logging format to the specified format type.
Parameters:
- format: The desired format for log entries, represented by the _FORMAT type.
Returns:
- *Logging: A Logging instance to enable method chaining.
func (*Logging) SetFormatter ¶ added in v0.0.3
SetFormatter specifies a custom format string for the logging output.
default: "{level}{time} {file} {message}\n"
Parameters:
- formatter: A string defining the format for log entries, allowing custom log entry layouts.
Returns:
- *Logging: A Logging instance to enable method chaining.
func (*Logging) SetLevel ¶
SetLevel sets the logging level to the specified level type.
Parameters:
- level: The logging level (e.g., DEBUG, INFO, WARN), represented by the LEVELTYPE type.
Returns:
- *Logging: A Logging instance to enable method chaining.
func (*Logging) SetLevelOption ¶ added in v0.1.0
func (t *Logging) SetLevelOption(level LEVELTYPE, option *LevelOption) *Logging
func (*Logging) SetOption ¶ added in v0.0.3
SetOption applies the configuration options specified in the Option struct to the Logging instance. This method updates the fields of the Logging struct according to the provided Option and returns a pointer to the Logging instance for method chaining.
e.g.
SetOption(&Option{Level: LEVEL_DEBUG, Console: true, FileOption: &FileSizeMode{Filename: "test.log", Maxsize: 500, Maxbuckup: 3, IsCompress: false}})
func (*Logging) SetRollingByTime ¶
func (t *Logging) SetRollingByTime(fileDir, fileName string, mode _MODE_TIME) (l *Logging, err error)
SetRollingByTime
指定按 小时,天,月 分割日志文件 fileDir 日志文件夹路径 fileName 日志文件名 mode 指定 小时,天,月
func (*Logging) SetRollingDaily ¶
SetRollingDaily
Use SetOption() instead. 按日期分割日志文件 fileDir 日志文件夹路径 fileName 日志文件名
func (*Logging) SetRollingFile ¶
func (t *Logging) SetRollingFile(fileDir, fileName string, maxFileSize int64, unit _UNIT) (l *Logging, err error)
SetRollingFile
Use SetOption() instead. 按日志文件大小分割日志文件 fileDir 日志文件夹路径 fileName 日志文件名 maxFileSize 日志文件大小最大值 unit 日志文件大小单位
func (*Logging) SetRollingFileLoop ¶
func (t *Logging) SetRollingFileLoop(fileDir, fileName string, maxFileSize int64, unit _UNIT, maxBackup int) (l *Logging, err error)
SetRollingFileLoop
Use SetOption() instead. 按日志文件大小分割日志文件,指定保留的最大日志文件数 fileDir 日志文件夹路径 fileName 日志文件名 maxFileSize 日志文件大小最大值 unit 日志文件大小单位 maxFileNum 留的日志文件数
func (*Logging) Warn ¶
Warn logs a message at the WARN level. Accepts any number of arguments to format the log entry. Returns the Logging instance for method chaining.
Parameters:
- v: Variadic arguments to be logged.
Returns:
- *Logging: The current Logging instance for chaining.
func (*Logging) Warnf ¶ added in v0.1.2
Warnf logs a formatted message at the WARN level. Takes a format string followed by variadic arguments to be formatted. Returns the Logging instance for method chaining.
Parameters:
- format: The format string for the log entry.
- v: Variadic arguments to be formatted according to the format string.
Returns:
- *Logging: The current Logging instance for chaining.
type Option ¶ added in v0.0.3
type Option struct { Level LEVELTYPE // Log level, e.g., DEBUG, INFO, WARN, ERROR, etc. Console bool // Whether to also output logs to the console. Format _FORMAT // Log format. Formatter string // Formatting string for customizing the log output format. FileOption FileOption // File-specific options for the log handler. Stacktrace LEVELTYPE // Log level, e.g., DEBUG, INFO, WARN, ERROR, etc. // CustomHandler // // When customHandler returns false, the println function returns without executing further prints. Returning true allows the subsequent print operations to continue. // // customHandler返回false时,println函数返回,不再执行后续的打印,返回true时,继续执行后续打印。 CustomHandler func(lc *LogContext) bool // Custom log handler function allowing users to define additional log processing logic. AttrFormat *AttrFormat }
Option represents a configuration option for the Logging struct. It includes various settings such as log level, console output, format, formatter, file options, and a custom handler.