Log4j 2 JSON配置 [英] Log4j 2 JSON Configuration

查看:119
本文介绍了Log4j 2 JSON配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ERROR level and a DEBUG level message. Only ERROR messages 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.

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

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

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>

,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"
            }
         }
      }
   }
}


推荐答案

我找到了该问题的解决方案。

I found a solution to the problem.

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

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


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

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>


这篇关于Log4j 2 JSON配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆