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