package_logger.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 (
  16. "github.com/coreos/pkg/capnslog"
  17. "google.golang.org/grpc/grpclog"
  18. )
  19. // assert that "packageLogger" satisfy "Logger" interface
  20. var _ Logger = &packageLogger{}
  21. // NewPackageLogger wraps "*capnslog.PackageLogger" that implements "Logger" interface.
  22. //
  23. // For example:
  24. //
  25. // var defaultLogger Logger
  26. // defaultLogger = NewPackageLogger("go.etcd.io/etcd", "snapshot")
  27. //
  28. func NewPackageLogger(repo, pkg string) Logger {
  29. return &packageLogger{p: capnslog.NewPackageLogger(repo, pkg)}
  30. }
  31. type packageLogger struct {
  32. p *capnslog.PackageLogger
  33. }
  34. func (l *packageLogger) Info(args ...interface{}) { l.p.Info(args...) }
  35. func (l *packageLogger) Infoln(args ...interface{}) { l.p.Info(args...) }
  36. func (l *packageLogger) Infof(format string, args ...interface{}) { l.p.Infof(format, args...) }
  37. func (l *packageLogger) Warning(args ...interface{}) { l.p.Warning(args...) }
  38. func (l *packageLogger) Warningln(args ...interface{}) { l.p.Warning(args...) }
  39. func (l *packageLogger) Warningf(format string, args ...interface{}) { l.p.Warningf(format, args...) }
  40. func (l *packageLogger) Error(args ...interface{}) { l.p.Error(args...) }
  41. func (l *packageLogger) Errorln(args ...interface{}) { l.p.Error(args...) }
  42. func (l *packageLogger) Errorf(format string, args ...interface{}) { l.p.Errorf(format, args...) }
  43. func (l *packageLogger) Fatal(args ...interface{}) { l.p.Fatal(args...) }
  44. func (l *packageLogger) Fatalln(args ...interface{}) { l.p.Fatal(args...) }
  45. func (l *packageLogger) Fatalf(format string, args ...interface{}) { l.p.Fatalf(format, args...) }
  46. func (l *packageLogger) V(lvl int) bool {
  47. return l.p.LevelAt(capnslog.LogLevel(lvl))
  48. }
  49. func (l *packageLogger) Lvl(lvl int) grpclog.LoggerV2 {
  50. if l.p.LevelAt(capnslog.LogLevel(lvl)) {
  51. return l
  52. }
  53. return &discardLogger{}
  54. }