From d608f7af2b65ec857ac2dc4cc519ecbadfa4e4ee Mon Sep 17 00:00:00 2001 From: "richard.le-terrier" Date: Mon, 4 Dec 2023 13:52:25 +0000 Subject: [PATCH 1/2] fix: data race on registered fields slice --- go.mod | 11 +++++------ go.sum | 29 +++++++++++++---------------- log.go | 36 ++++++++++++++++++++++++++++-------- types.go | 5 +++++ 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index 9330783..14140ea 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index f7d021d..0cff41a 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,3 @@ -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= @@ -6,24 +5,22 @@ 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= diff --git a/log.go b/log.go index 6440e37..a05b54b 100644 --- a/log.go +++ b/log.go @@ -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 @@ -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 } @@ -91,11 +91,21 @@ 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) } @@ -103,8 +113,13 @@ func (l *Logger) RegisterDefaultFields() { 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{}) { @@ -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 } diff --git a/types.go b/types.go index 7a833fd..b5f8168 100644 --- a/types.go +++ b/types.go @@ -5,6 +5,11 @@ type ( Level int ) +type ExcludeRule struct { + Field Field + Value any +} + const ( LevelDebug Level = iota LevelInfo From 6625153531ced1c6e97ef39f028890c89eb37613 Mon Sep 17 00:00:00 2001 From: "richard.le-terrier" Date: Thu, 7 Dec 2023 09:15:22 +0000 Subject: [PATCH 2/2] chore: bump to go 1.21 --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 3e9a76d..3c1f6cf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 ./...