Skip to content

Commit

Permalink
feat(otelzap): add missing globals
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Oct 16, 2021
1 parent 8be466b commit a511657
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions otelzap/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package otelzap

import (
"sync"

"go.uber.org/zap"
)

var (
_globalMu sync.RWMutex
_globalL = New(zap.NewNop())
_globalS = _globalL.Sugar()
)

// L returns the global Logger, which can be reconfigured with ReplaceGlobals.
// It's safe for concurrent use.
func L() *Logger {
_globalMu.RLock()
l := _globalL
_globalMu.RUnlock()
return l
}

// S returns the global SugaredLogger, which can be reconfigured with
// ReplaceGlobals. It's safe for concurrent use.
func S() *SugaredLogger {
_globalMu.RLock()
s := _globalS
_globalMu.RUnlock()
return s
}

// ReplaceGlobals replaces the global Logger and SugaredLogger, and returns a
// function to restore the original values. It's safe for concurrent use.
func ReplaceGlobals(logger *Logger) func() {
_globalMu.Lock()
prev := _globalL
_globalL = logger
_globalS = logger.Sugar()
_globalMu.Unlock()
return func() { ReplaceGlobals(prev) }
}

0 comments on commit a511657

Please sign in to comment.