go get -u github.com/no-src/log
Current support following loggers
For example, init a console logger, to write logs.
package main
import (
"errors"
"github.com/no-src/log"
"github.com/no-src/log/level"
)
func main() {
// init console logger as default logger
// replace the line of code with any logger you need
log.InitDefaultLogger(log.NewConsoleLogger(level.DebugLevel))
defer log.Close()
text := "hello world"
// use default logger to write logs
log.Debug("%s, test debug log", text)
log.Info("%s, test info log", text)
log.Warn("%s, test warn log", text)
log.Error(errors.New("log err"), "%s, test error log", text)
log.ErrorIf(errors.New("log err"), "%s, test error log", text)
log.Log("%s, test log log", text)
}
Init empty logger as default logger.
log.InitDefaultLogger(log.NewEmptyLogger())
Init console logger as default logger.
log.InitDefaultLogger(log.NewConsoleLogger(level.DebugLevel))
Init file logger as default logger.
if logger, err := log.NewFileLogger(level.DebugLevel, "./logs", "default_"); err == nil {
log.InitDefaultLogger(logger)
} else {
log.Error(err, "init file logger error")
}
Init multi logger as default logger.
if logger, err := log.NewFileLogger(level.DebugLevel, "./logs", "multi_"); err == nil {
log.InitDefaultLogger(log.NewMultiLogger(log.NewConsoleLogger(level.DebugLevel), logger))
} else {
log.Error(err, "init file logger error")
}
Init console logger as default logger and set the sample rate, default is 1
.
log.InitDefaultLoggerWithSample(log.NewConsoleLogger(level.DebugLevel), 0.6)
Use default logger to write logs by random sampling.
text := "hello world"
log.DebugSample("[sample] %s, test debug log", text)
log.InfoSample("[sample] %s, test info log", text)
log.WarnSample("[sample] %s, test warn log", text)
log.ErrorSample(errors.New("log err"), "[sample] %s, test error log", text)
log.ErrorIfSample(errors.New("log err from ErrorIfSample"), "[sample] %s, test error log", text)