简体   繁体   English

log4j2版本'2.10'配置模式

[英]log4j2 version '2.10' configuration pattern

Something strange is happening when i'm trying to use log4j2 version 2.10. 当我尝试使用log4j2 2.10版时,发生了一些奇怪的事情。

Every example including those ones from log4j2 documentation seems wrong. 每个示例(包括log4j2文档中的示例)似乎都是错误的。

Consider this example from: https://logging.apache.org/log4j/2.x/manual/configuration.html 考虑以下示例: https : //logging.apache.org/log4j/2.x/manual/configuration.html

<?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>

Using the above example i got the following output: 使用上面的示例,我得到以下输出:

log4j:WARN Continuable parsing error 2 and column 30
log4j:WARN Document root element "Configuration", must match DOCTYPE root "null".
log4j:WARN Continuable parsing error 2 and column 30
log4j:WARN Document is invalid: no grammar found.
log4j:ERROR DOM element is - not a <log4j:configuration> element.
log4j:WARN No appenders could be found for logger (utils.Xablau).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

And nothing is logged. 并没有记录任何内容。

To solve this i have to use other pattern like this: 为了解决这个问题,我必须使用其他模式,例如:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} [%-5p] [%t] %m%n" />
        </layout>
    </appender>

    <appender name="FILE" class="org.apache.log4j.FileAppender">
        <param name="file"
               value="C:/tmp/logs/functional-test.log" />
        <param name="immediateFlush" value="true" />
        <param name="threshold" value="debug" />
        <param name="append" value="true" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%m%n" />
        </layout>
    </appender>

    <category name="se.jayway.ddsteps">
        <priority value="info"></priority>
    </category>

    <root>
        <priority value="INFO"></priority>
        <appender-ref ref="console" />
        <appender-ref ref="FILE" />
    </root>

</log4j:configuration>

That's the only way it works and log stuff. 这是它工作和记录内容的唯一方法。

btw i'm using the following maven dependency imports: 顺便说一句,我正在使用以下Maven依赖项导入:

<dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>

The new working configuration that you have used is of log4j , not log4j2 . 您使用的新工作配置是log4j ,而不是log4j2

I think Actual log4j2 configuration (first one) is not working because of dependency issue. 我认为由于依赖关系问题,实际的log4j2配置(第一个)不起作用。

Following dependencies must be in pom.xml for log4j2 to make it work - 以下依赖关系必须在pom.xml中才能使log4j2正常工作-

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.10.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>log4j-core</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM