Log4j 2 JSON Configuration

谁说我不能喝 提交于 2019-11-30 21:20:36
bblincoe

I found a solution to the problem.

It turns out that the Log4j 2 Configuration doesn't document all the required dependencies:

The JSON support uses the Jackson Data Processor to parse the JSON files. These dependencies must be added to a project that wants to use JSON for configuration:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.7</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.7</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.7</version>
</dependency>

I believe that I have an additional correction to the JSON configuration that was posted. For me, this part produces problems:

  "properties": {
     "property": {
        "name":"Directory",
        "value":"${sys:user.home}/logs"
     },
     "property": {
        "name":"FileName",
        "value":"test.log"
     }
  },

On my setup, this will output ~/logs/${FileName} or ~/${Directory}/test.log, which means that it is only interpolating one of the properties instead of both.

I concluded from this that the properties were not specified correctly in JSON form. To fix it, I specified the properties as a JSON array

    "properties": {
        "property": [{
            "name":"Directory",
            "value":"${sys:user.home}/logs"
        },
        {
            "name":"FileName",
            "value":"test.log"
        }]
    },

With this in place, it seems to fix the problem and correctly outputs to ~/logs/test.log

My setup is log4j2 2.0.2 on Mac with tomcat 7.0.55

You have another flaw in there that might catch you, that I've run into with log4j json.

"appender-ref": {
    "ref":"Console"
},
"appender-ref": {
    "ref":"File"
}

Try this instead, as it is now, it will only catch one appender.

"appender-ref": [{
    "ref":"Console"
},
{
    "ref":"File"
}]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!