Skip to content

Commit 025f9f1

Browse files
authored
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: coreos/go-systemd@75f33b0 This is the only journal related change since the latest stable release
1 parent 3efdd82 commit 025f9f1

3 files changed

Lines changed: 7 additions & 9 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/rs/zerolog
33
go 1.15
44

55
require (
6-
github.com/coreos/go-systemd/v22 v22.3.2
6+
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534
77
github.com/mattn/go-colorable v0.1.12
88
github.com/pkg/errors v0.9.1
99
github.com/rs/xid v1.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
22
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
3+
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534 h1:rtAn27wIbmOGUs7RIbVgPEjb31ehTVniDwPGXyMxm5U=
4+
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
35
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
46
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
57
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=

journald/journald.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !windows
12
// +build !windows
23

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

7172
func (w journalWriter) Write(p []byte) (n int, err error) {
72-
if !journal.Enabled() {
73-
err = fmt.Errorf("cannot connect to journalD")
74-
return
75-
}
76-
7773
var event map[string]interface{}
7874
origPLen := len(p)
7975
p = cbor.DecodeIfBinaryToBytes(p)
8076
d := json.NewDecoder(bytes.NewReader(p))
8177
d.UseNumber()
8278
err = d.Decode(&event)
8379
jPrio := defaultJournalDPrio
84-
args := make(map[string]string, 0)
80+
args := make(map[string]string)
8581
if err != nil {
8682
return
8783
}
@@ -100,9 +96,9 @@ func (w journalWriter) Write(p []byte) (n int, err error) {
10096
continue
10197
}
10298

103-
switch value.(type) {
99+
switch v := value.(type) {
104100
case string:
105-
args[jKey], _ = value.(string)
101+
args[jKey] = v
106102
case json.Number:
107103
args[jKey] = fmt.Sprint(value)
108104
default:

0 commit comments

Comments
 (0)