什么是YAML中的log4j2示例默认配置文件?

时间:2015-01-23 02:02:53

标签: yaml log4j2

log4j2配置的documentation提供了一个与默认配置等效的简单示例XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

等效文件看起来像(有效)YAML?

1 个答案:

答案 0 :(得分:56)

YAML文件

等效的YAML文件( log4j2.yaml )如下所示:

Configuration:
  status: warn

  Appenders:
    Console:
      name: Console
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"

  Loggers:
    Root:
      level: error
      AppenderRef:
        ref: Console

此文件会自动加载(假设文件位于类路径中且具有所需的依赖关系),如Automatic Configuration中所述。

Maven

如果您使用的是Maven,则需要以下依赖项:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
    <version>2.5.0</version>
</dependency>

Imgur