logger.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 grpclog.LoggerV2
  23. var (
  24. logger settableLogger
  25. )
  26. type settableLogger struct {
  27. l grpclog.LoggerV2
  28. mu sync.RWMutex
  29. }
  30. func init() {
  31. // disable client side logs by default
  32. logger.mu.Lock()
  33. logger.l = grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard)
  34. // logger has to override the grpclog at initialization so that
  35. // any changes to the grpclog go through logger with locking
  36. // instead of through SetLogger
  37. //
  38. // now updates only happen through settableLogger.set
  39. grpclog.SetLoggerV2(&logger)
  40. logger.mu.Unlock()
  41. }
  42. // SetLogger sets client-side Logger. By default, logs are disabled.
  43. func SetLogger(l Logger) {
  44. logger.set(l)
  45. }
  46. // GetLogger returns the current logger.
  47. func GetLogger() Logger {
  48. return logger.get()
  49. }
  50. func (s *settableLogger) set(l Logger) {
  51. s.mu.Lock()
  52. logger.l = l
  53. grpclog.SetLoggerV2(&logger)
  54. s.mu.Unlock()
  55. }
  56. func (s *settableLogger) get() Logger {
  57. s.mu.RLock()
  58. l := logger.l
  59. s.mu.RUnlock()
  60. return l
  61. }
  62. // implement the grpclog.LoggerV2 interface
  63. func (s *settableLogger) Info(args ...interface{}) { s.get().Info(args...) }
  64. func (s *settableLogger) Infof(format string, args ...interface{}) { s.get().Infof(format, args...) }
  65. func (s *settableLogger) Infoln(args ...interface{}) { s.get().Infoln(args...) }
  66. func (s *settableLogger) Warning(args ...interface{}) { s.get().Warning(args...) }
  67. func (s *settableLogger) Warningf(format string, args ...interface{}) {
  68. s.get().Warningf(format, args...)
  69. }
  70. func (s *settableLogger) Warningln(args ...interface{}) { s.get().Warningln(args...) }
  71. func (s *settableLogger) Error(args ...interface{}) { s.get().Error(args...) }
  72. func (s *settableLogger) Errorf(format string, args ...interface{}) {
  73. s.get().Errorf(format, args...)
  74. }
  75. func (s *settableLogger) Errorln(args ...interface{}) { s.get().Errorln(args...) }
  76. func (s *settableLogger) Fatal(args ...interface{}) { s.get().Fatal(args...) }
  77. func (s *settableLogger) Fatalf(format string, args ...interface{}) { s.get().Fatalf(format, args...) }
  78. func (s *settableLogger) Fatalln(args ...interface{}) { s.get().Fatalln(args...) }
  79. func (s *settableLogger) Print(args ...interface{}) { s.get().Info(args...) }
  80. func (s *settableLogger) Printf(format string, args ...interface{}) { s.get().Infof(format, args...) }
  81. func (s *settableLogger) Println(args ...interface{}) { s.get().Infoln(args...) }
  82. func (s *settableLogger) V(l int) bool { return s.get().V(l) }