根据log4j配置文件位置的不同行为

时间:2015-12-26 11:41:33

标签: java log4j

我有 log4j.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
    debug="true">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{HH:mm:ss,SSS}] %-5p [%c] %m%n" />
        </layout>
    </appender>
    <logger name="org.apache.commons.httpclient">
        <level value="fatal" />
    </logger>

    <logger name="org.apache.http.client">
        <level value="fatal" />
    </logger>
    <logger name="org.apache.http.wire">
        <level value="fatal" />
    </logger>

    <logger name="org.apache.http.headers">
        <level value="fatal" />
    </logger>
    <logger name="org.apache.http.impl">
        <level value="fatal" />
    </logger>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="console" />
    </root>
</log4j:configuration>

如果它在主程序包中,项目就像这样开始:

java -jar -Dlog4j.configuration=log4j.xml api.jar

它按预期工作(不要看org.apache.http日志)。

但如果我把它放在 /resources/log4j.xml 中,项目就像这样开始:

java -jar -Dlog4j.configuration=/resources/log4j.xml api.jar

忽略org.apache.http日志的所有规则都不起作用,我看到了所有这些日志:

[15:09:24,127] DEBUG [org.apache.http.headers] http-outgoing-0 << HTTP/1.1 202 Accepted
[15:09:24,127] DEBUG [org.apache.http.headers] http-outgoing-0 << Content-Type: text/json
[15:09:24,127] DEBUG [org.apache.http.headers] http-outgoing-0 << Date: Fri, 25 Dec 2015 21:09:24 GMT
[15:09:24,127] DEBUG [org.apache.http.headers] http-outgoing-0 << DD-POOL: propjoe
[15:09:24,127] DEBUG [org.apache.http.headers] http-outgoing-0 << Strict-Transport-Security: max-age=15724800;
[15:09:24,127] DEBUG [org.apache.http.headers] http-outgoing-0 << X-Content-Type-Options: nosniff
[15:09:24,127] DEBUG [org.apache.http.headers] http-outgoing-0 << Content-Length: 16
[15:09:24,127] DEBUG [org.apache.http.headers] http-outgoing-0 << Connection: keep-alive

为什么?

1 个答案:

答案 0 :(得分:0)

添加&#34; log4j.debug = true&#34;在环境中,您可以在命令行上查看log4j的初始化。因此,您可以查看在哪个位置搜索哪个配置文件,以及将哪个配置文件用作配置。我假设找不到资源文件夹中的文件,但还有其他内容。

删除前导斜杠。 log4j.xml作为资源加载,必须在classpath中找到。