logger.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2016 The etcd 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 clientv3
  15. import (
  16. "io/ioutil"
  17. "sync"
  18. "google.golang.org/grpc/grpclog"
  19. )
  20. // Logger is the logger used by client library.
  21. // It implements grpclog.LoggerV2 interface.
  22. type Logger interface {
  23. grpclog.LoggerV2
  24. // Lvl returns logger if logger's verbosity level >= "lvl".
  25. // Otherwise, logger that discards all logs.
  26. Lvl(lvl int) Logger
  27. // to satisfy capnslog
  28. Print(args ...interface{})
  29. Printf(format string, args ...interface{})
  30. Println(args ...interface{})
  31. }
  32. var (
  33. loggerMu sync.RWMutex
  34. logger Logger
  35. )
  36. type settableLogger struct {
  37. l grpclog.LoggerV2
  38. mu sync.RWMutex
  39. }
  40. func init() {
  41. // disable client side logs by default
  42. logger = &settableLogger{}
  43. SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
  44. }
  45. // SetLogger sets client-side Logger.
  46. func SetLogger(l grpclog.LoggerV2) {
  47. loggerMu.Lock()
  48. logger = NewLogger(l)
  49. // override grpclog so that any changes happen with locking
  50. grpclog.SetLoggerV2(logger)
  51. loggerMu.Unlock()
  52. }
  53. // GetLogger returns the current logger.
  54. func GetLogger() Logger {
  55. loggerMu.RLock()
  56. l := logger
  57. loggerMu.RUnlock()
  58. return l
  59. }
  60. // NewLogger returns a new Logger with grpclog.LoggerV2.
  61. func NewLogger(gl grpclog.LoggerV2) Logger {
  62. return &settableLogger{l: gl}
  63. }
  64. func (s *settableLogger) get() grpclog.LoggerV2 {
  65. s.mu.RLock()
  66. l := s.l
  67. s.mu.RUnlock()
  68. return l
  69. }
  70. // implement the grpclog.LoggerV2 interface
  71. func (s *settableLogger) Info(args ...interface{}) { s.get().Info(args...) }
  72. func (s *settableLogger) Infof(format string, args ...interface{}) { s.get().Infof(format, args...) }
  73. func (s *settableLogger) Infoln(args ...interface{}) { s.get().Infoln(args...) }
  74. func (s *settableLogger) Warning(args ...interface{}) { s.get().Warning(args...) }
  75. func (s *settableLogger) Warningf(format string, args ...interface{}) {
  76. s.get().Warningf(format, args...)
  77. }
  78. func (s *settableLogger) Warningln(args ...interface{}) { s.get().Warningln(args...) }
  79. func (s *settableLogger) Error(args ...interface{}) { s.get().Error(args...) }
  80. func (s *settableLogger) Errorf(format string, args ...interface{}) {
  81. s.get().Errorf(format, args...)
  82. }
  83. func (s *settableLogger) Errorln(args ...interface{}) { s.get().Errorln(args...) }
  84. func (s *settableLogger) Fatal(args ...interface{}) { s.get().Fatal(args...) }
  85. func (s *settableLogger) Fatalf(format string, args ...interface{}) { s.get().Fatalf(format, args...) }
  86. func (s *settableLogger) Fatalln(args ...interface{}) { s.get().Fatalln(args...) }
  87. func (s *settableLogger) Print(args ...interface{}) { s.get().Info(args...) }
  88. func (s *settableLogger) Printf(format string, args ...interface{}) { s.get().Infof(format, args...) }
  89. func (s *settableLogger) Println(args ...interface{}) { s.get().Infoln(args...) }
  90. func (s *settableLogger) V(l int) bool { return s.get().V(l) }
  91. func (s *settableLogger) Lvl(lvl int) Logger {
  92. s.mu.RLock()
  93. l := s.l
  94. s.mu.RUnlock()
  95. if l.V(lvl) {
  96. return s
  97. }
  98. return &noLogger{}
  99. }
  100. type noLogger struct{}
  101. func (*noLogger) Info(args ...interface{}) {}
  102. func (*noLogger) Infof(format string, args ...interface{}) {}
  103. func (*noLogger) Infoln(args ...interface{}) {}
  104. func (*noLogger) Warning(args ...interface{}) {}
  105. func (*noLogger) Warningf(format string, args ...interface{}) {}
  106. func (*noLogger) Warningln(args ...interface{}) {}
  107. func (*noLogger) Error(args ...interface{}) {}
  108. func (*noLogger) Errorf(format string, args ...interface{}) {}
  109. func (*noLogger) Errorln(args ...interface{}) {}
  110. func (*noLogger) Fatal(args ...interface{}) {}
  111. func (*noLogger) Fatalf(format string, args ...interface{}) {}
  112. func (*noLogger) Fatalln(args ...interface{}) {}
  113. func (*noLogger) Print(args ...interface{}) {}
  114. func (*noLogger) Printf(format string, args ...interface{}) {}
  115. func (*noLogger) Println(args ...interface{}) {}
  116. func (*noLogger) V(l int) bool { return false }
  117. func (ng *noLogger) Lvl(lvl int) Logger { return ng }