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