package logging import ( "fmt" "time" "encoding/json" ) type Log struct { Time time.Time `json:"time"` Level LogLevel `json:"level"` Message string `json:"message"` } func valid_to_log(level LogLevel) bool { return level <= maxLevel && level >= minLevel } func log(log Log) error { if valid_to_log(log.Level) { switch logFormat { case FormatText: fmt.Printf("%s : %s : %s\n", log.Time.Format(time.DateTime), log.Level, log.Message) case FormatJson: data, err := json.Marshal(log) if err != nil { return err } fmt.Println(string(data)) } } return nil } func Debug(format string, args ...any) error { return log(Log{ Time: time.Now(), Level: LevelDebug, Message: fmt.Sprintf(format, args...)}) } func Info(format string, args ...any) error { return log(Log{ Time: time.Now(), Level: LevelInfo, Message: fmt.Sprintf(format, args...)}) } func Warn(format string, args ...any) error { return log(Log{ Time: time.Now(), Level: LevelWarn, Message: fmt.Sprintf(format, args...)}) } func Error(format string, args ...any) error { return log(Log{ Time: time.Now(), Level: LevelError, Message: fmt.Sprintf(format, args...)}) } func Fatal(format string, args ...any) error { return log(Log{ Time: time.Now(), Level: LevelFatal, Message: fmt.Sprintf(format, args...)}) }