gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-grpc-middleware/logging/zap/grpclogger.go (about) 1 // Copyright 2017 Michal Witkowski. All Rights Reserved. 2 // See LICENSE for licensing terms. 3 4 package grpc_zap 5 6 import ( 7 "fmt" 8 "gitee.com/ks-custle/core-gm/grpc/grpclog" 9 "go.uber.org/zap" 10 ) 11 12 // ReplaceGrpcLogger sets the given zap.Logger as a gRPC-level logger. 13 // This should be called *before* any other initialization, preferably from init() functions. 14 func ReplaceGrpcLogger(logger *zap.Logger) { 15 zgl := &zapGrpcLogger{logger.With(SystemField, zap.Bool("grpc_log", true))} 16 // grpclog.SetLogger is deprecated, use grpclog.SetLoggerV2 instead. 17 //grpclog.SetLogger(zgl) 18 grpclog.SetLoggerV2(zgl) 19 } 20 21 type zapGrpcLogger struct { 22 logger *zap.Logger 23 } 24 25 func (l *zapGrpcLogger) Fatal(args ...interface{}) { 26 l.logger.Fatal(fmt.Sprint(args...)) 27 } 28 29 func (l *zapGrpcLogger) Fatalf(format string, args ...interface{}) { 30 l.logger.Fatal(fmt.Sprintf(format, args...)) 31 } 32 33 func (l *zapGrpcLogger) Fatalln(args ...interface{}) { 34 l.logger.Fatal(fmt.Sprint(args...)) 35 } 36 37 func (l *zapGrpcLogger) Print(args ...interface{}) { 38 l.logger.Info(fmt.Sprint(args...)) 39 } 40 41 func (l *zapGrpcLogger) Printf(format string, args ...interface{}) { 42 l.logger.Info(fmt.Sprintf(format, args...)) 43 } 44 45 func (l *zapGrpcLogger) Println(args ...interface{}) { 46 l.logger.Info(fmt.Sprint(args...)) 47 } 48 49 //goland:noinspection GoUnusedParameter 50 func (l *zapGrpcLogger) V(le int) bool { 51 // V 接口是检查verbosity level 是否大于等于 请求的 verbose level。 52 // 但 *zap.Logger 没有verbosity level,这里直接返回true。 53 return true 54 } 55 56 func (l *zapGrpcLogger) Info(args ...interface{}) { 57 l.logger.Info(fmt.Sprint(args...)) 58 } 59 60 func (l *zapGrpcLogger) Infoln(args ...interface{}) { 61 l.logger.Info(fmt.Sprint(args...)) 62 } 63 64 func (l *zapGrpcLogger) Infof(format string, args ...interface{}) { 65 l.logger.Info(fmt.Sprintf(format, args...)) 66 } 67 68 func (l *zapGrpcLogger) Warning(args ...interface{}) { 69 l.logger.Warn(fmt.Sprint(args...)) 70 } 71 72 func (l *zapGrpcLogger) Warningln(args ...interface{}) { 73 l.logger.Warn(fmt.Sprint(args...)) 74 } 75 76 func (l *zapGrpcLogger) Warningf(format string, args ...interface{}) { 77 l.logger.Warn(fmt.Sprintf(format, args...)) 78 } 79 80 func (l *zapGrpcLogger) Error(args ...interface{}) { 81 l.logger.Error(fmt.Sprint(args...)) 82 } 83 84 func (l *zapGrpcLogger) Errorln(args ...interface{}) { 85 l.logger.Error(fmt.Sprint(args...)) 86 } 87 88 func (l *zapGrpcLogger) Errorf(format string, args ...interface{}) { 89 l.logger.Error(fmt.Sprintf(format, args...)) 90 }