internal_logging.go 2.1 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/atomic"
  19. "unsafe"
  20. "github.com/go-logr/logr"
  21. "github.com/go-logr/stdr"
  22. )
  23. // globalLogger is the logging interface used within the otel api and sdk provide deatails of the internals.
  24. //
  25. // The default logger uses stdr which is backed by the standard `log.Logger`
  26. // interface. This logger will only show messages at the Error Level.
  27. var globalLogger unsafe.Pointer
  28. func init() {
  29. SetLogger(stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)))
  30. }
  31. // SetLogger overrides the globalLogger with l.
  32. //
  33. // To see Info messages use a logger with `l.V(1).Enabled() == true`
  34. // To see Debug messages use a logger with `l.V(5).Enabled() == true`.
  35. func SetLogger(l logr.Logger) {
  36. atomic.StorePointer(&globalLogger, unsafe.Pointer(&l))
  37. }
  38. func getLogger() logr.Logger {
  39. return *(*logr.Logger)(atomic.LoadPointer(&globalLogger))
  40. }
  41. // Info prints messages about the general state of the API or SDK.
  42. // This should usually be less then 5 messages a minute.
  43. func Info(msg string, keysAndValues ...interface{}) {
  44. getLogger().V(1).Info(msg, keysAndValues...)
  45. }
  46. // Error prints messages about exceptional states of the API or SDK.
  47. func Error(err error, msg string, keysAndValues ...interface{}) {
  48. getLogger().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. getLogger().V(5).Info(msg, keysAndValues...)
  53. }