MCPcopy
hub / github.com/uber-go/zap / TestConfigWithSamplingHook

Function TestConfigWithSamplingHook

config_test.go:161–203  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

159}
160
161func TestConfigWithSamplingHook(t *testing.T) {
162 shook, dcount, scount := makeSamplerCountingHook()
163 cfg := Config{
164 Level: NewAtomicLevelAt(InfoLevel),
165 Development: false,
166 Sampling: &SamplingConfig{
167 Initial: 100,
168 Thereafter: 100,
169 Hook: shook,
170 },
171 Encoding: "json",
172 EncoderConfig: NewProductionEncoderConfig(),
173 OutputPaths: []string{"stderr"},
174 ErrorOutputPaths: []string{"stderr"},
175 }
176 expectRe := `{"level":"info","caller":"[a-z0-9_-]+/config_test.go:\d+","msg":"info","k":"v","z":"zz"}` + "\n" +
177 `{"level":"warn","caller":"[a-z0-9_-]+/config_test.go:\d+","msg":"warn","k":"v","z":"zz"}` + "\n"
178 expectDropped := 99 // 200 - 100 initial - 1 thereafter
179 expectSampled := 103 // 2 from initial + 100 + 1 thereafter
180
181 logOut := filepath.Join(t.TempDir(), "test.log")
182 cfg.OutputPaths = []string{logOut}
183 cfg.EncoderConfig.TimeKey = "" // no timestamps in tests
184 cfg.InitialFields = map[string]interface{}{"z": "zz", "k": "v"}
185
186 logger, err := cfg.Build()
187 require.NoError(t, err, "Unexpected error constructing logger.")
188
189 logger.Debug("debug")
190 logger.Info("info")
191 logger.Warn("warn")
192
193 byteContents, err := os.ReadFile(logOut)
194 require.NoError(t, err, "Couldn't read log contents from temp file.")
195 logs := string(byteContents)
196 assert.Regexp(t, expectRe, logs, "Unexpected log output.")
197
198 for i := 0; i < 200; i++ {
199 logger.Info("sampling")
200 }
201 assert.Equal(t, int64(expectDropped), dcount.Load())
202 assert.Equal(t, int64(expectSampled), scount.Load())
203}

Callers

nothing calls this directly

Calls 7

BuildMethod · 0.95
NewAtomicLevelAtFunction · 0.85
makeSamplerCountingHookFunction · 0.70
DebugMethod · 0.45
InfoMethod · 0.45
WarnMethod · 0.45

Tested by

no test coverage detected