Graylog2 cannot store Chinese when storing json in GELF format in UDP format

want to use graylog to store the login log of the system. The java code is as follows:

private static void send(String host, int port, Map<String, Object> params) throws Exception {
    if (SOCKET == null) {
        SOCKET = new DatagramSocket();
    }
    InetAddress address = InetAddress.getByName(host);
    String content = JSON.toJSONString(params);
    DatagramPacket packet = new DatagramPacket(content.getBytes("UTF-8"), content.length(), address, port);
    SOCKET.send(packet);
}

where content string is as follows:

{
"version":"1.1",
"host":"test.com",
"short_message":"loginLog",
"full_message":"loginLog",
"_name":"",
"_result":"SUCCESS:",
"_user_id":"f69cf0a3-ad33-4b7f-b770-ea18d615f6a1",
"_username":"test",
"_ip":"192.168.1.33"
}

after repeated tests, once the field contains Chinese, it cannot be stored successfully, but the description of GELF in Graylog"s document is as follows:

  • _ [additional field] string (UTF-8) or number

every field you send and prefix with an underscore (_) will be treated as an additional field. Allowed characters in field names are any word character (letter, number, underscore), dashes and dots. The verifying regular expression is: ^ [w. -] * $. Libraries SHOULD not allow to send id as additional field (_ id). Graylog server nodes omit this field automatically.

just use utf-8 encoding.

try again and again but still can"t solve it, come to ask for help.
I hope all the bosses can give me some suggestions. Thank you very much!

Mar.01,2021

solved. A very stupid mistake, just change the java code to the following:

private static void send(String host, int port, Map<String, Object> params) throws Exception {
    if (SOCKET == null) {
        SOCKET = new DatagramSocket();
    }
    InetAddress address = InetAddress.getByName(host);
    String content = JSON.toJSONString(params);
    byte[] bytes = content.getBytes("UTF-8");
    DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, port);
    SOCKET.send(packet);
}

so the problem is that the length of byte [] containing Chinese is not the same as the previous string.

Menu