logger.go 2.6 KB

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