How to log Protobuf string in nested objects in a human-readable way?

后端 未结 2 749
栀梦
栀梦 2021-01-23 18:19

Given a proto file:

syntax = "proto3";
package hello;

message TopGreeting {
    NestedGreeting greeting = 1;
}

message NestedGreeting {
    Greeting g         


        
相关标签:
2条回答
  • 2021-01-23 18:56

    Answering my own question, I solved this issue by digging through Protobuf source code.

    System.out.println(TextFormat.printer().escapingNonAscii(false).printToString(greeting))
    

    Output:

    greeting {
      greeting {
        message: "오늘은 무슨 요일입니까?"
      }
    }
    

    toString uses the same mechanism but with escapingNonAscii(true) (default when omitted).

    Also see this answer for how to convert Octal sequences to UTF-8 characters in case you don't have access to the source code, only logs.

    0 讨论(0)
  • 2021-01-23 18:57

    The protobuf binary format isn't human readable and you shouldn't attempt to make it so. There is a JSON variant if you need, but frankly it would be better to log the interpreted data, not the payloads.

    0 讨论(0)
提交回复
热议问题