Skip to content

Commit

Permalink
Global logger uses an atomic value instead of a mutex.
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Dec 16, 2022
1 parent 7a60bc7 commit f06e4f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add `Producer` interface and `Reader.RegisterProducer(Producer)` to `go.opentelemetry.io/otel/sdk/metric` to enable external metric Producers. (#3524)

### Changed

- Global logger uses an atomic value instead of a mutex. (#3545)

## [1.11.2/0.34.0] 2022-12-05

### Added
Expand Down
29 changes: 14 additions & 15 deletions internal/global/internal_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package global // import "go.opentelemetry.io/otel/internal/global"
import (
"log"
"os"
"sync"
"sync/atomic"

"github.com/go-logr/logr"
"github.com/go-logr/stdr"
Expand All @@ -27,37 +27,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 = func() *atomic.Value {
dl := &atomic.Value{}
dl.Store(stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)))
return dl
}()

// 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
globalLogger.Store(l)
}

func getLogger() logr.Logger {
return globalLogger.Load().(logr.Logger)
}

// 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...)
}

0 comments on commit f06e4f8

Please sign in to comment.