Takahiro Suzuki | 241c10e | 2020-12-17 20:17:57 +0900 | [diff] [blame] | 1 | // Copyright 2018 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 logutil |
| 16 | |
| 17 | import "google.golang.org/grpc/grpclog" |
| 18 | |
| 19 | // Logger defines logging interface. |
| 20 | // TODO: deprecate in v3.5. |
| 21 | type Logger interface { |
| 22 | grpclog.LoggerV2 |
| 23 | |
| 24 | // Lvl returns logger if logger's verbosity level >= "lvl". |
| 25 | // Otherwise, logger that discards everything. |
| 26 | Lvl(lvl int) grpclog.LoggerV2 |
| 27 | } |
| 28 | |
| 29 | // assert that "defaultLogger" satisfy "Logger" interface |
| 30 | var _ Logger = &defaultLogger{} |
| 31 | |
| 32 | // NewLogger wraps "grpclog.LoggerV2" that implements "Logger" interface. |
| 33 | // |
| 34 | // For example: |
| 35 | // |
| 36 | // var defaultLogger Logger |
| 37 | // g := grpclog.NewLoggerV2WithVerbosity(os.Stderr, os.Stderr, os.Stderr, 4) |
| 38 | // defaultLogger = NewLogger(g) |
| 39 | // |
| 40 | func NewLogger(g grpclog.LoggerV2) Logger { return &defaultLogger{g: g} } |
| 41 | |
| 42 | type defaultLogger struct { |
| 43 | g grpclog.LoggerV2 |
| 44 | } |
| 45 | |
| 46 | func (l *defaultLogger) Info(args ...interface{}) { l.g.Info(args...) } |
| 47 | func (l *defaultLogger) Infoln(args ...interface{}) { l.g.Info(args...) } |
| 48 | func (l *defaultLogger) Infof(format string, args ...interface{}) { l.g.Infof(format, args...) } |
| 49 | func (l *defaultLogger) Warning(args ...interface{}) { l.g.Warning(args...) } |
| 50 | func (l *defaultLogger) Warningln(args ...interface{}) { l.g.Warning(args...) } |
| 51 | func (l *defaultLogger) Warningf(format string, args ...interface{}) { l.g.Warningf(format, args...) } |
| 52 | func (l *defaultLogger) Error(args ...interface{}) { l.g.Error(args...) } |
| 53 | func (l *defaultLogger) Errorln(args ...interface{}) { l.g.Error(args...) } |
| 54 | func (l *defaultLogger) Errorf(format string, args ...interface{}) { l.g.Errorf(format, args...) } |
| 55 | func (l *defaultLogger) Fatal(args ...interface{}) { l.g.Fatal(args...) } |
| 56 | func (l *defaultLogger) Fatalln(args ...interface{}) { l.g.Fatal(args...) } |
| 57 | func (l *defaultLogger) Fatalf(format string, args ...interface{}) { l.g.Fatalf(format, args...) } |
| 58 | func (l *defaultLogger) V(lvl int) bool { return l.g.V(lvl) } |
| 59 | func (l *defaultLogger) Lvl(lvl int) grpclog.LoggerV2 { |
| 60 | if l.g.V(lvl) { |
| 61 | return l |
| 62 | } |
| 63 | return &discardLogger{} |
| 64 | } |