Skip to content

Commit ef67c62

Browse files
authored
Fix the data race in the Option implementation (#20)
1 parent 8a9fae7 commit ef67c62

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.2.2
1+
v0.2.3

base_logger.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package log
22

33
import (
44
"fmt"
5+
"sync"
56

67
"github.com/no-src/log/content"
78
"github.com/no-src/log/formatter"
@@ -16,6 +17,7 @@ type baseLogger struct {
1617
f formatter.Formatter
1718
appendTime bool
1819
timeFormat string
20+
optMu sync.RWMutex // protect Option
1921
}
2022

2123
func (l *baseLogger) Debug(format string, args ...interface{}) {
@@ -49,7 +51,11 @@ func (l *baseLogger) log(lvl level.Level, format string, args ...interface{}) {
4951

5052
func (l *baseLogger) logWithErr(err error, lvl level.Level, format string, args ...interface{}) {
5153
if checkLogLevel(l.lvl, lvl) {
52-
data, _ := l.f.Serialize(content.NewContent(lvl, err, l.appendTime, l.timeFormat, format, args...))
54+
l.optMu.RLock()
55+
c := content.NewContent(lvl, err, l.appendTime, l.timeFormat, format, args...)
56+
f := l.f
57+
l.optMu.RUnlock()
58+
data, _ := f.Serialize(c)
5359
l.Log(string(data))
5460
}
5561
}
@@ -72,15 +78,19 @@ func (l *baseLogger) init(w Writer, lvl level.Level, appendTime bool) {
7278

7379
func (l *baseLogger) setFormatter(f formatter.Formatter) {
7480
if f != nil {
81+
l.optMu.Lock()
7582
l.f = f
83+
l.optMu.Unlock()
7684
}
7785
}
7886

7987
func (l *baseLogger) setTimeFormat(f string) {
8088
if len(f) == 0 {
8189
f = content.DefaultLogTimeFormat()
8290
}
91+
l.optMu.Lock()
8392
l.timeFormat = f
93+
l.optMu.Unlock()
8494
}
8595

8696
func checkLogLevel(lvl level.Level, currentLevel level.Level) bool {

file_logger_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@ func TestFileLogger_WithAutoFlushWithFlushDelay(t *testing.T) {
7979
<-time.After(wait * 20)
8080
}
8181

82-
func TestConsoleLoggerAndFileLogger(t *testing.T) {
83-
fLogger, err := NewFileLogger(level.DebugLevel, "./multi_logs", "ns")
84-
if err != nil {
85-
t.Fatal(err)
86-
}
87-
InitDefaultLogger(NewMultiLogger(NewConsoleLogger(level.DebugLevel), fLogger))
88-
defer Close()
89-
testLogs(t)
90-
}
91-
9282
func TestFileLogger_WithSplitDate(t *testing.T) {
9383
fLogger, err := NewFileLoggerWithOption(option.FileLoggerOption{
9484
Level: level.DebugLevel,

logger_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/no-src/log/content"
89
"github.com/no-src/log/formatter"
910
"github.com/no-src/log/internal/sync"
1011
"github.com/no-src/log/level"
@@ -17,6 +18,7 @@ var (
1718
)
1819

1920
func testLogs(t *testing.T) {
21+
DefaultLogger().WithFormatter(formatter.Default()).WithTimeFormat(content.DefaultLogTimeFormat())
2022
Debug("%s %s, test debug log", "hello", "world")
2123
Info("%s %s, test info log", "hello", "world")
2224
Warn("%s %s, test warn log", "hello", "world")

multi_logger_test.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,30 @@ import (
1010

1111
func TestMultiLogger(t *testing.T) {
1212
testCases := []struct {
13-
name string
14-
formatter string
13+
name string
14+
formatter string
15+
concurrency bool
16+
timeFormat string
1517
}{
16-
{"TextFormatter", formatter.TextFormatter},
17-
{"JsonFormatter", formatter.JsonFormatter},
18+
{"TextFormatter", formatter.TextFormatter, false, testTimeFormat},
19+
{"JsonFormatter", formatter.JsonFormatter, false, testTimeFormat},
20+
{"TextFormatter Concurrency", formatter.TextFormatter, true, ""},
21+
{"JsonFormatter Concurrency", formatter.JsonFormatter, true, ""},
1822
}
1923
for _, tc := range testCases {
2024
t.Run(tc.name, func(t *testing.T) {
21-
InitDefaultLogger(NewMultiLogger(NewConsoleLogger(level.DebugLevel).WithFormatter(formatter.New(tc.formatter))))
25+
fLogger, err := NewFileLogger(level.DebugLevel, "./multi_logs", "ns"+tc.formatter)
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
InitDefaultLogger(NewMultiLogger(NewConsoleLogger(level.DebugLevel), fLogger).WithFormatter(formatter.New(tc.formatter)).WithTimeFormat(tc.timeFormat))
2230
defer Close()
23-
testLogs(t)
31+
32+
if tc.concurrency {
33+
testLogsConcurrency(t, "TestMultiLogger")
34+
} else {
35+
testLogs(t)
36+
}
2437
})
2538
}
2639
}

sample_logger_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"testing"
66

7+
"github.com/no-src/log/content"
78
"github.com/no-src/log/formatter"
89
"github.com/no-src/log/level"
910
)
@@ -31,6 +32,7 @@ func TestSampleLogger(t *testing.T) {
3132
}
3233

3334
func testSampleLogs() {
35+
DefaultSampleLogger().WithFormatter(formatter.Default()).WithTimeFormat(content.DefaultLogTimeFormat())
3436
DebugSample("[sample] %s %s, test debug log", "hello", "world")
3537
InfoSample("[sample] %s %s, test info log", "hello", "world")
3638
WarnSample("[sample] %s %s, test warn log", "hello", "world")

0 commit comments

Comments
 (0)