sirupsen / logrus

Structured, pluggable logging for Go.
MIT License
24.8k stars 2.27k forks source link

Data race in TextFormatter.isColored() #1302

Closed CaledoniaProject closed 2 years ago

CaledoniaProject commented 2 years ago

I have multiple goroutines that will create a logger for each goroutine, and it results in a data race on isColored(). What was wrong?

Data race:

==================
WARNING: DATA RACE
Read at 0x00000116d101 by goroutine 106:
  github.com/sirupsen/logrus.(*TextFormatter).isColored()
      /root/go/pkg/mod/github.com/sirupsen/logrus@v1.8.1/text_formatter.go:129 +0xf0
  github.com/sirupsen/logrus.(*TextFormatter).Format()
      /root/go/pkg/mod/github.com/sirupsen/logrus@v1.8.1/text_formatter.go:202 +0x1611
  github.com/rifflock/lfshook.(*LfsHook).ioWrite()
      /root/go/pkg/mod/github.com/rifflock/lfshook@v0.0.0-20180920164130-b9218ef580f5/lfshook.go:142 +0x136
  github.com/rifflock/lfshook.(*LfsHook).Fire()
      /root/go/pkg/mod/github.com/rifflock/lfshook@v0.0.0-20180920164130-b9218ef580f5/lfshook.go:116 +0x11c
  github.com/sirupsen/logrus.LevelHooks.Fire()
      /root/go/pkg/mod/github.com/sirupsen/logrus@v1.8.1/hooks.go:28 +0xbb
  github.com/sirupsen/logrus.(*Entry).fireHooks()
      /root/go/pkg/mod/github.com/sirupsen/logrus@v1.8.1/entry.go:272 +0x304
  github.com/sirupsen/logrus.(*Entry).log()
      /root/go/pkg/mod/github.com/sirupsen/logrus@v1.8.1/entry.go:241 +0x315
  github.com/sirupsen/logrus.(*Entry).Log()
      /root/go/pkg/mod/github.com/sirupsen/logrus@v1.8.1/entry.go:293 +0x8b
  github.com/sirupsen/logrus.(*Entry).Logf()
      /root/go/pkg/mod/github.com/sirupsen/logrus@v1.8.1/entry.go:338 +0xc4
  github.com/sirupsen/logrus.(*Logger).Logf()
      /root/go/pkg/mod/github.com/sirupsen/logrus@v1.8.1/logger.go:151 +0xa4
  github.com/sirupsen/logrus.(*Logger).Infof()
      /root/go/pkg/mod/github.com/sirupsen/logrus@v1.8.1/logger.go:165 +0x204

Previous write at 0x00000116d101 by goroutine 11:
  github.com/rifflock/lfshook.(*LfsHook).SetFormatter()
      /root/go/pkg/mod/github.com/rifflock/lfshook@v0.0.0-20180920164130-b9218ef580f5/lfshook.go:87 +0x104
  github.com/rifflock/lfshook.NewHook()
      /root/go/pkg/mod/github.com/rifflock/lfshook@v0.0.0-20180920164130-b9218ef580f5/lfshook.go:48 +0x130
==================
CaledoniaProject commented 2 years ago

Resolved. Need to create a new formatter each time. Same as logrus.New() method.