kos/pkg/log/logger.go

81 lines
1.3 KiB
Go
Raw Normal View History

2023-04-23 17:57:36 +08:00
package log
import "strings"
const (
TraceLevel int = iota + 1
DebugLevel
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
PanicLevel
)
var (
lvText = map[int]string{
TraceLevel: "TRACE",
DebugLevel: "DEBUG",
InfoLevel: "INFO",
WarnLevel: "WARN",
ErrorLevel: "ERROR",
FatalLevel: "FATAL",
PanicLevel: "PANIC",
}
)
type Logger interface {
SetLevel(int)
Prefix(string)
Print(i ...interface{})
Printf(format string, args ...interface{})
Debug(i ...interface{})
Debugf(format string, args ...interface{})
Info(i ...interface{})
Infof(format string, args ...interface{})
Warn(i ...interface{})
Warnf(format string, args ...interface{})
Error(i ...interface{})
Errorf(format string, args ...interface{})
Fatal(i ...interface{})
Fatalf(format string, args ...interface{})
Panic(i ...interface{})
Panicf(format string, args ...interface{})
}
func getLevelText(lv int) string {
if s, ok := lvText[lv]; ok {
return s
} else {
return "TRACE"
}
}
func FormatLevel(lv int) string {
return getLevelText(lv)
}
func ParseLevel(s string) (lv int) {
lv = DebugLevel
s = strings.ToUpper(s)
for k, v := range lvText {
if s == v {
lv = k
return
}
}
return
}
func ParseLevelWithoutDefault(s string) (lv int) {
lv = -1
s = strings.ToUpper(s)
for k, v := range lvText {
if s == v {
lv = k
return
}
}
return
}