Skip to content

Commit 13f2b53

Browse files
committed
LogSampled
1 parent 9c57685 commit 13f2b53

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

emf/logger.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"math/rand"
78
"os"
89
"strings"
910
"time"
@@ -169,6 +170,15 @@ func (l *Logger) Log() {
169170
_, _ = fmt.Fprintln(l.out, string(buf))
170171
}
171172

173+
// LogSampled samples Log calls based on a rate.
174+
// Ex. rate of 0.7 means Log will be called 70% of a time.
175+
func (l *Logger) LogSampled(rate float64) {
176+
rand.Seed(time.Now().UnixNano())
177+
if rand.Float64() < rate {
178+
l.Log()
179+
}
180+
}
181+
172182
// NewContext creates new context for given logger.
173183
func (l *Logger) NewContext() *Context {
174184
c := newContext(l.values)

0 commit comments

Comments
 (0)