zap_grpc.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "go.uber.org/zap"
  17. "go.uber.org/zap/zapcore"
  18. "google.golang.org/grpc/grpclog"
  19. )
  20. // NewGRPCLoggerV2 converts "*zap.Logger" to "grpclog.LoggerV2".
  21. // It discards all INFO level logging in gRPC, if debug level
  22. // is not enabled in "*zap.Logger".
  23. func NewGRPCLoggerV2(lcfg zap.Config) (grpclog.LoggerV2, error) {
  24. lg, err := lcfg.Build(zap.AddCallerSkip(1)) // to annotate caller outside of "logutil"
  25. if err != nil {
  26. return nil, err
  27. }
  28. return &zapGRPCLogger{lg: lg, sugar: lg.Sugar()}, nil
  29. }
  30. type zapGRPCLogger struct {
  31. lg *zap.Logger
  32. sugar *zap.SugaredLogger
  33. }
  34. func (zl *zapGRPCLogger) Info(args ...interface{}) {
  35. if !zl.lg.Core().Enabled(zapcore.DebugLevel) {
  36. return
  37. }
  38. zl.sugar.Info(args...)
  39. }
  40. func (zl *zapGRPCLogger) Infoln(args ...interface{}) {
  41. if !zl.lg.Core().Enabled(zapcore.DebugLevel) {
  42. return
  43. }
  44. zl.sugar.Info(args...)
  45. }
  46. func (zl *zapGRPCLogger) Infof(format string, args ...interface{}) {
  47. if !zl.lg.Core().Enabled(zapcore.DebugLevel) {
  48. return
  49. }
  50. zl.sugar.Infof(format, args...)
  51. }
  52. func (zl *zapGRPCLogger) Warning(args ...interface{}) {
  53. zl.sugar.Warn(args...)
  54. }
  55. func (zl *zapGRPCLogger) Warningln(args ...interface{}) {
  56. zl.sugar.Warn(args...)
  57. }
  58. func (zl *zapGRPCLogger) Warningf(format string, args ...interface{}) {
  59. zl.sugar.Warnf(format, args...)
  60. }
  61. func (zl *zapGRPCLogger) Error(args ...interface{}) {
  62. zl.sugar.Error(args...)
  63. }
  64. func (zl *zapGRPCLogger) Errorln(args ...interface{}) {
  65. zl.sugar.Error(args...)
  66. }
  67. func (zl *zapGRPCLogger) Errorf(format string, args ...interface{}) {
  68. zl.sugar.Errorf(format, args...)
  69. }
  70. func (zl *zapGRPCLogger) Fatal(args ...interface{}) {
  71. zl.sugar.Fatal(args...)
  72. }
  73. func (zl *zapGRPCLogger) Fatalln(args ...interface{}) {
  74. zl.sugar.Fatal(args...)
  75. }
  76. func (zl *zapGRPCLogger) Fatalf(format string, args ...interface{}) {
  77. zl.sugar.Fatalf(format, args...)
  78. }
  79. func (zl *zapGRPCLogger) V(l int) bool {
  80. // infoLog == 0
  81. if l <= 0 { // debug level, then we ignore info level in gRPC
  82. return !zl.lg.Core().Enabled(zapcore.DebugLevel)
  83. }
  84. return true
  85. }