2019-03-25 09:28:58 +01:00
|
|
|
package logger
|
2019-02-27 17:33:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/op/go-logging"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Log is global logger
|
|
|
|
var Log = logging.MustGetLogger("myLogger")
|
2019-03-25 09:28:58 +01:00
|
|
|
var logBackendLeveled logging.LeveledBackend
|
2019-02-27 17:33:26 +01:00
|
|
|
|
2019-03-25 09:28:58 +01:00
|
|
|
// SetLogLevel sets log level for global logger (debug, info, notice, warning, error)
|
|
|
|
func SetLogLevel(level string) {
|
|
|
|
logBackendLevel := logging.NOTICE
|
2019-02-27 17:33:26 +01:00
|
|
|
switch level {
|
|
|
|
case "debug":
|
|
|
|
logBackendLevel = logging.DEBUG
|
|
|
|
break
|
|
|
|
|
|
|
|
case "info":
|
|
|
|
logBackendLevel = logging.INFO
|
|
|
|
break
|
|
|
|
|
|
|
|
case "notice":
|
|
|
|
logBackendLevel = logging.NOTICE
|
|
|
|
break
|
|
|
|
|
|
|
|
case "warning":
|
|
|
|
logBackendLevel = logging.WARNING
|
|
|
|
break
|
|
|
|
|
|
|
|
case "error":
|
|
|
|
logBackendLevel = logging.ERROR
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
2019-03-25 09:28:58 +01:00
|
|
|
|
2019-02-27 17:33:26 +01:00
|
|
|
logBackendLeveled.SetLevel(logBackendLevel, "")
|
2019-03-25 09:28:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// configureLogger sets logger backend and level
|
|
|
|
func configureLogger() {
|
|
|
|
logBackend := logging.NewLogBackend(os.Stderr, "", 0)
|
|
|
|
logBackendFormatter := logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(
|
|
|
|
`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
|
|
|
|
))
|
|
|
|
logBackendLeveled = logging.AddModuleLevel(logBackendFormatter)
|
|
|
|
logBackendLeveled.SetLevel(logging.NOTICE, "")
|
2019-02-27 17:33:26 +01:00
|
|
|
logging.SetBackend(logBackendLeveled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
spew.Config.DisablePointerAddresses = true
|
|
|
|
spew.Config.DisableCapacities = true
|
|
|
|
spew.Config.DisableMethods = true
|
|
|
|
spew.Config.DisablePointerMethods = true
|
2019-03-25 09:28:58 +01:00
|
|
|
configureLogger()
|
2019-02-27 17:33:26 +01:00
|
|
|
}
|