无论根级别

时间:2017-02-01 15:10:02

标签: java log4j

我们有一些独立的java应用程序,它们将信息记录到日志文件中,使用log4j(它是旧版本:1.2.8)。

我们已将根级别设置为ERROR,并根据调用模块定义了几个appender。

奇怪的是,无论级别如何(INFO,DEBUG等)都会记录所有内容。然后我们决定将-Dlog4j.debug选项添加到java命令行,以确定是否没有加载错误的log4.xml文件,然后我们看到了这个:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@64c3c749.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@64c3c749 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@64c3c749.
log4j: Trying to find [log4j.properties] using sun.misc.Launcher$AppClassLoader@64c3c749 class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].

为什么log4j会记录所有内容,即使根级别设置为ERROR,以及如何在无法找到配置的情况下加载配置?

编辑:

1)是的,所有内容都记录在正确的appender中,格式和文件名正确 2)log4j.xml位于jar路径中,在类路径中指定。

这是log4j.xml:

<!-- Definition des appenders par niveau de log -->
<appender name="all" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_ARBOR2010_ALL.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="levelToMatch" value="ERROR"/>
    </filter>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>

<appender name="debug" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_ARBOR2010_DEBUG.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="levelToMatch" value="DEBUG"/>
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>

<appender name="info" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_ARBOR2010_INFO.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="levelToMatch" value="INFO"/>
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>

<appender name="warn" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_ARBOR2010_WARN.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="levelToMatch" value="WARN"/>
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>

<appender name="error" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_ARBOR2010_ERROR.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="levelToMatch" value="ERROR"/>
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>

<appender name="fatal" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_ARBOR2010_FATAL.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="levelToMatch" value="FATAL"/>
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>

<!-- Definition des appenders specifiques -->
<!-- MVNO 2012 -->
<appender name="mvno2012" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_MVNO2012.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.PatternLayout">            
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %t | %L | %m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">            
        <param name="levelMin" value="DEBUG"/>
        <param name="levelMax" value="FATAL"/>
    </filter>
</appender>

<!-- Moteur de collecte -->
<appender name="engine_collecte" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_COLLECTE_ENGINE.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">            
        <param name="levelMin" value="DEBUG"/>
        <param name="levelMax" value="FATAL"/>
    </filter>
    <layout class="org.apache.log4j.PatternLayout">            
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %t | %L | %m%n" />
    </layout>
</appender>

<!-- Utilitaires apache (org.apache ...) -->
<appender name="apache_utilities" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_APACHE_UTILITIES.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="levelMin" value="DEBUG"/>
        <param name="levelMax" value="FATAL"/>
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>



<appender name="aruba" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_ARUBA.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">            
        <param name="levelMin" value="DEBUG"/>
        <param name="levelMax" value="FATAL"/>
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>

<appender name="fx" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_FX.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">            
        <param name="levelMin" value="DEBUG"/>
        <param name="levelMax" value="FATAL"/>
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>

<appender name="tuxedo" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_TUXEDO.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">            
        <param name="levelMin" value="DEBUG"/>
        <param name="levelMax" value="FATAL"/>
    </filter>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c | %L | %m%n" />
    </layout>
</appender>

<appender name="arbor2010" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${REP_LOG_OPE}/CA_ARBOR2010.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="80000KB"/>
    <param name="MaxBackupIndex" value="2000"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">            
        <param name="levelMin" value="DEBUG"/>
        <param name="levelMax" value="FATAL"/>
    </filter>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yy - HH:mm:ss,SSS} | %-1p | %c{1} | %L | %X{ID} | %m%n" />
    </layout>
</appender>

<logger name="com.ours.theirs.arbor2010" additivity="false">
    <appender-ref ref="arbor2010"/>
</logger>

<logger name="com.ours.theirs.mvno2012" additivity="false">
    <appender-ref ref="mvno2012"/>
</logger>

<logger name="com.csgsystems" additivity="false">
    <appender-ref ref="aruba"/>
</logger>

<logger name="terrapin" additivity="false">
    <appender-ref ref="tuxedo"/>
</logger>

<logger name="com.csgsystems.fx" additivity="false">
    <appender-ref ref="fx"/>
</logger>

<logger name="com.ours.collecte" additivity="true">
    <appender-ref ref="engine_collecte"/>
</logger>

<logger name="com.ours.common" additivity="false">
    <appender-ref ref="engine_collecte"/>
</logger>

<logger name="org.apache" additivity="false">
    <appender-ref ref="apache_utilities"/>
</logger>

<root>
    <level value="ERROR"/>
    <appender-ref ref="all"/>
</root>

编辑2:

我仍然没有找到发生的事情,但这是我迄今为止所发现和完成的事情:

1)log4j.xml文件格式错误:它缺少DOCTYPE,有多个布局,一些布局类不正确。
2)我在命令行中指定了log4j.xml文件的路径 3)有几次调用DOMConfigurator和PropertyConfigurator,我已经删除了它们。

我已将研究范围缩小到一个基于线程ID创建动态日志文件的类。在执行该类中的代码之前,log4j控制台日志清楚地表明日志级别为ERROR。创建动态日志文件的那一刻,日志级别切换到DEBUG。

答案就在附近。

编辑3

我找到了罪魁祸首!

第三方组件在我们的上面加载自己的log4j配置。它添加了appender,它改变了根日志记录级别。

值得注意的是,当加载新文件时,log4j似乎不会将该信息调试到控制台。

1 个答案:

答案 0 :(得分:0)

根级别由第三方组件更改,该组件加载特定配置。

遇到这样的问题时:

  1. 检查您的代码是否包含任何额外的log4j动态配置加载(DOMConfiguratorPropertyConfigurator
  2. 检查是否有任何第三方组件未加载任何额外的log4j配置
  3. 确保您的log4j.xml(或log4j.properties)有效。