Does log4j support JSON format?

前端 未结 4 1287
鱼传尺愫
鱼传尺愫 2021-02-01 03:17

Is it possible to let log4j output its logging in JSON format by only altering the log4j.properties.xml configuration file?
I make use of an old application tha

4条回答
  •  遇见更好的自我
    2021-02-01 03:57

    just use buildin PatternLayout is ok:

    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.encoding=UTF-8
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern={"debug_level":"%p","debug_timestamp":"%d{ISO8601}","debug_thread":"%t","debug_file":"%F", "debug_line":"%L","debug_message":"%m"}%n
    

    will out put like:

    {
        "debug_level" : "INFO",
        "debug_timestamp" : "2016-05-26 16:37:08,938",
        "debug_thread" : "main",
        "debug_file" : "TestLogOutPut.java",
        "debug_line" : "316",
        "debug_message" : "hello i am a log message"
    }
    

提交回复
热议问题