Skip to content

Commit c21892b

Browse files
authored
Fixed Enabled() for lazy evaluation and supporting >2 V-level (#25)
Add SetMaxV to reduce level-mapping confusion
1 parent 8ee7818 commit c21892b

File tree

7 files changed

+64
-54
lines changed

7 files changed

+64
-54
lines changed

.github/workflows/lint.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
- uses: actions/checkout@v2
88
- uses: actions/setup-go@v2
99
with:
10-
go-version: "^1.17"
10+
go-version: "^1.19"
1111
- uses: golangci/golangci-lint-action@v2
1212
with:
1313
version: latest

.github/workflows/test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ jobs:
77
- uses: actions/checkout@v2
88
- uses: actions/setup-go@v2
99
with:
10-
go-version: "^1.17"
10+
go-version: "^1.19"
1111
- run: go test ./...

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import (
1919

2020
func main() {
2121
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
22+
2223
zerologr.NameFieldName = "logger"
2324
zerologr.NameSeparator = "/"
25+
zerologr.SetMaxV(1)
2426

2527
zl := zerolog.New(os.Stderr)
2628
zl = zl.With().Caller().Timestamp().Logger()
@@ -34,8 +36,4 @@ func main() {
3436

3537
For the most part, concepts in Zerolog correspond directly with those in logr.
3638

37-
Levels in logr correspond to custom debug levels in Zerolog. Any given level
38-
in logr is represents by `zerologLevel = 1 - logrLevel`.
39-
40-
For example `V(2)` is equivalent to Zerolog's `TraceLevel`, while `V(1)` is
41-
equivalent to Zerolog's `DebugLevel`.
39+
V-levels in logr correspond to levels in Zerolog as `zerologLevel = 1 - logrV`. `logr.V(0)` is equivalent to `zerolog.InfoLevel` or 1; `logr.V(1)` is equivalent to `zerolog.DebugLevel` or 0 (default global level in Zerolog); `logr.V(2)` is equivalent to `zerolog.TraceLevel` or -1. Higher than 2 V-level is possible but misses some features in Zerolog, e.g. Hooks and Sampling. V-level value is a number and is only logged on Info(), not Error().

example/example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func helper2(log logr.Logger, msg string) {
4444
}
4545

4646
func ExampleNew() {
47-
zerolog.SetGlobalLevel(zerolog.DebugLevel)
47+
zerologr.SetMaxV(1)
4848
zl := zerolog.New(os.Stdout)
4949
log := zerologr.New(&zl)
5050
log = log.WithName("MyName")

go.mod

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ module github.com/go-logr/zerologr
33
go 1.17
44

55
require (
6-
github.com/go-logr/logr v1.2.2
7-
github.com/rs/zerolog v1.26.1
6+
github.com/go-logr/logr v1.2.3
7+
github.com/rs/zerolog v1.29.0
8+
)
9+
10+
require (
11+
github.com/mattn/go-colorable v0.1.12 // indirect
12+
github.com/mattn/go-isatty v0.0.14 // indirect
13+
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
814
)

go.sum

+13-32
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
1-
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
2-
github.com/go-logr/logr v1.2.2 h1:ahHml/yUpnlb96Rp8HCvtYVPY8ZYpxq3g7UYchIYwbs=
3-
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1+
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
2+
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
3+
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
44
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
5+
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
6+
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
7+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
8+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
59
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
6-
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
7-
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
8-
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
9-
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
10-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
11-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
12-
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
13-
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
14-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
15-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
16-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
17-
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
18-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
19-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
20-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
21-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
22-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
23-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
24-
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
26-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
27-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
28-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
29-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
30-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
31-
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
32-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
33-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
34-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
10+
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
11+
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
12+
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
13+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14+
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
15+
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

zerologr.go

+37-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
// Package zerologr defines an implementation of the github.com/go-logr/logr
22
// interfaces built on top of Zerolog (https://github.com/rs/zerolog).
33
//
4-
// Usage
4+
// # Usage
55
//
66
// A new logr.Logger can be constructed from an existing zerolog.Logger using
77
// the New function:
88
//
9-
// log := zerologr.New(someZeroLogger)
9+
// log := zerologr.New(someZeroLogger)
1010
//
11-
// Implementation Details
11+
// # Implementation Details
1212
//
1313
// For the most part, concepts in Zerolog correspond directly with those in
1414
// logr.
1515
//
16-
// Levels in logr correspond to custom debug levels in Zerolog. Any given level
17-
// in logr is represents by `zerologLevel = 1 - logrLevel`.
18-
// For example V(2) is equivalent to Zerolog's TraceLevel, while V(1) is
19-
// equivalent to Zerolog's DebugLevel. Verbosity value is a number and is only
16+
// V-levels in logr correspond to levels in Zerolog as `zerologLevel = 1 - logrV`.
17+
// `logr.V(0)` is equivalent to `zerolog.InfoLevel` or 1; `logr.V(1)` is equivalent to
18+
// `zerolog.DebugLevel` or 0 (default global level in Zerolog); `logr.V(2)` is equivalent
19+
// to `zerolog.TraceLevel` or -1. Higher than 2 V-level is possible but misses some
20+
// features in Zerolog, e.g. Hooks and Sampling. V-level value is a number and is only
2021
// logged on Info(), not Error().
2122
package zerologr
2223

@@ -37,12 +38,17 @@ var (
3738
VerbosityFieldName = "v"
3839

3940
// RenderArgsHook mutates the list of key-value pairs passed directly to
40-
// logr.Info and logr.Error.
41+
// logr.Info and logr.Error. If set to nil, it is disabled.
4142
RenderArgsHook = DefaultRender
4243
// RenderValuesHook mutates the list of key-value pairs saved via logr.WithValues.
44+
// If set to nil, it is disabled.
4345
RenderValuesHook = DefaultRender
4446
)
4547

48+
const (
49+
minZerologLevel = -128 // zerolog.Level is int8
50+
)
51+
4652
// Logger is type alias of logr.Logger.
4753
type Logger = logr.Logger
4854

@@ -66,8 +72,14 @@ var (
6672
_ logr.CallDepthLogSink = &LogSink{}
6773
)
6874

69-
// New returns a logr.Logger with logr.LogSink implemented by Zerolog.
75+
// New returns a logr.Logger with logr.LogSink implemented by Zerolog. Local level
76+
// is mutated to allow max V-level if not set explicitly, so SetMaxV alone can control
77+
// Zerolog's level. Use NewLogSink directly if local level mutation is undesirable.
7078
func New(l *zerolog.Logger) Logger {
79+
if l.GetLevel() == zerolog.TraceLevel {
80+
ll := l.Level(minZerologLevel)
81+
l = &ll
82+
}
7183
ls := NewLogSink(l)
7284
return logr.New(ls)
7385
}
@@ -77,16 +89,29 @@ func NewLogSink(l *zerolog.Logger) *LogSink {
7789
return &LogSink{l: l}
7890
}
7991

92+
// SetMaxV updates Zerolog's global level. Default max V-level is 1 (DebugLevel).
93+
// The range of max V-level is 0 through 129 inclusive, but higher than 2 V-level
94+
// misses some features in Zerolog, e.g. Hooks and Sampling.
95+
func SetMaxV(level int) {
96+
if level < 0 {
97+
level = 0
98+
}
99+
zlvl := 1 - level
100+
if zlvl < minZerologLevel {
101+
zlvl = minZerologLevel
102+
}
103+
zerolog.SetGlobalLevel(zerolog.Level(zlvl))
104+
}
105+
80106
// Init receives runtime info about the logr library.
81107
func (ls *LogSink) Init(ri logr.RuntimeInfo) {
82108
ls.depth = ri.CallDepth + 2
83109
}
84110

85111
// Enabled tests whether this LogSink is enabled at the specified V-level.
86112
func (ls *LogSink) Enabled(level int) bool {
87-
// Optimization: Info() will check level internally.
88-
const traceLevel = 1 - int(zerolog.TraceLevel)
89-
return level <= traceLevel
113+
zlvl := zerolog.Level(1 - level)
114+
return zlvl >= ls.l.GetLevel() && zlvl >= zerolog.GlobalLevel()
90115
}
91116

92117
// Info logs a non-error message at specified V-level with the given key/value pairs as context.

0 commit comments

Comments
 (0)