Skip to content

Commit

Permalink
fix: data race on registered fields slice (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Dec 7, 2023
1 parent 01b6af8 commit ca4b7ae
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21

- name: Build
run: go build -v ./...
Expand Down
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
module github.com/rockbears/log

go 1.19
go 1.21

require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.0
go.uber.org/zap v1.23.0
github.com/sirupsen/logrus v1.9.3
go.uber.org/zap v1.26.0
)

require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sys v0.15.0 // indirect
)
29 changes: 13 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36 changes: 28 additions & 8 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func init() {

type Logger struct {
registeredFields []Field
registeredFieldsMutex sync.Mutex
excludeRules map[Field]any
excludeRulesMutex sync.Mutex
registeredFieldsMutex sync.RWMutex
excludeRules []ExcludeRule
excludeRulesMutex sync.RWMutex
factory WrapperFactoryFunc
callerFrameToSkip int

Expand All @@ -44,7 +44,7 @@ func New() *Logger {
}

func NewWithFactory(factory WrapperFactoryFunc) *Logger {
logger := &Logger{factory: factory, callerFrameToSkip: 2, excludeRules: make(map[Field]any)}
logger := &Logger{factory: factory, callerFrameToSkip: 2}
logger.RegisterDefaultFields()
return logger
}
Expand Down Expand Up @@ -91,20 +91,35 @@ loop:
}

func (l *Logger) GetRegisteredFields() []Field {
l.registeredFieldsMutex.RLock()
defer l.registeredFieldsMutex.RUnlock()
fields := make([]Field, len(l.registeredFields))
copy(fields, l.registeredFields)
return fields
}

func (l *Logger) GetExcludeRules() []ExcludeRule {
l.excludeRulesMutex.RLock()
defer l.excludeRulesMutex.RUnlock()
excludeRules := make([]ExcludeRule, len(l.excludeRules))
copy(excludeRules, l.excludeRules)
return excludeRules
}

func (l *Logger) RegisterDefaultFields() {
l.RegisterField(FieldSourceFile, FieldSourceLine, FieldCaller, FieldStackTrace)
}

func (l *Logger) Skip(field Field, value interface{}) {
l.excludeRulesMutex.Lock()
defer l.excludeRulesMutex.Unlock()

l.excludeRules[field] = value
for i := range l.excludeRules {
if l.excludeRules[i].Field == field {
l.excludeRules[i].Value = value
return
}
}
l.excludeRules = append(l.excludeRules, ExcludeRule{field, value})
}

func (l *Logger) Debug(ctx context.Context, format string, args ...interface{}) {
Expand Down Expand Up @@ -154,10 +169,15 @@ func (l *Logger) call(ctx context.Context, level Level, format string, args ...i
}
}

for _, k := range l.registeredFields {
mExcludeRules := make(map[Field]any)
for i := range l.GetExcludeRules() {
mExcludeRules[l.excludeRules[i].Field] = l.excludeRules[i].Value
}

for _, k := range l.GetRegisteredFields() {
v := ctx.Value(k)
if v != nil {
if exludeValue, has := l.excludeRules[k]; has {
if exludeValue, has := mExcludeRules[k]; has {
if v == exludeValue {
return
}
Expand Down
5 changes: 5 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ type (
Level int
)

type ExcludeRule struct {
Field Field
Value any
}

const (
LevelDebug Level = iota
LevelInfo
Expand Down

0 comments on commit ca4b7ae

Please sign in to comment.