2023-10-12 12:00:00 +02:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import "log"
|
|
|
|
|
|
|
|
type Level int
|
|
|
|
|
|
|
|
const (
|
2023-10-18 12:00:00 +02:00
|
|
|
UNKNOWN = Level(-1)
|
|
|
|
DEBUG = Level(1)
|
|
|
|
INFO = Level(2)
|
|
|
|
WARN = Level(3)
|
|
|
|
ERROR = Level(4)
|
|
|
|
FATAL = Level(5)
|
2023-10-12 12:00:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (l Level) String() string {
|
|
|
|
switch l {
|
|
|
|
case DEBUG:
|
|
|
|
return "DEBUG "
|
|
|
|
case INFO:
|
|
|
|
return "INFO "
|
|
|
|
case WARN:
|
|
|
|
return "WARN "
|
|
|
|
case ERROR:
|
|
|
|
return "ERROR "
|
|
|
|
case FATAL:
|
|
|
|
return "FATAL "
|
|
|
|
default:
|
|
|
|
return "????? "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 12:00:00 +02:00
|
|
|
func FromString(s string) Level {
|
|
|
|
switch s {
|
|
|
|
case "DEBUG":
|
|
|
|
return DEBUG
|
|
|
|
case "INFO":
|
|
|
|
return INFO
|
|
|
|
case "WARN":
|
|
|
|
return WARN
|
|
|
|
case "ERROR":
|
|
|
|
return ERROR
|
|
|
|
case "FATAL":
|
|
|
|
return FATAL
|
|
|
|
default:
|
|
|
|
return UNKNOWN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 12:00:00 +02:00
|
|
|
var LogLevel Level = 2
|
|
|
|
|
|
|
|
func SetLogLevel(level Level) {
|
|
|
|
LogLevel = level
|
|
|
|
}
|
|
|
|
|
|
|
|
func Println(level Level, args ...any) {
|
|
|
|
if level >= LogLevel {
|
2023-10-18 12:00:00 +02:00
|
|
|
newargs := make([]any, 0)
|
|
|
|
newargs = append(newargs, level)
|
|
|
|
newargs = append(newargs, args...)
|
|
|
|
log.Println(newargs...)
|
2023-10-12 12:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Printf(level Level, format string, args ...any) {
|
|
|
|
if level >= LogLevel {
|
2023-10-18 12:00:00 +02:00
|
|
|
log.Printf(level.String()+format, args...)
|
2023-10-12 12:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalln(args ...any) {
|
2023-10-18 12:00:00 +02:00
|
|
|
newargs := make([]any, 0)
|
|
|
|
newargs = append(newargs, FATAL)
|
|
|
|
newargs = append(newargs, args...)
|
|
|
|
log.Fatalln(newargs...)
|
2023-10-12 12:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalf(format string, args ...any) {
|
|
|
|
level := FATAL
|
|
|
|
log.Fatalf(level.String()+format, args)
|
|
|
|
}
|