From 84015df635a0032e9dce0a42c47bfab7743cdc4a Mon Sep 17 00:00:00 2001 From: Jonathan Buch Date: Wed, 20 May 2020 08:42:02 +0200 Subject: [PATCH] Add systemd journal handler --- go.mod | 1 + go.sum | 3 +++ handlers/journal/systemdlog.go | 47 ++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 handlers/journal/systemdlog.go diff --git a/go.mod b/go.mod index 40ca197..621af1f 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/aphistic/sweet v0.2.0 // indirect github.com/aws/aws-sdk-go v1.20.6 github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 + github.com/coreos/go-systemd/v22 v22.0.0 github.com/fatih/color v1.7.0 github.com/go-logfmt/logfmt v0.4.0 github.com/golang/protobuf v1.3.1 // indirect diff --git a/go.sum b/go.sum index f367e3e..aeaa0ef 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/aws/aws-sdk-go v1.20.6 h1:kmy4Gvdlyez1fV4kw5RYxZzWKVyuHZHgPWeU/YvRsV4 github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 h1:WWB576BN5zNSZc/M9d/10pqEx5VHNhaQ/yOVAkmj5Yo= github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I= +github.com/coreos/go-systemd/v22 v22.0.0 h1:XJIw/+VlJ+87J+doOxznsAWIdmWuViOVhkQamW5YV28= +github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -18,6 +20,7 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80nA= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= diff --git a/handlers/journal/systemdlog.go b/handlers/journal/systemdlog.go new file mode 100644 index 0000000..a9ae4e4 --- /dev/null +++ b/handlers/journal/systemdlog.go @@ -0,0 +1,47 @@ +package journal + +import ( + "errors" + "fmt" + + "github.com/apex/log" + "github.com/coreos/go-systemd/v22/journal" +) + +// Handler implementation. +type Handler struct { +} + +// New handler. +func New() *Handler { + return &Handler{} +} + +// HandleLog implements log.Handler. +func (h *Handler) HandleLog(e *log.Entry) error { + + fields := make(map[string]string, len(e.Fields)) + for k, v := range e.Fields { + fields[k] = fmt.Sprint(v) + } + + switch e.Level { + case log.DebugLevel: + return journal.Send(e.Message, journal.PriDebug, fields) + case log.InfoLevel: + return journal.Send(e.Message, journal.PriInfo, fields) + case log.WarnLevel: + return journal.Send(e.Message, journal.PriWarning, fields) + case log.ErrorLevel: + return journal.Send(e.Message, journal.PriErr, fields) + case log.FatalLevel: + return journal.Send(e.Message, journal.PriCrit, fields) + } + + return errors.New("unknown log level given for systemd") +} + +// Close shuts down the handler. +func (h *Handler) Close() error { + return nil +}