Skip to content

Commit

Permalink
feat: Return inner Logger from GetLogger for log.WithContext reuse
Browse files Browse the repository at this point in the history
fix: Ensure thread safety by eliminating lock copy in GetLogger
  • Loading branch information
guangxue.wu committed Oct 31, 2024
1 parent bb85d7e commit 84e9a4a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions log/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var global = &loggerAppliance{}
// loggerAppliance is the proxy of `Logger` to
// make logger change will affect all sub-logger.
type loggerAppliance struct {
lock sync.Mutex
lock sync.RWMutex
Logger
}

Expand All @@ -35,7 +35,9 @@ func SetLogger(logger Logger) {

// GetLogger returns global logger appliance as logger in current process.
func GetLogger() Logger {
return global
global.lock.RLock()
defer global.lock.RUnlock()
return global.Logger
}

// Log Print log by level and keyvals.
Expand Down

0 comments on commit 84e9a4a

Please sign in to comment.