2

I want to log my payload using Logrus on grpc. I've made it using grpc_middleware but the log format is not what I want. I'm using *logrus.Logger as param for logrus.NewEntry() the code is just like this:

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))))

Log output from above code is like this:

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"

I want it to be in JSON format like this that show 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
}

I also want to log its http endpoint that already defined in proto

// 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  : "*"
    };
  }
}

Actually I want it to be like this:

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"}

Ken White
  • 123,280
  • 14
  • 225
  • 444
Enryu
  • 91
  • 11

0 Answers0