记录 HTTP 端点和有效负载 GRPC

问题描述

我想在 grpc 上使用 logrus 记录我的负载。我已经使用 grpc_middleware 制作了它,但日志格式不是我想要的。我使用 *logrus.Logger 作为 logrus.NewEntry() 的参数,代码就像这样:

logrusEntry := logrus.NewEntry(logger)

    opts := []grpc_logrus.Option{grpc_logrus.WithDurationField(func(duration time.Duration) (key string,value interface{}) {
        return "grpc.time_ns",duration.Nanoseconds()
    })}

    alwaysLoggingDeciderServer := func(ctx context.Context,fullMethodName string,servingObject interface{}) bool { return true }

    // register grpc service server
    grpcServer := grpc.NewServer(
        grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(AuthInterceptor,grpc_ctxtags.UnaryServerInterceptor(),grpc_logrus.UnaryServerInterceptor(logrusEntry,opts...),grpc_logrus.PayloadUnaryServerInterceptor(logrusEntry,alwaysLoggingDeciderServer))))

上面代码的日志输出是这样的:

server request payload logged as grpc.request.content field  [36mgrpc.method[0m="FormData" [36mgrpc.request.content[0m="bankid:\"01\"  userid:\"085156173506\"  applicationid:\"17235\"" [36mgrpc.service[0m="api.pinangochgo" [36mgrpc.start_time[0m="2021-04-30T11:18:45+07:00" [36mpeer.address[0m="127.0.0.1:34182" [36mspan.kind[0m="server" [36msystem[0m="grpc"

我希望它是像这样显示 http url 的 JSON 格式:

{
  "level": "info",// string logrus log levels
  "msg": "client request payload logged as grpc.request.content",// string log message

  "grpc.request.content": {                     // object content of RPC request
    "value": "something",// string defined by caller
    "sleepTimeMs": 9999                         // int    defined by caller
  },"grpc.method": "Ping",// string method being called
  "grpc.service": "mwitkow.testproto.TestService",// string service being called
  "span.kind": "client",// string client | server
  "system": "grpc"                          // string
}

我还想记录它已经在 proto 中定义的 http 端点

// CsUpdate service provides utility methods for the API.
service Serve {
  rpc Update(RetrieveRequest) returns (RetrieveResponse){
    option (google.api.http) = {
      post  : "/bank/{bankId}/custom/{applicationId}"
      body  : "*"
    };
  }
}

其实我希望它是这样的:

2021-02-19 21:55:43.770 D/FYZ: Request To: {url}/v1/banks/01/users
2021-02-19 21:55:43.770 D/FYZ: Method: 2
2021-02-19 21:55:43.771 D/FYZ: Request Parameter: {"accesscode":"0","authenticationMode":"","authenticationModeType":"S","channelId":"2","deviceid":"","isPrelogin":"","rmPlusAuthentication":"","secret":"","tokenrequired":"N"}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...