logger.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "go.etcd.io/etcd/pkg/logutil"
  19. "google.golang.org/grpc/grpclog"
  20. )
  21. var (
  22. lgMu sync.RWMutex
  23. lg logutil.Logger
  24. )
  25. type settableLogger struct {
  26. l grpclog.LoggerV2
  27. mu sync.RWMutex
  28. }
  29. func init() {
  30. // disable client side logs by default
  31. lg = &settableLogger{}
  32. SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
  33. }
  34. // SetLogger sets client-side Logger.
  35. func SetLogger(l grpclog.LoggerV2) {
  36. lgMu.Lock()
  37. lg = logutil.NewLogger(l)
  38. // override grpclog so that any changes happen with locking
  39. grpclog.SetLoggerV2(lg)
  40. lgMu.Unlock()
  41. }
  42. // GetLogger returns the current logutil.Logger.
  43. func GetLogger() logutil.Logger {
  44. lgMu.RLock()
  45. l := lg
  46. lgMu.RUnlock()
  47. return l
  48. }
  49. // NewLogger returns a new Logger with logutil.Logger.
  50. func NewLogger(gl grpclog.LoggerV2) logutil.Logger {
  51. return &settableLogger{l: gl}
  52. }
  53. func (s *settableLogger) get() grpclog.LoggerV2 {
  54. s.mu.RLock()
  55. l := s.l
  56. s.mu.RUnlock()
  57. return l
  58. }
  59. // implement the grpclog.LoggerV2 interface
  60. func (s *settableLogger) Info(args ...interface{}) { s.get().Info(args...) }
  61. func (s *settableLogger) Infof(format string, args ...interface{}) { s.get().Infof(format, args...) }
  62. func (s *settableLogger) Infoln(args ...interface{}) { s.get().Infoln(args...) }
  63. func (s *settableLogger) Warning(args ...interface{}) { s.get().Warning(args...) }
  64. func (s *settableLogger) Warningf(format string, args ...interface{}) {
  65. s.get().Warningf(format, args...)
  66. }
  67. func (s *settableLogger) Warningln(args ...interface{}) { s.get().Warningln(args...) }
  68. func (s *settableLogger) Error(args ...interface{}) { s.get().Error(args...) }
  69. func (s *settableLogger) Errorf(format string, args ...interface{}) {
  70. s.get().Errorf(format, args...)
  71. }
  72. func (s *settableLogger) Errorln(args ...interface{}) { s.get().Errorln(args...) }
  73. func (s *settableLogger) Fatal(args ...interface{}) { s.get().Fatal(args...) }
  74. func (s *settableLogger) Fatalf(format string, args ...interface{}) { s.get().Fatalf(format, args...) }
  75. func (s *settableLogger) Fatalln(args ...interface{}) { s.get().Fatalln(args...) }
  76. func (s *settableLogger) Print(args ...interface{}) { s.get().Info(args...) }
  77. func (s *settableLogger) Printf(format string, args ...interface{}) { s.get().Infof(format, args...) }
  78. func (s *settableLogger) Println(args ...interface{}) { s.get().Infoln(args...) }
  79. func (s *settableLogger) V(l int) bool { return s.get().V(l) }
  80. func (s *settableLogger) Lvl(lvl int) grpclog.LoggerV2 {
  81. s.mu.RLock()
  82. l := s.l
  83. s.mu.RUnlock()
  84. if l.V(lvl) {
  85. return s
  86. }
  87. return logutil.NewDiscardLogger()
  88. }