internal_logging.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "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 details 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 atomic.Pointer[logr.Logger]
  27. func init() {
  28. SetLogger(stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)))
  29. }
  30. // SetLogger overrides the globalLogger with l.
  31. //
  32. // To see Warn messages use a logger with `l.V(1).Enabled() == true`
  33. // To see Info messages use a logger with `l.V(4).Enabled() == true`
  34. // To see Debug messages use a logger with `l.V(8).Enabled() == true`.
  35. func SetLogger(l logr.Logger) {
  36. globalLogger.Store(&l)
  37. }
  38. func getLogger() logr.Logger {
  39. return *globalLogger.Load()
  40. }
  41. // Info prints messages about the general state of the API or SDK.
  42. // This should usually be less than 5 messages a minute.
  43. func Info(msg string, keysAndValues ...interface{}) {
  44. getLogger().V(4).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(8).Info(msg, keysAndValues...)
  53. }
  54. // Warn prints messages about warnings in the API or SDK.
  55. // Not an error but is likely more important than an informational event.
  56. func Warn(msg string, keysAndValues ...interface{}) {
  57. getLogger().V(1).Info(msg, keysAndValues...)
  58. }