diff --git a/internal/log/config.go b/internal/log/config.go index be524a0be4..bf6ed35f7b 100644 --- a/internal/log/config.go +++ b/internal/log/config.go @@ -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 diff --git a/internal/log/log_test.go b/internal/log/log_test.go index 2d273022cb..3225a67c54 100644 --- a/internal/log/log_test.go +++ b/internal/log/log_test.go @@ -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 + } + } +} diff --git a/internal/log/zap_log_test.go b/internal/log/zap_log_test.go index b167ea9557..faa95b1807 100644 --- a/internal/log/zap_log_test.go +++ b/internal/log/zap_log_test.go @@ -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