Files
logger/strategy/json.go
2025-05-09 18:43:02 +02:00

35 lines
660 B
Go

package strategy
import (
"encoding/json"
"fmt"
"os"
"time"
"git.schreifuchs.ch/schreifuchs/logger"
"git.schreifuchs.ch/schreifuchs/logger/lvl"
)
// JSON logs to std wit the schema:
// {"time":"","level":"","message":""}.
func JSON(logs <-chan *logger.Log) {
for log := range logs {
timedLog := struct {
Time time.Time `json:"time"`
LVL lvl.Level `json:"level"`
Message string `json:"message"`
}{
Time: time.Now(),
LVL: log.Level,
Message: log.Message,
}
out, err := json.Marshal(timedLog)
if err != nil {
fmt.Println("Error while marshaling log: ", err)
os.Exit(1)
}
println(string(out))
}
}