chore: fix data race (#1593)
This commit is contained in:
@@ -3,6 +3,7 @@ package logx
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/timex"
|
"github.com/zeromicro/go-zero/core/timex"
|
||||||
@@ -79,7 +80,7 @@ func (l *durationLogger) WithDuration(duration time.Duration) Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *durationLogger) write(writer io.Writer, level string, val interface{}) {
|
func (l *durationLogger) write(writer io.Writer, level string, val interface{}) {
|
||||||
switch encoding {
|
switch atomic.LoadUint32(&encoding) {
|
||||||
case plainEncodingType:
|
case plainEncodingType:
|
||||||
writePlainAny(writer, level, val, l.Duration)
|
writePlainAny(writer, level, val, l.Duration)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package logx
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -38,10 +39,10 @@ func TestWithDurationInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWithDurationInfoConsole(t *testing.T) {
|
func TestWithDurationInfoConsole(t *testing.T) {
|
||||||
old := encoding
|
old := atomic.LoadUint32(&encoding)
|
||||||
encoding = plainEncodingType
|
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||||
defer func() {
|
defer func() {
|
||||||
encoding = old
|
atomic.StoreUint32(&encoding, old)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
jsonEncodingType uint32 = iota
|
jsonEncodingType = iota
|
||||||
plainEncodingType
|
plainEncodingType
|
||||||
|
|
||||||
jsonEncoding = "json"
|
jsonEncoding = "json"
|
||||||
@@ -75,7 +75,7 @@ var (
|
|||||||
timeFormat = "2006-01-02T15:04:05.000Z07:00"
|
timeFormat = "2006-01-02T15:04:05.000Z07:00"
|
||||||
writeConsole bool
|
writeConsole bool
|
||||||
logLevel uint32
|
logLevel uint32
|
||||||
encoding = jsonEncodingType
|
encoding uint32 = jsonEncodingType
|
||||||
// use uint32 for atomic operations
|
// use uint32 for atomic operations
|
||||||
disableStat uint32
|
disableStat uint32
|
||||||
infoLog io.WriteCloser
|
infoLog io.WriteCloser
|
||||||
@@ -137,9 +137,9 @@ func SetUp(c LogConf) error {
|
|||||||
}
|
}
|
||||||
switch c.Encoding {
|
switch c.Encoding {
|
||||||
case plainEncoding:
|
case plainEncoding:
|
||||||
setEncoding(plainEncodingType)
|
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||||
default:
|
default:
|
||||||
setEncoding(jsonEncodingType)
|
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch c.Mode {
|
switch c.Mode {
|
||||||
@@ -424,7 +424,7 @@ func infoTextSync(msg string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func outputAny(writer io.Writer, level string, val interface{}) {
|
func outputAny(writer io.Writer, level string, val interface{}) {
|
||||||
switch encoding {
|
switch atomic.LoadUint32(&encoding) {
|
||||||
case plainEncodingType:
|
case plainEncodingType:
|
||||||
writePlainAny(writer, level, val)
|
writePlainAny(writer, level, val)
|
||||||
default:
|
default:
|
||||||
@@ -438,7 +438,7 @@ func outputAny(writer io.Writer, level string, val interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func outputText(writer io.Writer, level, msg string) {
|
func outputText(writer io.Writer, level, msg string) {
|
||||||
switch encoding {
|
switch atomic.LoadUint32(&encoding) {
|
||||||
case plainEncodingType:
|
case plainEncodingType:
|
||||||
writePlainText(writer, level, msg)
|
writePlainText(writer, level, msg)
|
||||||
default:
|
default:
|
||||||
@@ -666,7 +666,3 @@ func (lw logWriter) Write(data []byte) (int, error) {
|
|||||||
lw.logger.Print(string(data))
|
lw.logger.Print(string(data))
|
||||||
return len(data), nil
|
return len(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setEncoding(encodingType uint32) {
|
|
||||||
atomic.StoreUint32(&encoding, encodingType)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -145,10 +145,10 @@ func TestStructedLogInfoConsoleAny(t *testing.T) {
|
|||||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||||
infoLog = writer
|
infoLog = writer
|
||||||
}, func(v ...interface{}) {
|
}, func(v ...interface{}) {
|
||||||
old := encoding
|
old := atomic.LoadUint32(&encoding)
|
||||||
encoding = plainEncodingType
|
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||||
defer func() {
|
defer func() {
|
||||||
encoding = old
|
atomic.StoreUint32(&encoding, old)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
Infov(v)
|
Infov(v)
|
||||||
@@ -159,10 +159,10 @@ func TestStructedLogInfoConsoleAnyString(t *testing.T) {
|
|||||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||||
infoLog = writer
|
infoLog = writer
|
||||||
}, func(v ...interface{}) {
|
}, func(v ...interface{}) {
|
||||||
old := encoding
|
old := atomic.LoadUint32(&encoding)
|
||||||
encoding = plainEncodingType
|
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||||
defer func() {
|
defer func() {
|
||||||
encoding = old
|
atomic.StoreUint32(&encoding, old)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
Infov(fmt.Sprint(v...))
|
Infov(fmt.Sprint(v...))
|
||||||
@@ -173,10 +173,10 @@ func TestStructedLogInfoConsoleAnyError(t *testing.T) {
|
|||||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||||
infoLog = writer
|
infoLog = writer
|
||||||
}, func(v ...interface{}) {
|
}, func(v ...interface{}) {
|
||||||
old := encoding
|
old := atomic.LoadUint32(&encoding)
|
||||||
encoding = plainEncodingType
|
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||||
defer func() {
|
defer func() {
|
||||||
encoding = old
|
atomic.StoreUint32(&encoding, old)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
Infov(errors.New(fmt.Sprint(v...)))
|
Infov(errors.New(fmt.Sprint(v...)))
|
||||||
@@ -187,10 +187,10 @@ func TestStructedLogInfoConsoleAnyStringer(t *testing.T) {
|
|||||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||||
infoLog = writer
|
infoLog = writer
|
||||||
}, func(v ...interface{}) {
|
}, func(v ...interface{}) {
|
||||||
old := encoding
|
old := atomic.LoadUint32(&encoding)
|
||||||
encoding = plainEncodingType
|
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||||
defer func() {
|
defer func() {
|
||||||
encoding = old
|
atomic.StoreUint32(&encoding, old)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
Infov(ValStringer{
|
Infov(ValStringer{
|
||||||
@@ -203,10 +203,10 @@ func TestStructedLogInfoConsoleText(t *testing.T) {
|
|||||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||||
infoLog = writer
|
infoLog = writer
|
||||||
}, func(v ...interface{}) {
|
}, func(v ...interface{}) {
|
||||||
old := encoding
|
old := atomic.LoadUint32(&encoding)
|
||||||
encoding = plainEncodingType
|
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||||
defer func() {
|
defer func() {
|
||||||
encoding = old
|
atomic.StoreUint32(&encoding, old)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
Info(fmt.Sprint(v...))
|
Info(fmt.Sprint(v...))
|
||||||
|
|||||||
@@ -29,9 +29,9 @@ func TestRedirector(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func captureOutput(f func()) string {
|
func captureOutput(f func()) string {
|
||||||
atomic.StoreUint32(&initialized, 1)
|
|
||||||
writer := new(mockWriter)
|
writer := new(mockWriter)
|
||||||
infoLog = writer
|
infoLog = writer
|
||||||
|
atomic.StoreUint32(&initialized, 1)
|
||||||
|
|
||||||
prevLevel := atomic.LoadUint32(&logLevel)
|
prevLevel := atomic.LoadUint32(&logLevel)
|
||||||
SetLevel(InfoLevel)
|
SetLevel(InfoLevel)
|
||||||
@@ -44,5 +44,9 @@ func captureOutput(f func()) string {
|
|||||||
func getContent(jsonStr string) string {
|
func getContent(jsonStr string) string {
|
||||||
var entry logEntry
|
var entry logEntry
|
||||||
json.Unmarshal([]byte(jsonStr), &entry)
|
json.Unmarshal([]byte(jsonStr), &entry)
|
||||||
return entry.Content.(string)
|
val, ok := entry.Content.(string)
|
||||||
|
if ok {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/timex"
|
"github.com/zeromicro/go-zero/core/timex"
|
||||||
@@ -80,7 +81,7 @@ func (l *traceLogger) write(writer io.Writer, level string, val interface{}) {
|
|||||||
traceID := traceIdFromContext(l.ctx)
|
traceID := traceIdFromContext(l.ctx)
|
||||||
spanID := spanIdFromContext(l.ctx)
|
spanID := spanIdFromContext(l.ctx)
|
||||||
|
|
||||||
switch encoding {
|
switch atomic.LoadUint32(&encoding) {
|
||||||
case plainEncodingType:
|
case plainEncodingType:
|
||||||
writePlainAny(writer, level, val, l.Duration, traceID, spanID)
|
writePlainAny(writer, level, val, l.Duration, traceID, spanID)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -83,10 +83,10 @@ func TestTraceInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTraceInfoConsole(t *testing.T) {
|
func TestTraceInfoConsole(t *testing.T) {
|
||||||
old := encoding
|
old := atomic.LoadUint32(&encoding)
|
||||||
setEncoding(jsonEncodingType)
|
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||||
defer func() {
|
defer func() {
|
||||||
setEncoding(old)
|
atomic.StoreUint32(&encoding, old)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var buf mockWriter
|
var buf mockWriter
|
||||||
|
|||||||
@@ -356,8 +356,8 @@ github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da h1:NimzV1aGyq29m5u
|
|||||||
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA=
|
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA=
|
||||||
github.com/zeromicro/antlr v0.0.1 h1:CQpIn/dc0pUjgGQ81y98s/NGOm2Hfru2NNio2I9mQgk=
|
github.com/zeromicro/antlr v0.0.1 h1:CQpIn/dc0pUjgGQ81y98s/NGOm2Hfru2NNio2I9mQgk=
|
||||||
github.com/zeromicro/antlr v0.0.1/go.mod h1:nfpjEwFR6Q4xGDJMcZnCL9tEfQRgszMwu3rDz2Z+p5M=
|
github.com/zeromicro/antlr v0.0.1/go.mod h1:nfpjEwFR6Q4xGDJMcZnCL9tEfQRgszMwu3rDz2Z+p5M=
|
||||||
github.com/zeromicro/ddl-parser v1.0.2 h1:bHmxTh8+unNAQoIiCwBdzS49ho6Bb9E0/KrH1HqUwqo=
|
github.com/zeromicro/ddl-parser v1.0.3 h1:hFecpbt0oPQMhHAbqG1tz78MUepHUnOkFJp1dvRBFyc=
|
||||||
github.com/zeromicro/ddl-parser v1.0.2/go.mod h1:ISU/8NuPyEpl9pa17Py9TBPetMjtsiHrb9f5XGiYbo8=
|
github.com/zeromicro/ddl-parser v1.0.3/go.mod h1:ISU/8NuPyEpl9pa17Py9TBPetMjtsiHrb9f5XGiYbo8=
|
||||||
github.com/zeromicro/go-zero v1.3.0 h1:Eyn36yBtR043sm4YKmxR6eS3UA/GtZDktQ+UqIJ3Lm0=
|
github.com/zeromicro/go-zero v1.3.0 h1:Eyn36yBtR043sm4YKmxR6eS3UA/GtZDktQ+UqIJ3Lm0=
|
||||||
github.com/zeromicro/go-zero v1.3.0/go.mod h1:Hy4o1VFAt32lXaQMbaBhoFeZjA/rJqJ4PTGNdGsURcc=
|
github.com/zeromicro/go-zero v1.3.0/go.mod h1:Hy4o1VFAt32lXaQMbaBhoFeZjA/rJqJ4PTGNdGsURcc=
|
||||||
go.etcd.io/etcd/api/v3 v3.5.1/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
|
go.etcd.io/etcd/api/v3 v3.5.1/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
|
||||||
|
|||||||
Reference in New Issue
Block a user