logger.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. s.mu.Unlock()
  54. }
  55. func (s *settableLogger) get() Logger {
  56. s.mu.RLock()
  57. l := logger.l
  58. s.mu.RUnlock()
  59. return l
  60. }
  61. // implement the grpclog.LoggerV2 interface
  62. func (s *settableLogger) Info(args ...interface{}) { s.get().Info(args...) }
  63. func (s *settableLogger) Infof(format string, args ...interface{}) { s.get().Infof(format, args...) }
  64. func (s *settableLogger) Infoln(args ...interface{}) { s.get().Infoln(args...) }
  65. func (s *settableLogger) Warning(args ...interface{}) { s.get().Warning(args...) }
  66. func (s *settableLogger) Warningf(format string, args ...interface{}) {
  67. s.get().Warningf(format, args...)
  68. }
  69. func (s *settableLogger) Warningln(args ...interface{}) { s.get().Warningln(args...) }
  70. func (s *settableLogger) Error(args ...interface{}) { s.get().Error(args...) }
  71. func (s *settableLogger) Errorf(format string, args ...interface{}) {
  72. s.get().Errorf(format, args...)
  73. }
  74. func (s *settableLogger) Errorln(args ...interface{}) { s.get().Errorln(args...) }
  75. func (s *settableLogger) Fatal(args ...interface{}) { s.get().Fatal(args...) }
  76. func (s *settableLogger) Fatalf(format string, args ...interface{}) { s.get().Fatalf(format, args...) }
  77. func (s *settableLogger) Fatalln(args ...interface{}) { s.get().Fatalln(args...) }
  78. func (s *settableLogger) Print(args ...interface{}) { s.get().Info(args...) }
  79. func (s *settableLogger) Printf(format string, args ...interface{}) { s.get().Infof(format, args...) }
  80. func (s *settableLogger) Println(args ...interface{}) { s.get().Infoln(args...) }
  81. func (s *settableLogger) V(l int) bool { return s.get().V(l) }