initial commit

This commit is contained in:
Tristan Ancelet
2026-02-27 14:28:59 -06:00
commit fcb9d6c3c3
4 changed files with 169 additions and 0 deletions

57
log.go Normal file
View File

@@ -0,0 +1,57 @@
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...)})
}