Skip to content

Commit 971ada5

Browse files
authored
Fix the data race of default logger and add the DefaultSampleLogger function (#5)
1 parent 6b2e2b0 commit 971ada5

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

default_logger.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package log
22

33
import (
4+
"sync"
5+
46
"github.com/no-src/log/level"
57
)
68

79
var (
810
defaultLogger Logger
911
defaultSampleLogger Logger
12+
mu sync.RWMutex
1013
)
1114

1215
const defaultSampleRate = 1
@@ -20,6 +23,8 @@ func InitDefaultLogger(logger Logger) {
2023
// InitDefaultLoggerWithSample init a default logger and sample logger
2124
// if not specified, default is consoleLogger with InfoLevel, and default sample rate is 1
2225
func InitDefaultLoggerWithSample(logger Logger, sampleRate float64) {
26+
mu.Lock()
27+
defer mu.Unlock()
2328
defaultLogger = logger
2429
if defaultLogger == nil {
2530
defaultLogger = NewEmptyLogger()
@@ -29,22 +34,22 @@ func InitDefaultLoggerWithSample(logger Logger, sampleRate float64) {
2934

3035
// Debug write the debug log
3136
func Debug(format string, args ...interface{}) {
32-
defaultLogger.Debug(format, args...)
37+
DefaultLogger().Debug(format, args...)
3338
}
3439

3540
// Info write the info log
3641
func Info(format string, args ...interface{}) {
37-
defaultLogger.Info(format, args...)
42+
DefaultLogger().Info(format, args...)
3843
}
3944

4045
// Warn write the warn log
4146
func Warn(format string, args ...interface{}) {
42-
defaultLogger.Warn(format, args...)
47+
DefaultLogger().Warn(format, args...)
4348
}
4449

4550
// Error write the error log
4651
func Error(err error, format string, args ...interface{}) {
47-
defaultLogger.Error(err, format, args...)
52+
DefaultLogger().Error(err, format, args...)
4853
}
4954

5055
// ErrorIf write the error log if err is not nil
@@ -57,22 +62,22 @@ func ErrorIf(err error, format string, args ...interface{}) error {
5762

5863
// DebugSample write the debug log by random sampling
5964
func DebugSample(format string, args ...interface{}) {
60-
defaultSampleLogger.Debug(format, args...)
65+
DefaultSampleLogger().Debug(format, args...)
6166
}
6267

6368
// InfoSample write the info log by random sampling
6469
func InfoSample(format string, args ...interface{}) {
65-
defaultSampleLogger.Info(format, args...)
70+
DefaultSampleLogger().Info(format, args...)
6671
}
6772

6873
// WarnSample write the warn log by random sampling
6974
func WarnSample(format string, args ...interface{}) {
70-
defaultSampleLogger.Warn(format, args...)
75+
DefaultSampleLogger().Warn(format, args...)
7176
}
7277

7378
// ErrorSample write the error log by random sampling
7479
func ErrorSample(err error, format string, args ...interface{}) {
75-
defaultSampleLogger.Error(err, format, args...)
80+
DefaultSampleLogger().Error(err, format, args...)
7681
}
7782

7883
// ErrorIfSample write the error log by random sampling if err is not nil
@@ -85,19 +90,28 @@ func ErrorIfSample(err error, format string, args ...interface{}) error {
8590

8691
// Log write the log without level
8792
func Log(format string, args ...interface{}) {
88-
defaultLogger.Log(format, args...)
93+
DefaultLogger().Log(format, args...)
8994
}
9095

9196
// Close close the current logger
9297
func Close() error {
93-
return defaultLogger.Close()
98+
return DefaultLogger().Close()
9499
}
95100

96101
// DefaultLogger return the global default logger
97102
func DefaultLogger() Logger {
103+
mu.RLock()
104+
defer mu.RUnlock()
98105
return defaultLogger
99106
}
100107

108+
// DefaultSampleLogger return the global default sample logger
109+
func DefaultSampleLogger() Logger {
110+
mu.RLock()
111+
defer mu.RUnlock()
112+
return defaultSampleLogger
113+
}
114+
101115
func init() {
102116
InitDefaultLogger(NewConsoleLogger(level.InfoLevel))
103117
}

logger_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ func TestBaseLogger_Close(t *testing.T) {
130130
testLogs(t)
131131
}
132132

133+
func TestReadWriteLoggerConcurrency(t *testing.T) {
134+
go func() {
135+
for i := 0; i < 10; i++ {
136+
InitDefaultLogger(NewConsoleLogger(level.DebugLevel))
137+
}
138+
}()
139+
testLogsConcurrency(t, "TestReadWriteLoggerConcurrency")
140+
defer Close()
141+
}
142+
133143
type minLogger struct {
134144
baseLogger
135145
}

0 commit comments

Comments
 (0)