mfer/internal/log/log.go

75 lines
1.2 KiB
Go
Raw Normal View History

2022-12-05 22:59:08 +00:00
package log
import (
"github.com/apex/log"
acli "github.com/apex/log/handlers/cli"
2022-12-06 02:29:01 +00:00
"github.com/davecgh/go-spew/spew"
2022-12-05 22:59:08 +00:00
"github.com/pterm/pterm"
)
2022-12-06 02:29:01 +00:00
type Level = log.Level
2022-12-05 22:59:08 +00:00
func DisableStyling() {
pterm.DisableColor()
pterm.DisableStyling()
pterm.Debug.Prefix.Text = ""
pterm.Info.Prefix.Text = ""
pterm.Success.Prefix.Text = ""
pterm.Warning.Prefix.Text = ""
pterm.Error.Prefix.Text = ""
pterm.Fatal.Prefix.Text = ""
}
func Init() {
log.SetHandler(acli.Default)
log.SetLevel(log.InfoLevel)
}
2022-12-06 02:29:01 +00:00
func Debug(arg string) {
log.Debug(arg)
}
func Dump(args ...interface{}) {
str := spew.Sdump(args...)
Debug(str)
}
func EnableDebugLogging() {
SetLevel(log.DebugLevel)
}
func VerbosityStepsToLogLevel(l int) log.Level {
switch l {
case 1:
return log.WarnLevel
case 2:
return log.InfoLevel
case 3:
return log.DebugLevel
}
return log.ErrorLevel
}
func SetLevelFromVerbosity(l int) {
SetLevel(VerbosityStepsToLogLevel(l))
}
2022-12-05 22:59:08 +00:00
func SetLevel(arg log.Level) {
log.SetLevel(arg)
}
2022-12-06 02:29:01 +00:00
func GetLogger() *log.Logger {
2022-12-05 22:59:08 +00:00
if logger, ok := log.Log.(*log.Logger); ok {
2022-12-06 02:29:01 +00:00
return logger
2022-12-05 22:59:08 +00:00
}
2022-12-06 02:29:01 +00:00
panic("unable to get logger")
2022-12-05 22:59:08 +00:00
}
2022-12-06 02:29:01 +00:00
func GetLevel() log.Level {
return GetLogger().Level
2022-12-05 22:59:08 +00:00
}
func WithError(e error) *log.Entry {
2022-12-06 02:29:01 +00:00
return GetLogger().WithError(e)
2022-12-05 22:59:08 +00:00
}