我想在 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"
我希望它是这样的 JSON 格式,显示 http url:
{
"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"}