瀏覽代碼

Jeff saves the day

binwiederhier 3 年之前
父節點
當前提交
057c4a3239
共有 2 個文件被更改,包括 14 次插入14 次删除
  1. 4 4
      log/event.go
  2. 10 10
      log/log.go

+ 4 - 4
log/event.go

@@ -197,9 +197,9 @@ func (e *Event) shouldLog(l Level) bool {
 }
 
 func (e *Event) globalLevelWithOverride() Level {
-	mu.Lock()
+	mu.RLock()
 	l, ov := level, overrides
-	mu.Unlock()
+	mu.RUnlock()
 	if e.fields == nil {
 		return l
 	}
@@ -217,9 +217,9 @@ func (e *Event) globalLevelWithOverride() Level {
 }
 
 func (e *Event) maybeApplyContexters() bool {
-	mu.Lock()
+	mu.RLock()
 	hasOverrides := len(overrides) > 0
-	mu.Unlock()
+	mu.RUnlock()
 	if hasOverrides {
 		e.applyContexters()
 	}

+ 10 - 10
log/log.go

@@ -20,7 +20,7 @@ var (
 	format              = DefaultFormat
 	overrides           = make(map[string]*levelOverride)
 	output    io.Writer = DefaultOutput
-	mu                  = &sync.Mutex{}
+	mu                  = &sync.RWMutex{}
 )
 
 // Fatal prints the given message, and exits the program
@@ -85,8 +85,8 @@ func Timing(f func()) *Event {
 
 // CurrentLevel returns the current log level
 func CurrentLevel() Level {
-	mu.Lock()
-	defer mu.Unlock()
+	mu.RLock()
+	defer mu.RUnlock()
 	return level
 }
 
@@ -111,10 +111,10 @@ func ResetLevelOverrides() {
 	overrides = make(map[string]*levelOverride)
 }
 
-// CurrentFormat returns the current log formt
+// CurrentFormat returns the current log format
 func CurrentFormat() Format {
-	mu.Lock()
-	defer mu.Unlock()
+	mu.RLock()
+	defer mu.RUnlock()
 	return format
 }
 
@@ -138,8 +138,8 @@ func SetOutput(w io.Writer) {
 
 // File returns the log file, if any, or an empty string otherwise
 func File() string {
-	mu.Lock()
-	defer mu.Unlock()
+	mu.RLock()
+	defer mu.RUnlock()
 	if f, ok := output.(*os.File); ok {
 		return f.Name()
 	}
@@ -148,8 +148,8 @@ func File() string {
 
 // IsFile returns true if the output is a non-default file
 func IsFile() bool {
-	mu.Lock()
-	defer mu.Unlock()
+	mu.RLock()
+	defer mu.RUnlock()
 	if _, ok := output.(*os.File); ok && output != DefaultOutput {
 		return true
 	}