zap_grpc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // NewGRPCLoggerV2FromZapCore creates "grpclog.LoggerV2" from "zap.Core"
  31. // and "zapcore.WriteSyncer". It discards all INFO level logging in gRPC,
  32. // if debug level is not enabled in "*zap.Logger".
  33. func NewGRPCLoggerV2FromZapCore(cr zapcore.Core, syncer zapcore.WriteSyncer) grpclog.LoggerV2 {
  34. // "AddCallerSkip" to annotate caller outside of "logutil"
  35. lg := zap.New(cr, zap.AddCaller(), zap.AddCallerSkip(1), zap.ErrorOutput(syncer))
  36. return &zapGRPCLogger{lg: lg, sugar: lg.Sugar()}
  37. }
  38. type zapGRPCLogger struct {
  39. lg *zap.Logger
  40. sugar *zap.SugaredLogger
  41. }
  42. func (zl *zapGRPCLogger) Info(args ...interface{}) {
  43. if !zl.lg.Core().Enabled(zapcore.DebugLevel) {
  44. return
  45. }
  46. zl.sugar.Info(args...)
  47. }
  48. func (zl *zapGRPCLogger) Infoln(args ...interface{}) {
  49. if !zl.lg.Core().Enabled(zapcore.DebugLevel) {
  50. return
  51. }
  52. zl.sugar.Info(args...)
  53. }
  54. func (zl *zapGRPCLogger) Infof(format string, args ...interface{}) {
  55. if !zl.lg.Core().Enabled(zapcore.DebugLevel) {
  56. return
  57. }
  58. zl.sugar.Infof(format, args...)
  59. }
  60. func (zl *zapGRPCLogger) Warning(args ...interface{}) {
  61. zl.sugar.Warn(args...)
  62. }
  63. func (zl *zapGRPCLogger) Warningln(args ...interface{}) {
  64. zl.sugar.Warn(args...)
  65. }
  66. func (zl *zapGRPCLogger) Warningf(format string, args ...interface{}) {
  67. zl.sugar.Warnf(format, args...)
  68. }
  69. func (zl *zapGRPCLogger) Error(args ...interface{}) {
  70. zl.sugar.Error(args...)
  71. }
  72. func (zl *zapGRPCLogger) Errorln(args ...interface{}) {
  73. zl.sugar.Error(args...)
  74. }
  75. func (zl *zapGRPCLogger) Errorf(format string, args ...interface{}) {
  76. zl.sugar.Errorf(format, args...)
  77. }
  78. func (zl *zapGRPCLogger) Fatal(args ...interface{}) {
  79. zl.sugar.Fatal(args...)
  80. }
  81. func (zl *zapGRPCLogger) Fatalln(args ...interface{}) {
  82. zl.sugar.Fatal(args...)
  83. }
  84. func (zl *zapGRPCLogger) Fatalf(format string, args ...interface{}) {
  85. zl.sugar.Fatalf(format, args...)
  86. }
  87. func (zl *zapGRPCLogger) V(l int) bool {
  88. // infoLog == 0
  89. if l <= 0 { // debug level, then we ignore info level in gRPC
  90. return !zl.lg.Core().Enabled(zapcore.DebugLevel)
  91. }
  92. return true
  93. }