mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-05 22:54:18 +02:00
Global logger uses an atomic value instead of a mutex. (#3545)
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
This commit is contained in:
parent
4014204d42
commit
0617172787
@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||
### Changed
|
||||
|
||||
- Global error handler uses an atomic value instead of a mutex. (#3543)
|
||||
- Global logger uses an atomic value instead of a mutex. (#3545)
|
||||
|
||||
## [1.11.2/0.34.0] 2022-12-05
|
||||
|
||||
|
@ -17,7 +17,8 @@ package global // import "go.opentelemetry.io/otel/internal/global"
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/go-logr/stdr"
|
||||
@ -27,37 +28,36 @@ import (
|
||||
//
|
||||
// The default logger uses stdr which is backed by the standard `log.Logger`
|
||||
// interface. This logger will only show messages at the Error Level.
|
||||
var globalLogger logr.Logger = stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile))
|
||||
var globalLoggerLock = &sync.RWMutex{}
|
||||
var globalLogger unsafe.Pointer
|
||||
|
||||
func init() {
|
||||
SetLogger(stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)))
|
||||
}
|
||||
|
||||
// SetLogger overrides the globalLogger with l.
|
||||
//
|
||||
// To see Info messages use a logger with `l.V(1).Enabled() == true`
|
||||
// To see Debug messages use a logger with `l.V(5).Enabled() == true`.
|
||||
func SetLogger(l logr.Logger) {
|
||||
globalLoggerLock.Lock()
|
||||
defer globalLoggerLock.Unlock()
|
||||
globalLogger = l
|
||||
atomic.StorePointer(&globalLogger, unsafe.Pointer(&l))
|
||||
}
|
||||
|
||||
func getLogger() logr.Logger {
|
||||
return *(*logr.Logger)(atomic.LoadPointer(&globalLogger))
|
||||
}
|
||||
|
||||
// Info prints messages about the general state of the API or SDK.
|
||||
// This should usually be less then 5 messages a minute.
|
||||
func Info(msg string, keysAndValues ...interface{}) {
|
||||
globalLoggerLock.RLock()
|
||||
defer globalLoggerLock.RUnlock()
|
||||
globalLogger.V(1).Info(msg, keysAndValues...)
|
||||
getLogger().V(1).Info(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Error prints messages about exceptional states of the API or SDK.
|
||||
func Error(err error, msg string, keysAndValues ...interface{}) {
|
||||
globalLoggerLock.RLock()
|
||||
defer globalLoggerLock.RUnlock()
|
||||
globalLogger.Error(err, msg, keysAndValues...)
|
||||
getLogger().Error(err, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Debug prints messages about all internal changes in the API or SDK.
|
||||
func Debug(msg string, keysAndValues ...interface{}) {
|
||||
globalLoggerLock.RLock()
|
||||
defer globalLoggerLock.RUnlock()
|
||||
globalLogger.V(5).Info(msg, keysAndValues...)
|
||||
getLogger().V(5).Info(msg, keysAndValues...)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user