zlog/journald/journald.go

122 lines
2.7 KiB
Go
Raw Normal View History

//go:build !windows
// +build !windows
// Package journald provides a io.Writer to send the logs
// to journalD component of systemd.
package journald
2022-03-20 19:19:42 +00:00
// This file provides a zlog writer so that logs printed
// using zlog library can be sent to a journalD.
// Zerolog's Top level key/Value Pairs are translated to
// journald's args - all Values are sent to journald as strings.
// And all key strings are converted to uppercase before sending
// to journald (as required by journald).
// In addition, entire log message (all Key Value Pairs), is also
// sent to journald under the key "JSON".
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"github.com/coreos/go-systemd/v22/journal"
2022-03-20 19:19:42 +00:00
"git.tuxpa.in/a/zlog"
"git.tuxpa.in/a/zlog/internal/cbor"
)
const defaultJournalDPrio = journal.PriNotice
2022-03-20 19:19:42 +00:00
// NewJournalDWriter returns a zlog log destination
// to be used as parameter to New() calls. Writing logs
// to this writer will send the log messages to journalD
// running in this system.
func NewJournalDWriter() io.Writer {
return journalWriter{}
}
type journalWriter struct {
}
2022-03-20 19:19:42 +00:00
// levelToJPrio converts zlog Level string into
// journalD's priority values. JournalD has more
2022-03-20 19:19:42 +00:00
// priorities than zlog.
func levelToJPrio(zLevel string) journal.Priority {
2022-03-20 19:19:42 +00:00
lvl, _ := zlog.ParseLevel(zLevel)
switch lvl {
2022-03-20 19:19:42 +00:00
case zlog.TraceLevel:
2019-11-04 19:39:22 +00:00
return journal.PriDebug
2022-03-20 19:19:42 +00:00
case zlog.DebugLevel:
return journal.PriDebug
2022-03-20 19:19:42 +00:00
case zlog.InfoLevel:
return journal.PriInfo
2022-03-20 19:19:42 +00:00
case zlog.WarnLevel:
return journal.PriWarning
2022-03-20 19:19:42 +00:00
case zlog.ErrorLevel:
return journal.PriErr
2022-03-20 19:19:42 +00:00
case zlog.FatalLevel:
return journal.PriCrit
2022-03-20 19:19:42 +00:00
case zlog.PanicLevel:
return journal.PriEmerg
2022-03-20 19:19:42 +00:00
case zlog.NoLevel:
return journal.PriNotice
}
return defaultJournalDPrio
}
func (w journalWriter) Write(p []byte) (n int, err error) {
var event map[string]interface{}
origPLen := len(p)
p = cbor.DecodeIfBinaryToBytes(p)
d := json.NewDecoder(bytes.NewReader(p))
d.UseNumber()
err = d.Decode(&event)
jPrio := defaultJournalDPrio
args := make(map[string]string)
if err != nil {
return
}
2022-03-20 19:19:42 +00:00
if l, ok := event[zlog.LevelFieldName].(string); ok {
jPrio = levelToJPrio(l)
}
msg := ""
for key, value := range event {
jKey := strings.ToUpper(key)
switch key {
2022-03-20 19:19:42 +00:00
case zlog.LevelFieldName, zlog.TimestampFieldName:
continue
2022-03-20 19:19:42 +00:00
case zlog.MessageFieldName:
msg, _ = value.(string)
continue
}
switch v := value.(type) {
case string:
args[jKey] = v
case json.Number:
args[jKey] = fmt.Sprint(value)
default:
b, err := json.Marshal(value)
if err != nil {
args[jKey] = fmt.Sprintf("[error: %v]", err)
} else {
args[jKey] = string(b)
}
}
}
args["JSON"] = string(p)
err = journal.Send(msg, jPrio, args)
if err == nil {
n = origPLen
}
return
}