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:

75f33b08db

This is the only journal related change since the latest stable release
This commit is contained in:
Nicola Murino 2022-02-03 14:49:04 +01:00 committed by GitHub
parent 3efdd82416
commit 025f9f1819
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 9 deletions

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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: