Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
Use the more API friendly zerolog instead of slog
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjp committed Apr 29, 2023
1 parent ca77e22 commit a0e94ec
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 62 deletions.
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ go_library(
"@com_github_prometheus_client_golang//prometheus",
"@com_github_prometheus_client_golang//prometheus/promauto",
"@com_github_prometheus_client_golang//prometheus/promhttp",
"@com_github_rs_zerolog//:zerolog",
"@com_github_sourcegraph_conc//pool",
"@org_golang_x_exp//slog",
],
)

Expand Down
36 changes: 28 additions & 8 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def go_dependencies():
go_repository(
name = "com_github_coreos_go_systemd_v22",
importpath = "github.com/coreos/go-systemd/v22",
sum = "h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=",
version = "v22.3.2",
sum = "h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=",
version = "v22.5.0",
)

go_repository(
Expand Down Expand Up @@ -149,6 +149,13 @@ def go_dependencies():
sum = "h1:4mx0EYENAdX/B/rbunjlt5+4RTA/a9SMHBRuSKdGxPM=",
version = "v1.0.2",
)
go_repository(
name = "com_github_godbus_dbus_v5",
importpath = "github.com/godbus/dbus/v5",
sum = "h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=",
version = "v5.0.4",
)

go_repository(
name = "com_github_gogo_protobuf",
importpath = "github.com/gogo/protobuf",
Expand Down Expand Up @@ -294,14 +301,14 @@ def go_dependencies():
go_repository(
name = "com_github_mattn_go_colorable",
importpath = "github.com/mattn/go-colorable",
sum = "h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=",
version = "v0.1.8",
sum = "h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=",
version = "v0.1.13",
)
go_repository(
name = "com_github_mattn_go_isatty",
importpath = "github.com/mattn/go-isatty",
sum = "h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=",
version = "v0.0.14",
sum = "h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=",
version = "v0.0.18",
)
go_repository(
name = "com_github_matttproud_golang_protobuf_extensions",
Expand Down Expand Up @@ -450,6 +457,19 @@ def go_dependencies():
sum = "h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=",
version = "v1.9.0",
)
go_repository(
name = "com_github_rs_xid",
importpath = "github.com/rs/xid",
sum = "h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=",
version = "v1.4.0",
)
go_repository(
name = "com_github_rs_zerolog",
importpath = "github.com/rs/zerolog",
sum = "h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=",
version = "v1.29.1",
)

go_repository(
name = "com_github_sirupsen_logrus",
importpath = "github.com/sirupsen/logrus",
Expand Down Expand Up @@ -639,8 +659,8 @@ def go_dependencies():
go_repository(
name = "org_golang_x_sys",
importpath = "golang.org/x/sys",
sum = "h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=",
version = "v0.6.0",
sum = "h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=",
version = "v0.7.0",
)

go_repository(
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ require (
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rs/zerolog v1.29.1 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/sys v0.7.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -11,15 +12,24 @@ github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.15.0 h1:5fCgGYogn0hFdhyhLbw7hEsWxufKtY9klyvdNfFlFhM=
Expand All @@ -30,6 +40,9 @@ github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -42,8 +55,13 @@ go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTV
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/service/debug/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ go_library(
"@com_github_go_kit_kit//endpoint",
"@com_github_go_kit_kit//transport/http",
"@com_github_prometheus_client_golang//prometheus",
"@org_golang_x_exp//slog",
"@com_github_rs_zerolog//:zerolog",
],
)
36 changes: 18 additions & 18 deletions internal/pkg/service/debug/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,49 @@ package debug
import (
"time"

"golang.org/x/exp/slog"
"github.com/rs/zerolog"
)

type loggingMiddleware struct {
logger *slog.Logger
logger zerolog.Logger
svc Service
}

func NewLoggingservice(logger *slog.Logger, s Service) Service {
func NewLoggingservice(logger zerolog.Logger, s Service) Service {
return loggingMiddleware{logger, s}
}

func (lm loggingMiddleware) Version() (output string) {
defer func(begin time.Time) {
lm.logger.Info("Request processed",
"method", "version",
"output", output,
"took", time.Since(begin),
)
lm.logger.Info().
Str("method", "version").
Str("output", output).
Dur("took", time.Since(begin)).
Msg("Request processed")
}(time.Now())

return lm.svc.Version()
}

func (lm loggingMiddleware) BuildTimestamp() (output string) {
defer func(begin time.Time) {
lm.logger.Info("Request processed",
"method", "buildtimestamp",
"output", output,
"took", time.Since(begin),
)
lm.logger.Info().
Str("method", "buildtimestamp").
Str("output", output).
Dur("took", time.Since(begin)).
Msg("Request processed")
}(time.Now())

return lm.svc.BuildTimestamp()
}

func (lm loggingMiddleware) Environment() (output []string) {
defer func(begin time.Time) {
lm.logger.Info("Request processed",
"method", "environment",
"output", output,
"took", time.Since(begin),
)
lm.logger.Info().
Str("method", "environment").
Strs("output", output).
Dur("took", time.Since(begin)).
Msg("Request processed")
}(time.Now())

return lm.svc.Environment()
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/service/string/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ go_library(
"@com_github_go_kit_kit//endpoint",
"@com_github_go_kit_kit//transport/http",
"@com_github_prometheus_client_golang//prometheus",
"@org_golang_x_exp//slog",
"@com_github_rs_zerolog//:zerolog",
],
)
32 changes: 16 additions & 16 deletions internal/pkg/service/string/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@ package string
import (
"time"

"golang.org/x/exp/slog"
"github.com/rs/zerolog"
)

type loggingMiddleware struct {
logger *slog.Logger
logger zerolog.Logger
svc Service
}

func NewLoggingService(logger *slog.Logger, s Service) Service {
func NewLoggingService(logger zerolog.Logger, s Service) Service {
return &loggingMiddleware{logger, s}
}

func (lm loggingMiddleware) Uppercase(s string) (output string, err error) {
defer func(begin time.Time) {
lm.logger.Info("Request processed",
"method", "uppercase",
"input", s,
"output", output,
"err", err,
"took", time.Since(begin),
)
lm.logger.Info().
Str("method", "uppercase").
Str("input", s).
Str("output", output).
Err(err).
Dur("took", time.Since(begin)).
Msg("Request processed")
}(time.Now())

return lm.svc.Uppercase(s)
}

func (lm loggingMiddleware) Count(s string) (n int) {
defer func(begin time.Time) {
lm.logger.Info("Request processed",
"method", "count",
"input", s,
"n", n,
"took", time.Since(begin),
)
lm.logger.Info().
Str("method", "count").
Str("input", s).
Int("n", n).
Dur("took", time.Since(begin)).
Msg("Request processed")
}(time.Now())

return lm.svc.Count(s)
Expand Down
Loading

0 comments on commit a0e94ec

Please sign in to comment.