Documentation ¶
Overview ¶
Package log is a well formatted golang logging library.
Index ¶
- Constants
- Variables
- func Debug(args ...interface{})
- func Debugf(format string, args ...interface{})
- func Error(args ...interface{})
- func Errorf(format string, args ...interface{})
- func Fatal(args ...interface{})
- func Fatalf(format string, args ...interface{})
- func GetLogLevel() logrus.Level
- func Info(args ...interface{})
- func Infof(format string, args ...interface{})
- func Panic(args ...interface{})
- func Panicf(format string, args ...interface{})
- func SetLogLevel(level logrus.Level)
- func SetupColor()
- func Warn(args ...interface{})
- func Warnf(format string, args ...interface{})
- type ColorFormat
- type Formatter
- type SWLog
- func (logger *SWLog) Debug(args ...interface{})
- func (logger *SWLog) Debugf(format string, args ...interface{})
- func (logger *SWLog) Error(args ...interface{})
- func (logger *SWLog) Errorf(format string, args ...interface{})
- func (logger *SWLog) Fatal(args ...interface{})
- func (logger *SWLog) Fatalf(format string, args ...interface{})
- func (logger *SWLog) Info(args ...interface{})
- func (logger *SWLog) Infof(format string, args ...interface{})
- func (logger *SWLog) Init(logFile string, level logrus.Level, log2STD bool)
- func (logger *SWLog) Log(level logrus.Level, filename string, funcname string, args ...interface{})
- func (logger *SWLog) Logf(level logrus.Level, format string, filename string, funcname string, ...)
- func (logger *SWLog) Panic(args ...interface{})
- func (logger *SWLog) Panicf(format string, args ...interface{})
- func (logger *SWLog) Warn(args ...interface{})
- func (logger *SWLog) Warnf(format string, args ...interface{})
Examples ¶
Constants ¶
View Source
const ( // PanicLevel level, highest level of severity. Logs and then calls panic with the // message passed to Debug, Info, ... PanicLevel = logrus.PanicLevel // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the // logging level is set to Panic. FatalLevel = logrus.FatalLevel // ErrorLevel level. Logs. Used for errors that should definitely be noted. // Commonly used for hooks to send errors to an error tracking service. ErrorLevel = logrus.ErrorLevel // WarnLevel level. Non-critical entries that deserve eyes. WarnLevel = logrus.WarnLevel // InfoLevel level. General operational entries about what's going on inside the // application. InfoLevel = logrus.InfoLevel // DebugLevel level. Usually only enabled when debugging. Very verbose logging. DebugLevel = logrus.DebugLevel // DayHours is the hours of day DayHours = 24 // WeekHours is the hours of week WeekHours = 7 * 24 )
View Source
const ( DefaultLogDir = "/tmp/log/" DefaultLogFile = "log.log" DefaultCLILogFile = "cli.log" )
* SWLogger is the global logging instance for out of box logging
Variables ¶
View Source
var ( // SWLogger is global logging instance, which need to call `SWLog.Init()` to setup before running SWLogger = &SWLog{ isSetup: false, } // LevelFromStr map configuration string with log level LevelFromStr = map[string]logrus.Level{ "PANIC": PanicLevel, "FATAL": FatalLevel, "ERROR": ErrorLevel, "WARN": WarnLevel, "INFO": InfoLevel, "DEBUG": DebugLevel, } )
Functions ¶
func Debugf ¶
func Debugf(format string, args ...interface{})
Debugf logging in debug level with the given formated args
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf logging in fatal level and calling the os.Exit()
func Panic ¶
func Panic(args ...interface{})
Panic logging in panic level and calling the os.Panic()
func Panicf ¶
func Panicf(format string, args ...interface{})
Panicf logging in panic level and calling the os.Panic()
func SetupColor ¶
func SetupColor()
Types ¶
type ColorFormat ¶
type Formatter ¶
type Formatter struct { // Timestamp format logrus.TextFormatter // Available standard keys: time, msg, lvl // Also can include custom fields but limited to strings. // All of fields need to be wrapped inside %% i.e %time% %msg% LogFormat string // file name and line number where calling the LOG/INFO/DEBUG... FileName string // function name where calling the LOG/INFO/DEBUG... FuncName string }
Formatter implements logrus.Formatter interface.
type SWLog ¶
type SWLog struct { // log will output log into log file and stderr STDLogger *logrus.Logger FileLogger *logrus.Logger IsLog2STD bool LogFile string LogLevel logrus.Level // contains filtered or unexported fields }
SWLog wrap the log system for
func (*SWLog) Debug ¶
func (logger *SWLog) Debug(args ...interface{})
Debug logging in debug level
Example ¶
package main import ( "fmt" "github.com/edony-ink/log" "github.com/sirupsen/logrus" ) func main() { // **NOTE**: log need to be initated only once, `SWLogger.Init` is recommended being called in `init` function log.SWLogger.Init("/tmp/test.log", logrus.DebugLevel, true) log.Debug("test debug") /* try the following test way to assert output is as expected. patches := gomonkey.ApplyFunc(time.Now, func() time.Time { return time.Unix(1615256178, 0) }) defer patches.Reset() // parse logging string from log file to do assertion logFile, err := os.ReadFile("/tmp/test.log") if err != nil { panic(err) } lines := strings.Split(string(logFile), "\n") // ignore the empty line because each line endup with `\n` // for matching assertion, the stdoud line has to include the `\n` fmt.Println(lines[len(lines)-2] + "\n") */ // this is mocked output fmt.Println("2021-03-09 10:16:18.000|DEBUG |ExampleSWLog_Debug(examples/example_log_test.go:23)|test debug") }
Output: 2021-03-09 10:16:18.000|DEBUG |ExampleSWLog_Debug(examples/example_log_test.go:23)|test debug
func (*SWLog) Fatal ¶
func (logger *SWLog) Fatal(args ...interface{})
Fatal logging in fatal level and calling the os.Exit()
func (*SWLog) Info ¶
func (logger *SWLog) Info(args ...interface{})
Info logging in info level
Example ¶
package main import ( "fmt" "github.com/edony-ink/log" "github.com/sirupsen/logrus" ) func main() { log.SWLogger.Init("/tmp/test.log", logrus.DebugLevel, true) log.Info("test info") // this is mocked output fmt.Println("2021-03-09 10:16:18.000|INFO |ExampleSWLog_Info(examples/example_log_test.go:23)|test info") }
Output: 2021-03-09 10:16:18.000|INFO |ExampleSWLog_Info(examples/example_log_test.go:23)|test info
func (*SWLog) Logf ¶
func (logger *SWLog) Logf(level logrus.Level, format string, filename string, funcname string, args ...interface{})
Logf logging the message with the given formated args
func (*SWLog) Panic ¶
func (logger *SWLog) Panic(args ...interface{})
Panic logging in panic level and calling the os.Panic()
Click to show internal directories.
Click to hide internal directories.