-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_logger.go
81 lines (66 loc) · 1.78 KB
/
sample_logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package log
import (
"github.com/no-src/log/formatter"
"github.com/no-src/log/sample"
)
type sampleLogger struct {
sampleFunc sample.SampleFunc
rate float64
logger Logger
}
// NewDefaultSampleLogger get a sample logger with custom sample rate
func NewDefaultSampleLogger(logger Logger, sampleRate float64) Logger {
return NewSampleLogger(logger, sample.DefaultSampleFunc, sampleRate)
}
// NewSampleLogger get a sample logger with custom sample rate and sample function
func NewSampleLogger(logger Logger, sampleFunc sample.SampleFunc, sampleRate float64) Logger {
l := &sampleLogger{
sampleFunc: sampleFunc,
rate: sampleRate,
logger: logger,
}
return l
}
func (l *sampleLogger) Debug(format string, args ...any) {
if l.sample() {
l.logger.Debug(format, args...)
}
}
func (l *sampleLogger) Info(format string, args ...any) {
if l.sample() {
l.logger.Info(format, args...)
}
}
func (l *sampleLogger) Warn(format string, args ...any) {
if l.sample() {
l.logger.Warn(format, args...)
}
}
func (l *sampleLogger) Error(err error, format string, args ...any) {
if l.sample() {
l.logger.Error(err, format, args...)
}
}
func (l *sampleLogger) ErrorIf(err error, format string, args ...any) error {
return errorIf(l.Error, err, format, args...)
}
func (l *sampleLogger) Log(format string, args ...any) {
l.logger.Log(format, args...)
}
func (l *sampleLogger) Close() error {
return l.logger.Close()
}
func (l *sampleLogger) Write(p []byte) (n int, err error) {
return l.logger.Write(p)
}
func (l *sampleLogger) sample() bool {
return l.sampleFunc(l.rate)
}
func (l *sampleLogger) WithFormatter(f formatter.Formatter) Logger {
l.logger.WithFormatter(f)
return l
}
func (l *sampleLogger) WithTimeFormat(f string) Logger {
l.logger.WithTimeFormat(f)
return l
}