Skip to content

Commit

Permalink
journald: don't call Enabled before each write (#407)
Browse files Browse the repository at this point in the history
Enabled opens and close a socket connection by reusing or initializing
a global connection.

I also updated go-systemd to the current development release to include
the following fix:

coreos/go-systemd@75f33b0

This is the only journal related change since the latest stable release
  • Loading branch information
drakkan committed Feb 3, 2022
1 parent 3efdd82 commit 025f9f1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -3,7 +3,7 @@ module github.com/rs/zerolog
go 1.15

require (
github.com/coreos/go-systemd/v22 v22.3.2
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534
github.com/mattn/go-colorable v0.1.12
github.com/pkg/errors v0.9.1
github.com/rs/xid v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
@@ -1,5 +1,7 @@
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534 h1:rtAn27wIbmOGUs7RIbVgPEjb31ehTVniDwPGXyMxm5U=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
Expand Down
12 changes: 4 additions & 8 deletions journald/journald.go
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

// Package journald provides a io.Writer to send the logs
Expand Down Expand Up @@ -69,19 +70,14 @@ func levelToJPrio(zLevel string) journal.Priority {
}

func (w journalWriter) Write(p []byte) (n int, err error) {
if !journal.Enabled() {
err = fmt.Errorf("cannot connect to journalD")
return
}

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, 0)
args := make(map[string]string)
if err != nil {
return
}
Expand All @@ -100,9 +96,9 @@ func (w journalWriter) Write(p []byte) (n int, err error) {
continue
}

switch value.(type) {
switch v := value.(type) {
case string:
args[jKey], _ = value.(string)
args[jKey] = v
case json.Number:
args[jKey] = fmt.Sprint(value)
default:
Expand Down

0 comments on commit 025f9f1

Please sign in to comment.