Java Log4j 2 JSON 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21435990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:42:46  来源:igfitidea点击:

Log4j 2 JSON Configuration

javajsonlogginglog4j2

提问by bblincoe

I have a configuration in XML that I would like to convert to JSON. The JSON version is not being loaded by Log4j and I cannot find any typos. My test code simply logs an ERRORlevel and a DEBUGlevel message. Only ERRORmessages are being displayed and no file output is being generated - I'm assuming the framework falls back to the default initialization instead of the JSON file.

我有一个 XML 配置,我想将其转换为 JSON。Log4j 未加载 JSON 版本,我找不到任何拼写错误。我的测试代码只是记录一个ERROR级别和一个DEBUG级别消息。只ERROR显示消息并且没有生成文件输出 - 我假设框架回退到默认初始化而不是 JSON 文件。

Note: The log4j2-test.json file is on the classpath.

注意:log4j2-test.json 文件位于类路径中。

I'm using apache-log4j-2.0-beta9binary found here.

我正在使用此处找到的apache-log4j-2.0-beta9二进制文件。

The XML configuration is the following:

XML 配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration name="Test">
    <Properties>
        <Property name="Directory">${sys:user.home}/logs</Property>
        <Property name="Filename">test.log</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingFile name="File" 
            fileName="${Directory}/${Filename}" 
            filePattern="${Directory}/${date:yyyy-MM}/test-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %logger{36} [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="1 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        </Root>
    </Loggers>
</Configuration>

and the JSON configuration is:

JSON 配置为:

{
   "configuration": {
      "name": "Default",
      "properties": {
         "property": {
            "name":"Directory",
            "value":"${sys:user.home}/logs"
         },
         "property": {
            "name":"FileName",
            "value":"test.log"
         }
      },
      "appenders": {
         "Console": {
            "name":"Console",
            "target":"SYSTEM_OUT",
            "PatternLayout": {
               "pattern":"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
            }
         },
         "RollingFile": {
            "name":"File",
            "fileName":"${Directory}/${FileName}",
            "filePattern":"${Directory}/${date:yyyy-MM}/test-%d{MM-dd-yyyy}-%i.log.gz",
            "PatternLayout": {
               "pattern":"%d %p %logger{36} [%t] %m%n"
            },
            "Policies": {
               "SizeBasedTriggeringPolicy": {
                  "size":"1 MB"
               }
            },
            "DefaultRolloverStrategy": {
               "max":"10"
            }
         }
      },
      "loggers": {
         "root": {
            "level":"debug",
            "appender-ref": {
               "ref":"Console"
            },
            "appender-ref": {
              "ref":"File"
            }
         }
      }
   }
}

采纳答案by bblincoe

I found a solution to the problem.

我找到了解决问题的方法。

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

事实证明,Log4j 2 配置没有记录所有必需的依赖项:

The JSON support uses the Hymanson 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.Hymanson.core</groupId>
    <artifactId>Hymanson-core</artifactId>
    <version>2.8.7</version>
</dependency>

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

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

JSON 支持使用 Hymanson 数据处理器来解析 JSON 文件。必须将这些依赖项添加到要使用 JSON 进行配置的项目中:

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

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

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

回答by Colin D

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

我相信我对发布的 JSON 配置进行了额外的更正。对我来说,这部分会产生问题:

  "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.

在我的设置中,这将输出 ~/logs/${FileName} 或 ~/${Directory}/test.log,这意味着它只插入属性之一而不是两者。

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

我由此得出结论,未以 JSON 形式正确指定属性。为了修复它,我将属性指定为 JSON 数组

    "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

有了这个,它似乎解决了问题并正确输出到 ~/logs/test.log

My setup is log4j2 2.0.2 on Mac with tomcat 7.0.55

我的设置是带有 tomcat 7.0.55 的 Mac 上的 log4j2 2.0.2

回答by squishy

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

您还有另一个可能会引起您注意的缺陷,我在使用 log4j json 时遇到过。

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

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

试试这个,就像现在一样,它只会捕获一个 appender。

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