mark2web/pkg/logger/logger.go

139 lines
2.9 KiB
Go
Raw Permalink Normal View History

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-27 13:52:22 +01:00
var Prefix = ""
2019-03-25 09:28:58 +01:00
// SetLogLevel sets log level for global logger (debug, info, notice, warning, error)
func SetLogLevel(level string) {
2019-03-25 10:16:33 +01:00
logBackendLevel := logging.INFO
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(
2019-03-27 13:52:22 +01:00
`%{color}%{time:2006-01-02 15:04:05.000} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
2019-03-25 09:28:58 +01:00
))
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
}
2019-03-25 10:16:33 +01:00
2019-03-27 13:52:22 +01:00
func prefix() string {
return Prefix
}
2019-03-25 10:16:33 +01:00
// D is shorthand for Debugf
func D(format string, args ...interface{}) {
2019-03-27 13:52:22 +01:00
Log.Debugf(prefix()+format, args...)
2019-03-25 10:16:33 +01:00
}
// I is shorthand for Infof
func I(format string, args ...interface{}) {
2019-03-27 13:52:22 +01:00
Log.Infof(prefix()+format, args...)
2019-03-25 10:16:33 +01:00
}
// N is shorthand for Noticef
func N(format string, args ...interface{}) {
2019-03-27 13:52:22 +01:00
Log.Noticef(prefix()+format, args...)
2019-03-25 10:16:33 +01:00
}
// W is shorthand for Warningf
func W(format string, args ...interface{}) {
2019-03-27 13:52:22 +01:00
Log.Warningf(prefix()+format, args...)
2019-03-25 10:16:33 +01:00
}
// E is shorthand for Errorf
func E(format string, args ...interface{}) {
2019-03-27 13:52:22 +01:00
Log.Errorf(prefix()+format, args...)
2019-03-25 10:16:33 +01:00
}
// P is shorthand for Panicf
func P(format string, args ...interface{}) {
2019-03-27 13:52:22 +01:00
Log.Panicf(prefix()+format, args...)
2019-03-25 10:16:33 +01:00
}
// Eerr is shorthand for
// if err != nil {
// Log.Errorf(...)
// }
func Eerr(err error, format string, args ...interface{}) {
if err != nil {
args = append(args, err)
2019-03-27 13:52:22 +01:00
Log.Errorf(prefix()+format+" (Error: %s)", args...)
2019-03-25 10:16:33 +01:00
}
}
// Eexit is shorthand for
// if err != nil {
// Log.Errorf(...)
// os.Exit(1)
// }
func Eexit(err error, format string, args ...interface{}) {
Eerr(err, format, args...)
if err != nil {
os.Exit(1)
}
}
2019-03-29 15:49:25 +01:00
// Exit is shorthand for
// Log.Errorf(...)
// os.Exit(1)
func Exit(format string, args ...interface{}) {
E(format, args...)
os.Exit(1)
}
2019-03-25 10:16:33 +01:00
// Perr is shorthand for
// if err != nil {
// Log.Panicf(...)
// }
func Perr(err error, format string, args ...interface{}) {
if err != nil {
args = append(args, err)
2019-03-27 13:52:22 +01:00
Log.Panicf(prefix()+format+" (Error: %s)", args...)
2019-03-25 10:16:33 +01:00
}
}