Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend, logger: lock before reassigning defaultBackend #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func SetBackend(backends ...Backend) LeveledBackend {
backend = MultiLogger(backends...)
}

lock.Lock()
defaultBackend = AddModuleLevel(backend)
lock.Unlock()
return defaultBackend
}

Expand Down
19 changes: 11 additions & 8 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ const (
DEBUG
)

var levelNames = []string{
"CRITICAL",
"ERROR",
"WARNING",
"NOTICE",
"INFO",
"DEBUG",
}
var (
lock sync.Mutex
levelNames = []string{
"CRITICAL",
"ERROR",
"WARNING",
"NOTICE",
"INFO",
"DEBUG",
}
)

// String returns the string representation of a logging level.
func (p Level) String() string {
Expand Down
5 changes: 4 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ func Reset() {

// IsEnabledFor returns true if the logger is enabled for the given level.
func (l *Logger) IsEnabledFor(level Level) bool {
return defaultBackend.IsEnabledFor(level, l.Module)
lock.Lock()
enabled := defaultBackend.IsEnabledFor(level, l.Module)
defer lock.Unlock()
return enabled
}

func (l *Logger) log(lvl Level, format *string, args ...interface{}) {
Expand Down