Skip to content

Commit 78ea3b6

Browse files
Disable sampling for debug levels
1 parent 6b3a55e commit 78ea3b6

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

pkg/log/zap/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (ev *levelFlag) Set(flagValue string) error {
9393
if err != nil {
9494
return fmt.Errorf("invalid log level \"%s\"", flagValue)
9595
}
96-
if logLevel >= 0 {
96+
if logLevel > 0 {
9797
intLevel := -1 * logLevel
9898
ev.setFunc(zap.NewAtomicLevelAt(zapcore.Level(int8(intLevel))))
9999
} else {

pkg/log/zap/zap.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,16 @@ func (o *Options) addDefaults() {
181181
lvl := zap.NewAtomicLevelAt(zap.ErrorLevel)
182182
o.StacktraceLevel = &lvl
183183
}
184-
o.ZapOpts = append(o.ZapOpts,
185-
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
186-
return zapcore.NewSampler(core, time.Second, 100, 100)
187-
}))
184+
// Turn off sampling for all debug levels.
185+
if o.Level.Enabled(zapcore.DebugLevel) {
186+
o.ZapOpts = append(o.ZapOpts, zap.Development())
187+
} else {
188+
o.ZapOpts = append(o.ZapOpts,
189+
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
190+
return zapcore.NewSampler(core, time.Second, 100, 100)
191+
}))
192+
}
188193
}
189-
190194
o.ZapOpts = append(o.ZapOpts, zap.AddStacktrace(o.StacktraceLevel))
191195
}
192196

pkg/log/zap/zap_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ var _ = Describe("Zap log level flag options setup", func() {
307307
info = "info text"
308308
debug1 = "debug 1 text"
309309
debug2 = "debug 2 text"
310+
debug3 = "debug 3 text"
310311
)
311312

312313
BeforeEach(func() {
@@ -370,8 +371,25 @@ var _ = Describe("Zap log level flag options setup", func() {
370371
Expect(string(outRaw)).Should(ContainSubstring(debug1))
371372
})
372373

374+
It("Should set integer debug level 1 for zap-log-level flag.", func() {
375+
args := []string{"--zap-log-level=1", "--zap-devel=true"}
376+
fromFlags.BindFlags(&fs)
377+
err := fs.Parse(args)
378+
Expect(err).ToNot(HaveOccurred())
379+
logOut := new(bytes.Buffer)
380+
381+
logger := New(UseFlagOptions(&fromFlags), WriteTo(logOut))
382+
logger.V(0).Info(info)
383+
logger.V(1).Info(debug1)
384+
385+
outRaw := logOut.Bytes()
386+
387+
Expect(string(outRaw)).Should(ContainSubstring(info))
388+
Expect(string(outRaw)).Should(ContainSubstring(debug1))
389+
})
390+
373391
It("Should set integer debug level 2 for zap-log-level flag.", func() {
374-
args := []string{"--zap-log-level=2"}
392+
args := []string{"--zap-log-level=2", "--zap-devel=true"}
375393
fromFlags.BindFlags(&fs)
376394
err := fs.Parse(args)
377395
Expect(err).ToNot(HaveOccurred())
@@ -389,6 +407,25 @@ var _ = Describe("Zap log level flag options setup", func() {
389407
Expect(string(outRaw)).Should(ContainSubstring(debug2))
390408

391409
})
410+
It("Should set integer debug level 3 for zap-log-level flag.", func() {
411+
args := []string{"--zap-log-level=3", "--zap-devel=false"}
412+
fromFlags.BindFlags(&fs)
413+
err := fs.Parse(args)
414+
Expect(err).ToNot(HaveOccurred())
415+
logOut := new(bytes.Buffer)
416+
417+
logger := New(UseFlagOptions(&fromFlags), WriteTo(logOut))
418+
logger.V(0).Info(info)
419+
logger.V(1).Info(debug1)
420+
logger.V(3).Info(debug3)
421+
422+
outRaw := logOut.Bytes()
423+
424+
Expect(string(outRaw)).Should(ContainSubstring(info))
425+
Expect(string(outRaw)).Should(ContainSubstring(debug1))
426+
Expect(string(outRaw)).Should(ContainSubstring(debug3))
427+
428+
})
392429

393430
})
394431

0 commit comments

Comments
 (0)