Documentation
¶
Overview ¶
Package logger is a goroutine-safe logging facility which writes logs with different severity levels to files, console, or both. Logs with different severity levels are written to different logfiles.
It's recommended to use logger.Init to create a global Logger object, then use logger.Info/Infof/Warn/Warnf... to write logs.
logger.New can be use to create as many Logger objects as desired if in need.
For a quick reference about this package's features and performance, please refer to the associated README.md.(https://github.com/antigloss/go/blob/master/logger/README.md)
Index ¶
- Constants
- func Error(args ...interface{})
- func Errorf(format string, args ...interface{})
- func Fatal(args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Info(args ...interface{})
- func Infof(format string, args ...interface{})
- func Init(cfg *Config) (err error)
- func Panic(args ...interface{})
- func Panicf(format string, args ...interface{})
- func SetLogLevel(logLevel LogLevel)
- func Trace(args ...interface{})
- func Tracef(format string, args ...interface{})
- func Warn(args ...interface{})
- func Warnf(format string, args ...interface{})
- type Config
- type ControlFlag
- type LogDest
- type LogLevel
- type Logger
- func (l *Logger) Close() error
- func (l *Logger) Error(args ...interface{})
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) Fatal(args ...interface{})
- func (l *Logger) Fatalf(format string, args ...interface{})
- func (l *Logger) Info(args ...interface{})
- func (l *Logger) Infof(format string, args ...interface{})
- func (l *Logger) Panic(args ...interface{})
- func (l *Logger) Panicf(format string, args ...interface{})
- func (l *Logger) SetLogLevel(logLevel LogLevel)
- func (l *Logger) Trace(args ...interface{})
- func (l *Logger) Tracef(format string, args ...interface{})
- func (l *Logger) Warn(args ...interface{})
- func (l *Logger) Warnf(format string, args ...interface{})
Examples ¶
Constants ¶
const (
LogDestBoth = LogDestFile | LogDestConsole // Write logs both to files and console.
)
Variables ¶
This section is empty.
Functions ¶
func Error ¶
func Error(args ...interface{})
Error uses the global Logger object created by Init to write a log with error level.
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf uses the global Logger object created by Init to write a log with error level.
func Fatal ¶
func Fatal(args ...interface{})
Fatal uses the global Logger object created by Init to write a log with fatal level followed by a call to os.Exit(-1).
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf uses the global Logger object created by Init to write a log with fatal level followed by a call to os.Exit(-1).
func Info ¶
func Info(args ...interface{})
Info uses the global Logger object created by Init to write a log with info level.
func Infof ¶
func Infof(format string, args ...interface{})
Infof uses the global Logger object created by Init to write a log with info level.
func Init ¶
Init is used to create the global Logger object with cfg. It must be called once and only once before any other function backed by the global Logger object can be used. It returns nil if all goes well, otherwise it returns the corresponding error.
Example (GlobalLoggerObject) ¶
This example shows how to write logs with the global Logger object.
package main import ( "github.com/antigloss/go/logger" ) func main() { // Create the global Logger object err := logger.Init(&logger.Config{ LogDir: "./logs", LogFileMaxSize: 200, LogFileMaxNum: 500, LogFileNumToDel: 50, LogLevel: logger.LogLevelInfo, LogDest: logger.LogDestFile, Flag: logger.ControlFlagLogLineNum, }) if err != nil { panic(err) } logger.Info(123, 456.789, "abc", 432) logger.Infof("This is an %s", "example.") }
Output:
func Panic ¶
func Panic(args ...interface{})
Panic uses the global Logger object created by Init to write a log with panic level followed by a call to panic("Panicf").
func Panicf ¶
func Panicf(format string, args ...interface{})
Panicf uses the global Logger object created by Init to write a log with panic level followed by a call to panic("Panicf").
func SetLogLevel ¶
func SetLogLevel(logLevel LogLevel)
SetLogLevel is used to tell the global Logger object created by Init not to write logs below logLevel.
func Trace ¶
func Trace(args ...interface{})
Trace uses the global Logger object created by Init to write a log with trace level.
func Tracef ¶
func Tracef(format string, args ...interface{})
Tracef uses the global Logger object created by Init to write a log with trace level.
Types ¶
type Config ¶
type Config struct { // Directory to hold the log files. If left empty, current working directory is used. // Should you need to create multiple Logger objects, better to associate them with different directories. LogDir string // Name of a log file is formatted as `LogFilenamePrefix.LogLevel.DateTime.log`. // 3 placeholders are pre-defined: %P, %H and %U. When used in the prefix, // %P will be replaced with the program's name, %H will be replaced with hostname, // and %U will be replaced with username. // If LogFilenamePrefix is left empty, it'll be defaulted to `%P.%H.%U`. // If you create multiple Logger objects with the same directory, you must associate them with different prefixes. LogFilenamePrefix string // Latest log files of each level are associated with symbolic links. Name of a symlink is formatted as `LogSymlinkPrefix.LogLevel`. // If LogSymlinkPrefix is left empty, it'll be defaulted to `%P.%U`. // If you create multiple Logger objects with the same directory, you must associate them with different prefixes. LogSymlinkPrefix string // Limit the maximum size in MB for a single log file. 0 means unlimited. LogFileMaxSize uint32 // Limit the maximum number of log files under `LogDir`. `LogFileNumToDel` log files will be deleted if reached. <=0 means unlimited. LogFileMaxNum int // Number of log files to be deleted when `LogFileMaxNum` reached. <=0 means don't delete. LogFileNumToDel int // Don't write logs below `LogLevel`. LogLevel LogLevel // Where the logs are written. LogDest LogDest // How the logs are written. Flag ControlFlag }
Config contains options for creating a new Logger object.
type ControlFlag ¶
type ControlFlag int // ControlFlag controls how the logs are written. Use `|`(Or operator) to mix multiple flags.
const ( ControlFlagLogThrough ControlFlag = 1 << iota // Controls if logs with higher level are written to lower level log files. ControlFlagLogFuncName // Controls if function name is prepended to the logs. ControlFlagLogLineNum // Controls if filename and line number are prepended to the logs. ControlFlagLogDate // Controls if a date string formatted as '20201201' is prepended to the logs. ControlFlagNone = 0 )
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger can be used to write logs with different severity levels to files, console, or both. Logs with different severity levels are written to different files. It is goroutine-safe and supports the following features:
- Auto rotation: It'll create a new logfile whenever day changes or size of the current logfile exceeds the configured size limit.
- Auto purging: It'll delete some oldest logfiles whenever the number of logfiles exceeds the configured limit.
- Log-through: Logs with higher severity level will be written to all the logfiles with lower severity level.
- Log levels: 6 different levels are supported. Logs with different levels are written to different logfiles. By setting the Logger object to a higher log level, lower level logs will be filtered out.
- Logs are not buffered, they are written to logfiles immediately with os.(*File).Write().
- It'll create symlinks that link to the most current logfiles.
func New ¶
New can be used to create as many Logger objects as desired, while the global Logger object created by Init should be enough for most cases. Should you need to create multiple Logger objects, better to associate them with different directories, at least with different filename prefixes(including symlink prefixes), otherwise they will not work properly.
Example (MultiLoggerObject) ¶
This example shows how to create multiple Logger objects and write logs with them.
package main import ( "github.com/antigloss/go/logger" ) func main() { lg1, err := logger.New(&logger.Config{ LogDir: "./logs1", // Better to associate different Logger object with different directory. LogFileMaxSize: 200, LogFileMaxNum: 500, LogFileNumToDel: 50, LogLevel: logger.LogLevelInfo, LogDest: logger.LogDestFile, Flag: logger.ControlFlagLogLineNum, }) if err != nil { panic(err) } defer lg1.Close() // Don't forget to close the Logger object when you've done with it. lg2, err := logger.New(&logger.Config{ LogDir: "./logs2", // Better to associate different Logger object with different directory. LogFileMaxSize: 200, LogFileMaxNum: 500, LogFileNumToDel: 50, LogLevel: logger.LogLevelInfo, LogDest: logger.LogDestFile, Flag: logger.ControlFlagLogLineNum, }) if err != nil { panic(err) } defer lg2.Close() // Don't forget to close the Logger object when you've done with it. lg1.Error(333, 444.55, "This", "is", "an", "example.") lg2.Warnf("This is %s %s %s", "yet", "another", "example.") }
Output:
func (*Logger) Error ¶
func (l *Logger) Error(args ...interface{})
Error writes a log with error level.
func (*Logger) Fatal ¶
func (l *Logger) Fatal(args ...interface{})
Fatal writes a log with fatal level followed by a call to os.Exit(-1).
func (*Logger) Panic ¶
func (l *Logger) Panic(args ...interface{})
Panic writes a log with panic level followed by a call to panic("Panic").
func (*Logger) SetLogLevel ¶
SetLogLevel tells the Logger object not to write logs below `logLevel`.
func (*Logger) Trace ¶
func (l *Logger) Trace(args ...interface{})
Trace writes a log with trace level.