2022-02-03 13:49:04 +00:00
|
|
|
//go:build !windows
|
2018-04-27 06:15:29 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2018-05-17 01:42:33 +00:00
|
|
|
// Package journald provides a io.Writer to send the logs
|
|
|
|
// to journalD component of systemd.
|
|
|
|
|
2018-04-27 06:15:29 +00:00
|
|
|
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.
|
2018-04-27 06:15:29 +00:00
|
|
|
|
|
|
|
// 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"
|
2019-07-19 11:07:15 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2021-05-23 08:29:31 +00:00
|
|
|
"github.com/coreos/go-systemd/v22/journal"
|
2022-11-03 15:18:09 +00:00
|
|
|
"tuxpa.in/a/zlog"
|
|
|
|
"tuxpa.in/a/zlog/internal/cbor"
|
2018-04-27 06:15:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultJournalDPrio = journal.PriNotice
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
// NewJournalDWriter returns a zlog log destination
|
2018-04-27 06:15:29 +00:00
|
|
|
// 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
|
2018-04-27 06:15:29 +00:00
|
|
|
// journalD's priority values. JournalD has more
|
2022-03-20 19:19:42 +00:00
|
|
|
// priorities than zlog.
|
2018-04-27 06:15:29 +00:00
|
|
|
func levelToJPrio(zLevel string) journal.Priority {
|
2022-03-20 19:19:42 +00:00
|
|
|
lvl, _ := zlog.ParseLevel(zLevel)
|
2018-04-27 06:15:29 +00:00
|
|
|
|
|
|
|
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:
|
2018-04-27 06:15:29 +00:00
|
|
|
return journal.PriDebug
|
2022-03-20 19:19:42 +00:00
|
|
|
case zlog.InfoLevel:
|
2018-04-27 06:15:29 +00:00
|
|
|
return journal.PriInfo
|
2022-03-20 19:19:42 +00:00
|
|
|
case zlog.WarnLevel:
|
2018-04-27 06:15:29 +00:00
|
|
|
return journal.PriWarning
|
2022-03-20 19:19:42 +00:00
|
|
|
case zlog.ErrorLevel:
|
2018-04-27 06:15:29 +00:00
|
|
|
return journal.PriErr
|
2022-03-20 19:19:42 +00:00
|
|
|
case zlog.FatalLevel:
|
2018-04-27 06:15:29 +00:00
|
|
|
return journal.PriCrit
|
2022-03-20 19:19:42 +00:00
|
|
|
case zlog.PanicLevel:
|
2018-04-27 06:15:29 +00:00
|
|
|
return journal.PriEmerg
|
2022-03-20 19:19:42 +00:00
|
|
|
case zlog.NoLevel:
|
2018-04-27 06:15:29 +00:00
|
|
|
return journal.PriNotice
|
|
|
|
}
|
|
|
|
return defaultJournalDPrio
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w journalWriter) Write(p []byte) (n int, err error) {
|
|
|
|
var event map[string]interface{}
|
2020-01-15 21:01:51 +00:00
|
|
|
origPLen := len(p)
|
2018-04-27 06:15:29 +00:00
|
|
|
p = cbor.DecodeIfBinaryToBytes(p)
|
|
|
|
d := json.NewDecoder(bytes.NewReader(p))
|
|
|
|
d.UseNumber()
|
|
|
|
err = d.Decode(&event)
|
|
|
|
jPrio := defaultJournalDPrio
|
2022-02-03 13:49:04 +00:00
|
|
|
args := make(map[string]string)
|
2018-04-27 06:15:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-03-20 19:19:42 +00:00
|
|
|
if l, ok := event[zlog.LevelFieldName].(string); ok {
|
2018-04-27 06:15:29 +00:00
|
|
|
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:
|
2018-04-27 06:15:29 +00:00
|
|
|
continue
|
2022-03-20 19:19:42 +00:00
|
|
|
case zlog.MessageFieldName:
|
2018-04-27 06:15:29 +00:00
|
|
|
msg, _ = value.(string)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-03 13:49:04 +00:00
|
|
|
switch v := value.(type) {
|
2018-04-27 06:15:29 +00:00
|
|
|
case string:
|
2022-02-03 13:49:04 +00:00
|
|
|
args[jKey] = v
|
2018-04-27 06:15:29 +00:00
|
|
|
case json.Number:
|
|
|
|
args[jKey] = fmt.Sprint(value)
|
|
|
|
default:
|
2022-07-16 20:03:29 +00:00
|
|
|
b, err := zerolog.InterfaceMarshalFunc(value)
|
2018-04-27 06:15:29 +00:00
|
|
|
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)
|
2020-01-15 21:01:51 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
n = origPLen
|
|
|
|
}
|
|
|
|
|
2018-04-27 06:15:29 +00:00
|
|
|
return
|
|
|
|
}
|