Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame^] | 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 | |
| 15 | package logutil |
| 16 | |
| 17 | import ( |
| 18 | "go.uber.org/zap" |
| 19 | "go.uber.org/zap/zapcore" |
| 20 | "google.golang.org/grpc/grpclog" |
| 21 | ) |
| 22 | |
| 23 | // NewGRPCLoggerV2 converts "*zap.Logger" to "grpclog.LoggerV2". |
| 24 | // It discards all INFO level logging in gRPC, if debug level |
| 25 | // is not enabled in "*zap.Logger". |
| 26 | func NewGRPCLoggerV2(lcfg zap.Config) (grpclog.LoggerV2, error) { |
| 27 | lg, err := lcfg.Build(zap.AddCallerSkip(1)) // to annotate caller outside of "logutil" |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | return &zapGRPCLogger{lg: lg, sugar: lg.Sugar()}, nil |
| 32 | } |
| 33 | |
| 34 | // NewGRPCLoggerV2FromZapCore creates "grpclog.LoggerV2" from "zap.Core" |
| 35 | // and "zapcore.WriteSyncer". It discards all INFO level logging in gRPC, |
| 36 | // if debug level is not enabled in "*zap.Logger". |
| 37 | func NewGRPCLoggerV2FromZapCore(cr zapcore.Core, syncer zapcore.WriteSyncer) grpclog.LoggerV2 { |
| 38 | // "AddCallerSkip" to annotate caller outside of "logutil" |
| 39 | lg := zap.New(cr, zap.AddCaller(), zap.AddCallerSkip(1), zap.ErrorOutput(syncer)) |
| 40 | return &zapGRPCLogger{lg: lg, sugar: lg.Sugar()} |
| 41 | } |
| 42 | |
| 43 | type zapGRPCLogger struct { |
| 44 | lg *zap.Logger |
| 45 | sugar *zap.SugaredLogger |
| 46 | } |
| 47 | |
| 48 | func (zl *zapGRPCLogger) Info(args ...interface{}) { |
| 49 | if !zl.lg.Core().Enabled(zapcore.DebugLevel) { |
| 50 | return |
| 51 | } |
| 52 | zl.sugar.Info(args...) |
| 53 | } |
| 54 | |
| 55 | func (zl *zapGRPCLogger) Infoln(args ...interface{}) { |
| 56 | if !zl.lg.Core().Enabled(zapcore.DebugLevel) { |
| 57 | return |
| 58 | } |
| 59 | zl.sugar.Info(args...) |
| 60 | } |
| 61 | |
| 62 | func (zl *zapGRPCLogger) Infof(format string, args ...interface{}) { |
| 63 | if !zl.lg.Core().Enabled(zapcore.DebugLevel) { |
| 64 | return |
| 65 | } |
| 66 | zl.sugar.Infof(format, args...) |
| 67 | } |
| 68 | |
| 69 | func (zl *zapGRPCLogger) Warning(args ...interface{}) { |
| 70 | zl.sugar.Warn(args...) |
| 71 | } |
| 72 | |
| 73 | func (zl *zapGRPCLogger) Warningln(args ...interface{}) { |
| 74 | zl.sugar.Warn(args...) |
| 75 | } |
| 76 | |
| 77 | func (zl *zapGRPCLogger) Warningf(format string, args ...interface{}) { |
| 78 | zl.sugar.Warnf(format, args...) |
| 79 | } |
| 80 | |
| 81 | func (zl *zapGRPCLogger) Error(args ...interface{}) { |
| 82 | zl.sugar.Error(args...) |
| 83 | } |
| 84 | |
| 85 | func (zl *zapGRPCLogger) Errorln(args ...interface{}) { |
| 86 | zl.sugar.Error(args...) |
| 87 | } |
| 88 | |
| 89 | func (zl *zapGRPCLogger) Errorf(format string, args ...interface{}) { |
| 90 | zl.sugar.Errorf(format, args...) |
| 91 | } |
| 92 | |
| 93 | func (zl *zapGRPCLogger) Fatal(args ...interface{}) { |
| 94 | zl.sugar.Fatal(args...) |
| 95 | } |
| 96 | |
| 97 | func (zl *zapGRPCLogger) Fatalln(args ...interface{}) { |
| 98 | zl.sugar.Fatal(args...) |
| 99 | } |
| 100 | |
| 101 | func (zl *zapGRPCLogger) Fatalf(format string, args ...interface{}) { |
| 102 | zl.sugar.Fatalf(format, args...) |
| 103 | } |
| 104 | |
| 105 | func (zl *zapGRPCLogger) V(l int) bool { |
| 106 | // infoLog == 0 |
| 107 | if l <= 0 { // debug level, then we ignore info level in gRPC |
| 108 | return !zl.lg.Core().Enabled(zapcore.DebugLevel) |
| 109 | } |
| 110 | return true |
| 111 | } |