mirror of https://github.com/milvus-io/milvus.git
parent
b0524adbd8
commit
79516dacd2
|
@ -101,7 +101,7 @@ func (cfg *Config) buildOptions(errSink zapcore.WriteSyncer) []zap.Option {
|
|||
|
||||
if cfg.Sampling != nil {
|
||||
opts = append(opts, zap.WrapCore(func(core zapcore.Core) zapcore.Core {
|
||||
return zapcore.NewSampler(core, time.Second, cfg.Sampling.Initial, cfg.Sampling.Thereafter)
|
||||
return zapcore.NewSamplerWithOptions(core, time.Second, cfg.Sampling.Initial, cfg.Sampling.Thereafter, zapcore.SamplerHook(cfg.Sampling.Hook))
|
||||
}))
|
||||
}
|
||||
return opts
|
||||
|
|
|
@ -98,3 +98,33 @@ func TestLevelGetterAndSetter(t *testing.T) {
|
|||
SetLevel(zap.ErrorLevel)
|
||||
assert.Equal(t, zap.ErrorLevel, GetLevel())
|
||||
}
|
||||
|
||||
func TestSampling(t *testing.T) {
|
||||
sample, drop := make(chan zapcore.SamplingDecision, 1), make(chan zapcore.SamplingDecision, 1)
|
||||
samplingConf := zap.SamplingConfig{
|
||||
Initial: 1,
|
||||
Thereafter: 2,
|
||||
Hook: func(entry zapcore.Entry, decision zapcore.SamplingDecision) {
|
||||
switch decision {
|
||||
case zapcore.LogSampled:
|
||||
sample <- decision
|
||||
case zapcore.LogDropped:
|
||||
drop <- decision
|
||||
}
|
||||
},
|
||||
}
|
||||
conf := &Config{Level: "debug", File: FileLogConfig{}, Sampling: &samplingConf}
|
||||
|
||||
ts := newTestLogSpy(t)
|
||||
logger, p, _ := InitTestLogger(ts, conf)
|
||||
ReplaceGlobals(logger, p)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
Debug("test")
|
||||
if i%2 == 0 {
|
||||
<-sample
|
||||
} else {
|
||||
<-drop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
|
@ -37,6 +36,7 @@ import (
|
|||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
@ -242,6 +242,28 @@ func TestWithOptions(t *testing.T) {
|
|||
ts.assertMessagesNotContains("stack")
|
||||
}
|
||||
|
||||
func TestNamedLogger(t *testing.T) {
|
||||
ts := newTestLogSpy(t)
|
||||
conf := &Config{
|
||||
Level: "debug",
|
||||
DisableTimestamp: true,
|
||||
DisableErrorVerbose: true,
|
||||
}
|
||||
logger, _, _ := InitTestLogger(ts, conf, zap.AddStacktrace(zapcore.FatalLevel))
|
||||
namedLogger := logger.Named("testLogger")
|
||||
namedLogger.Error("testing")
|
||||
ts.assertMessagesContains("testLogger")
|
||||
}
|
||||
|
||||
func TestErrorLog(t *testing.T) {
|
||||
ts := newTestLogSpy(t)
|
||||
conf := &Config{Level: "debug", DisableTimestamp: true}
|
||||
logger, _, _ := InitTestLogger(ts, conf)
|
||||
logger.Error("", zap.NamedError("err", errors.New("log-stack-test")))
|
||||
ts.assertMessagesContains("[err=log-stack-test]")
|
||||
ts.assertMessagesContains("] [errVerbose=\"")
|
||||
}
|
||||
|
||||
// testLogSpy is a testing.TB that captures logged messages.
|
||||
type testLogSpy struct {
|
||||
testing.TB
|
||||
|
|
Loading…
Reference in New Issue