Log4j2的PatternLayout跳过括号中的模式

时间:2015-03-26 10:08:46

标签: java log4j2

我似乎无法正确打印以下模式[%level] %message%exception%n。每次我调用它时,它只会打印%message%exception(以及%n)。如何在括号之间打印%level

,我需要做什么?

这是我的Log4j2配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%level] %message%exception{short}%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

这是一个显示问题的单元测试:

import org.apache.logging.log4j.LogManager;
import org.junit.Test;
public class Log4j2Test {
  @Test public void testLogging() {
    LogManager.getLogger().info("expecting [INFO]", new RuntimeException());
  }
}

此测试的结果如下:

Running Log4j2Test
expecting [INFO] java.lang.RuntimeException
    at Log4j2Test.testLogging(Log4j2Test.java:19)
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.453 sec

那么我做错了什么?

编辑:这是我使用的pom.xml。只需将版本更改为2.1或2.2,就可以了。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>be.fror</groupId>
    <artifactId>tmp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

这是一个NetBeans问题,而不是Log4j2:https://netbeans.org/bugzilla/show_bug.cgi?id=251448