Fix(log): default level may race

Rename global variables in uber’s style.
This commit is contained in:
xjasonlyu
2021-02-11 15:56:45 +08:00
parent 04d0e1e9a7
commit 454ea0a49b
2 changed files with 10 additions and 10 deletions

View File

@@ -8,8 +8,8 @@ import (
)
var (
logCh = make(chan interface{})
source = observable.NewObservable(logCh)
_logCh = make(chan interface{})
_source = observable.NewObservable(_logCh)
)
type Event struct {
@@ -24,16 +24,16 @@ func newEvent(level Level, format string, args ...interface{}) *Event {
Time: time.Now(),
Message: fmt.Sprintf(format, args...),
}
logCh <- event /* send all events to logCh */
_logCh <- event /* send all events to logCh */
return event
}
func Subscribe() observable.Subscription {
sub, _ := source.Subscribe()
sub, _ := _source.Subscribe()
return sub
}
func UnSubscribe(sub observable.Subscription) {
source.UnSubscribe(sub)
_source.UnSubscribe(sub)
}

View File

@@ -2,14 +2,14 @@ package log
import (
"os"
"sync/atomic"
"github.com/sirupsen/logrus"
"go.uber.org/atomic"
)
var (
// defaultLevel is package default logging level.
defaultLevel = InfoLevel
// _defaultLevel is package default logging level.
_defaultLevel = atomic.NewUint32(uint32(InfoLevel))
)
func init() {
@@ -18,7 +18,7 @@ func init() {
}
func SetLevel(level Level) {
atomic.StoreUint32((*uint32)(&defaultLevel), uint32(level))
_defaultLevel.Store(uint32(level))
}
func Debugf(format string, args ...interface{}) {
@@ -43,7 +43,7 @@ func Fatalf(format string, args ...interface{}) {
func logf(level Level, format string, args ...interface{}) {
event := newEvent(level, format, args...)
if event.Level > defaultLevel {
if uint32(event.Level) > _defaultLevel.Load() {
return
}