Open
Description
下面是我看到源码中grpc的客户端logger的中间件, 服务器也一样, 不管error是不是nil, 都通过logger.Info来打印, 这样不是太好, 其他人没法通过日志等级来控制
func UnaryClientLog(logger *zap.Logger, opts ...LogOption) grpc.UnaryClientInterceptor {
o := defaultLogOptions()
o.apply(opts...)
if logger == nil {
logger, _ = zap.NewProduction()
}
if o.isReplaceGRPCLogger {
zapLog.ReplaceGRPCLoggerV2(logger)
}
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
startTime := time.Now()
var reqIDField zap.Field
if requestID := ClientCtxRequestID(ctx); requestID != "" {
reqIDField = zap.String(ContextRequestIDKey, requestID)
} else {
reqIDField = zap.Skip()
}
err := invoker(ctx, method, req, reply, cc, opts...)
fields := []zap.Field{
zap.String("code", status.Code(err).String()),
zap.Error(err),
zap.String("type", "unary"),
zap.String("method", method),
zap.Int64("time_us", time.Since(startTime).Microseconds()),
reqIDField,
}
logger.Info("invoker result", fields...)
return err
}
}
Metadata
Metadata
Assignees
Labels
No labels