Files
stfs/internal/logging/json.go
2021-12-28 22:53:41 +01:00

73 lines
1.4 KiB
Go

package logging
import (
"encoding/json"
"fmt"
"os"
"time"
golog "github.com/fclairamb/go-log"
)
type logMessage struct {
Time int64 `json:"time"`
Level string `json:"level"`
Event string `json:"event"`
Data interface{} `json:"data"`
}
func printJSON(level string, event string, keyvals interface{}) {
line, _ := json.Marshal(&logMessage{
Time: time.Now().Unix(),
Level: level,
Event: event,
Data: keyvals,
}) // We are ignoring JSON marshalling erorrs
_, _ = fmt.Fprintln(os.Stderr, string(line)) // We are ignoring printing errors in line wih the stdlib
}
type JSONLogger struct {
verbosity int
}
func NewJSONLogger(verbosity int) *JSONLogger {
return &JSONLogger{
verbosity: verbosity,
}
}
func (l JSONLogger) Trace(event string, keyvals ...interface{}) {
if l.verbosity >= 4 {
printJSON("TRACE", event, keyvals)
}
}
func (l JSONLogger) Debug(event string, keyvals ...interface{}) {
if l.verbosity >= 3 {
printJSON("DEBUG", event, keyvals)
}
}
func (l JSONLogger) Info(event string, keyvals ...interface{}) {
if l.verbosity >= 2 {
printJSON("INFO", event, keyvals)
}
}
func (l JSONLogger) Warn(event string, keyvals ...interface{}) {
if l.verbosity >= 1 {
printJSON("WARN", event, keyvals)
}
}
func (l JSONLogger) Error(event string, keyvals ...interface{}) {
if l.verbosity >= 0 {
printJSON("ERROR", event, keyvals)
}
}
func (l JSONLogger) With(keyvals ...interface{}) golog.Logger {
return l
}