logger.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. package logger
  15. import "google.golang.org/grpc/grpclog"
  16. // Logger defines logging interface.
  17. type Logger interface {
  18. grpclog.LoggerV2
  19. // Lvl returns logger if logger's verbosity level >= "lvl".
  20. // Otherwise, logger that discards everything.
  21. Lvl(lvl int) grpclog.LoggerV2
  22. }
  23. // assert that "defaultLogger" satisfy "Logger" interface
  24. var _ Logger = &defaultLogger{}
  25. // New wraps "grpclog.LoggerV2" that implements "Logger" interface.
  26. //
  27. // For example:
  28. //
  29. // var defaultLogger Logger
  30. // g := grpclog.NewLoggerV2WithVerbosity(os.Stderr, os.Stderr, os.Stderr, 4)
  31. // defaultLogger = New(g)
  32. //
  33. func New(g grpclog.LoggerV2) Logger { return &defaultLogger{g: g} }
  34. type defaultLogger struct {
  35. g grpclog.LoggerV2
  36. }
  37. func (l *defaultLogger) Info(args ...interface{}) { l.g.Info(args...) }
  38. func (l *defaultLogger) Infoln(args ...interface{}) { l.g.Info(args...) }
  39. func (l *defaultLogger) Infof(format string, args ...interface{}) { l.g.Infof(format, args...) }
  40. func (l *defaultLogger) Warning(args ...interface{}) { l.g.Warning(args...) }
  41. func (l *defaultLogger) Warningln(args ...interface{}) { l.g.Warning(args...) }
  42. func (l *defaultLogger) Warningf(format string, args ...interface{}) { l.g.Warningf(format, args...) }
  43. func (l *defaultLogger) Error(args ...interface{}) { l.g.Error(args...) }
  44. func (l *defaultLogger) Errorln(args ...interface{}) { l.g.Error(args...) }
  45. func (l *defaultLogger) Errorf(format string, args ...interface{}) { l.g.Errorf(format, args...) }
  46. func (l *defaultLogger) Fatal(args ...interface{}) { l.g.Fatal(args...) }
  47. func (l *defaultLogger) Fatalln(args ...interface{}) { l.g.Fatal(args...) }
  48. func (l *defaultLogger) Fatalf(format string, args ...interface{}) { l.g.Fatalf(format, args...) }
  49. func (l *defaultLogger) V(lvl int) bool { return l.g.V(lvl) }
  50. func (l *defaultLogger) Lvl(lvl int) grpclog.LoggerV2 {
  51. if l.g.V(lvl) {
  52. return l
  53. }
  54. return &discardLogger{}
  55. }