Implement --loglevel option ; Use logger instead of log everywhere

This commit is contained in:
ppom
2023-10-18 12:00:00 +02:00
parent 22ac3764e4
commit 3822e854b6
2 changed files with 55 additions and 18 deletions

View File

@ -5,11 +5,12 @@ import "log"
type Level int
const (
DEBUG = Level(1)
INFO = Level(2)
WARN = Level(3)
ERROR = Level(4)
FATAL = Level(5)
UNKNOWN = Level(-1)
DEBUG = Level(1)
INFO = Level(2)
WARN = Level(3)
ERROR = Level(4)
FATAL = Level(5)
)
func (l Level) String() string {
@ -29,6 +30,23 @@ func (l Level) String() string {
}
}
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
}
}
var LogLevel Level = 2
func SetLogLevel(level Level) {
@ -37,13 +55,16 @@ func SetLogLevel(level Level) {
func Println(level Level, args ...any) {
if level >= LogLevel {
log.Println(level, args)
newargs := make([]any, 0)
newargs = append(newargs, level)
newargs = append(newargs, args...)
log.Println(newargs...)
}
}
func Printf(level Level, format string, args ...any) {
if level >= LogLevel {
log.Printf(level.String()+format, args)
log.Printf(level.String()+format, args...)
}
}