Skip to content

Commit

Permalink
Fix the data race in the Option implementation (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev authored Jun 27, 2023
1 parent 8a9fae7 commit ef67c62
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.2
v0.2.3
12 changes: 11 additions & 1 deletion base_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"fmt"
"sync"

"github.com/no-src/log/content"
"github.com/no-src/log/formatter"
Expand All @@ -16,6 +17,7 @@ type baseLogger struct {
f formatter.Formatter
appendTime bool
timeFormat string
optMu sync.RWMutex // protect Option
}

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

func (l *baseLogger) logWithErr(err error, lvl level.Level, format string, args ...interface{}) {
if checkLogLevel(l.lvl, lvl) {
data, _ := l.f.Serialize(content.NewContent(lvl, err, l.appendTime, l.timeFormat, format, args...))
l.optMu.RLock()
c := content.NewContent(lvl, err, l.appendTime, l.timeFormat, format, args...)
f := l.f
l.optMu.RUnlock()
data, _ := f.Serialize(c)
l.Log(string(data))
}
}
Expand All @@ -72,15 +78,19 @@ func (l *baseLogger) init(w Writer, lvl level.Level, appendTime bool) {

func (l *baseLogger) setFormatter(f formatter.Formatter) {
if f != nil {
l.optMu.Lock()
l.f = f
l.optMu.Unlock()
}
}

func (l *baseLogger) setTimeFormat(f string) {
if len(f) == 0 {
f = content.DefaultLogTimeFormat()
}
l.optMu.Lock()
l.timeFormat = f
l.optMu.Unlock()
}

func checkLogLevel(lvl level.Level, currentLevel level.Level) bool {
Expand Down
10 changes: 0 additions & 10 deletions file_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ func TestFileLogger_WithAutoFlushWithFlushDelay(t *testing.T) {
<-time.After(wait * 20)
}

func TestConsoleLoggerAndFileLogger(t *testing.T) {
fLogger, err := NewFileLogger(level.DebugLevel, "./multi_logs", "ns")
if err != nil {
t.Fatal(err)
}
InitDefaultLogger(NewMultiLogger(NewConsoleLogger(level.DebugLevel), fLogger))
defer Close()
testLogs(t)
}

func TestFileLogger_WithSplitDate(t *testing.T) {
fLogger, err := NewFileLoggerWithOption(option.FileLoggerOption{
Level: level.DebugLevel,
Expand Down
2 changes: 2 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/no-src/log/content"
"github.com/no-src/log/formatter"
"github.com/no-src/log/internal/sync"
"github.com/no-src/log/level"
Expand All @@ -17,6 +18,7 @@ var (
)

func testLogs(t *testing.T) {
DefaultLogger().WithFormatter(formatter.Default()).WithTimeFormat(content.DefaultLogTimeFormat())
Debug("%s %s, test debug log", "hello", "world")
Info("%s %s, test info log", "hello", "world")
Warn("%s %s, test warn log", "hello", "world")
Expand Down
25 changes: 19 additions & 6 deletions multi_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,30 @@ import (

func TestMultiLogger(t *testing.T) {
testCases := []struct {
name string
formatter string
name string
formatter string
concurrency bool
timeFormat string
}{
{"TextFormatter", formatter.TextFormatter},
{"JsonFormatter", formatter.JsonFormatter},
{"TextFormatter", formatter.TextFormatter, false, testTimeFormat},
{"JsonFormatter", formatter.JsonFormatter, false, testTimeFormat},
{"TextFormatter Concurrency", formatter.TextFormatter, true, ""},
{"JsonFormatter Concurrency", formatter.JsonFormatter, true, ""},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
InitDefaultLogger(NewMultiLogger(NewConsoleLogger(level.DebugLevel).WithFormatter(formatter.New(tc.formatter))))
fLogger, err := NewFileLogger(level.DebugLevel, "./multi_logs", "ns"+tc.formatter)
if err != nil {
t.Fatal(err)
}
InitDefaultLogger(NewMultiLogger(NewConsoleLogger(level.DebugLevel), fLogger).WithFormatter(formatter.New(tc.formatter)).WithTimeFormat(tc.timeFormat))
defer Close()
testLogs(t)

if tc.concurrency {
testLogsConcurrency(t, "TestMultiLogger")
} else {
testLogs(t)
}
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions sample_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"testing"

"github.com/no-src/log/content"
"github.com/no-src/log/formatter"
"github.com/no-src/log/level"
)
Expand Down Expand Up @@ -31,6 +32,7 @@ func TestSampleLogger(t *testing.T) {
}

func testSampleLogs() {
DefaultSampleLogger().WithFormatter(formatter.Default()).WithTimeFormat(content.DefaultLogTimeFormat())
DebugSample("[sample] %s %s, test debug log", "hello", "world")
InfoSample("[sample] %s %s, test info log", "hello", "world")
WarnSample("[sample] %s %s, test warn log", "hello", "world")
Expand Down

0 comments on commit ef67c62

Please sign in to comment.