From 025f9f18192466986662ddf062a3f6dcedcb2f6b Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Thu, 3 Feb 2022 14:49:04 +0100 Subject: [PATCH] journald: don't call Enabled before each write (#407) 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: https://github.com/coreos/go-systemd/commit/75f33b08dbe229fb37b96bf0076907b6b8159af1 This is the only journal related change since the latest stable release --- go.mod | 2 +- go.sum | 2 ++ journald/journald.go | 12 ++++-------- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 5f2cb5da..746596d9 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 2b15addd..d8f860be 100644 --- a/go.sum +++ b/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= diff --git a/journald/journald.go b/journald/journald.go index bc368da3..8f77d0a2 100644 --- a/journald/journald.go +++ b/journald/journald.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows // Package journald provides a io.Writer to send the logs @@ -69,11 +70,6 @@ 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) @@ -81,7 +77,7 @@ func (w journalWriter) Write(p []byte) (n int, err error) { d.UseNumber() err = d.Decode(&event) jPrio := defaultJournalDPrio - args := make(map[string]string, 0) + args := make(map[string]string) if err != nil { return } @@ -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: