Skip to content

Commit

Permalink
Add systemd journal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
BuJo committed May 20, 2020
1 parent 1d5d21c commit 9fcccc8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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/google/uuid v1.1.1 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
Expand All @@ -16,6 +18,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/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
Expand Down
47 changes: 47 additions & 0 deletions handlers/journal/systemdlog.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 9fcccc8

Please sign in to comment.