最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Log4j2 - Log Custom Object With Nested Objects as JSON - Stack Overflow

programmeradmin1浏览0评论

I need to log a custom object as plain JSON string, without any extra log event details. I tried to use .apache.logging.log4j.message.MapMessage (solution is described here ), though it seems that it supports only one level flat JSON object, that doesn't have inner JSON objects. The MapMessage has put(String, String) method that accepts only values that are strings, so there is no option to store a complex value, another Object. I also tried the with method that accepts an Object, with("key1", obj) , but it only prints what Object.toString() can print:

    {"id1":"11111111-1111-1111-1111-111111111111","obj":"company.CustomLog$MyClass@3c80be62","id2":"22222222-2222-2222-2222-222222222222"}

In the documentation I read an interesting phrase, that I couldn't decipher (.12.x/manual/messages.html):

When an Appender is MessageLayout-aware, the object Log4j sends to target is not a Log4j Log Event but a custom object.

Does anybody has an idea about the possible implementation?

发布评论

评论列表(0)

  1. 暂无评论