internal_logging.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package global // import "go.opentelemetry.io/otel/internal/global"
  15. import (
  16. "log"
  17. "os"
  18. "sync"
  19. "github.com/go-logr/logr"
  20. "github.com/go-logr/stdr"
  21. )
  22. // globalLogger is the logging interface used within the otel api and sdk provide deatails of the internals.
  23. //
  24. // The default logger uses stdr which is backed by the standard `log.Logger`
  25. // interface. This logger will only show messages at the Error Level.
  26. var globalLogger logr.Logger = stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile))
  27. var globalLoggerLock = &sync.RWMutex{}
  28. // SetLogger overrides the globalLogger with l.
  29. //
  30. // To see Info messages use a logger with `l.V(1).Enabled() == true`
  31. // To see Debug messages use a logger with `l.V(5).Enabled() == true`.
  32. func SetLogger(l logr.Logger) {
  33. globalLoggerLock.Lock()
  34. defer globalLoggerLock.Unlock()
  35. globalLogger = l
  36. }
  37. // Info prints messages about the general state of the API or SDK.
  38. // This should usually be less then 5 messages a minute.
  39. func Info(msg string, keysAndValues ...interface{}) {
  40. globalLoggerLock.RLock()
  41. defer globalLoggerLock.RUnlock()
  42. globalLogger.V(1).Info(msg, keysAndValues...)
  43. }
  44. // Error prints messages about exceptional states of the API or SDK.
  45. func Error(err error, msg string, keysAndValues ...interface{}) {
  46. globalLoggerLock.RLock()
  47. defer globalLoggerLock.RUnlock()
  48. globalLogger.Error(err, msg, keysAndValues...)
  49. }
  50. // Debug prints messages about all internal changes in the API or SDK.
  51. func Debug(msg string, keysAndValues ...interface{}) {
  52. globalLoggerLock.RLock()
  53. defer globalLoggerLock.RUnlock()
  54. globalLogger.V(5).Info(msg, keysAndValues...)
  55. }