zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

Ability to remove certain events from the sampler.

Open aryehklein-rise opened this issue 10 months ago • 1 comments

i would like to add a sampler but there are certain logs that i want them not to be sampled. for example i have an http server, and if there is a panic on certain requests, i don't want to crash the server, but i catch the request and log it. i want to always catch those regardless of the sample rates.

aryehklein-rise avatar May 08 '25 14:05 aryehklein-rise

i can implement something like this:

type LogWriter struct {
	io.Writer
	sampler    zerolog.Sampler
	notSampled *regexp.Regexp
}
func (w *LogWriter) Write(p []byte) (n int, err error) {
	if w.notSampled != nil && w.notSampled.Match(p) {
		return w.Writer.Write(p)
	}

	if w.sampler.Sample(zerolog.NoLevel) {
		return w.Writer.Write(p)
	}

	return 0, nil
}

but not sure this is the best way, maybe just use the noLevel for my critical events and then do a sampler for all other levels?

aryehklein-rise avatar May 08 '25 16:05 aryehklein-rise