-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_logger.go
111 lines (90 loc) · 2.8 KB
/
default_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package log
import (
"sync"
"github.com/no-src/log/level"
)
var (
defaultLogger Logger
defaultSampleLogger Logger
mu sync.RWMutex
)
const defaultSampleRate = 1
// InitDefaultLogger init a default logger
// if not specified, default is consoleLogger with InfoLevel, and default sample rate is 1
func InitDefaultLogger(logger Logger) {
InitDefaultLoggerWithSample(logger, defaultSampleRate)
}
// InitDefaultLoggerWithSample init a default logger and sample logger
// if not specified, default is consoleLogger with InfoLevel, and default sample rate is 1
func InitDefaultLoggerWithSample(logger Logger, sampleRate float64) {
mu.Lock()
defer mu.Unlock()
defaultLogger = logger
if defaultLogger == nil {
defaultLogger = NewEmptyLogger()
}
defaultSampleLogger = NewDefaultSampleLogger(defaultLogger, sampleRate)
}
// Debug write the debug log
func Debug(format string, args ...any) {
DefaultLogger().Debug(format, args...)
}
// Info write the info log
func Info(format string, args ...any) {
DefaultLogger().Info(format, args...)
}
// Warn write the warn log
func Warn(format string, args ...any) {
DefaultLogger().Warn(format, args...)
}
// Error write the error log
func Error(err error, format string, args ...any) {
DefaultLogger().Error(err, format, args...)
}
// ErrorIf write the error log if err is not nil
func ErrorIf(err error, format string, args ...any) error {
return DefaultLogger().ErrorIf(err, format, args...)
}
// DebugSample write the debug log by random sampling
func DebugSample(format string, args ...any) {
DefaultSampleLogger().Debug(format, args...)
}
// InfoSample write the info log by random sampling
func InfoSample(format string, args ...any) {
DefaultSampleLogger().Info(format, args...)
}
// WarnSample write the warn log by random sampling
func WarnSample(format string, args ...any) {
DefaultSampleLogger().Warn(format, args...)
}
// ErrorSample write the error log by random sampling
func ErrorSample(err error, format string, args ...any) {
DefaultSampleLogger().Error(err, format, args...)
}
// ErrorIfSample write the error log by random sampling if err is not nil
func ErrorIfSample(err error, format string, args ...any) error {
return DefaultSampleLogger().ErrorIf(err, format, args...)
}
// Log write the log without level
func Log(format string, args ...any) {
DefaultLogger().Log(format, args...)
}
// Close close the current logger
func Close() error {
return DefaultLogger().Close()
}
// DefaultLogger return the global default logger
func DefaultLogger() Logger {
mu.RLock()
defer mu.RUnlock()
return defaultLogger
}
// DefaultSampleLogger return the global default sample logger
func DefaultSampleLogger() Logger {
mu.RLock()
defer mu.RUnlock()
return defaultSampleLogger
}
func init() {
InitDefaultLogger(NewConsoleLogger(level.InfoLevel))
}