Tomcat:正在使用多个log4j.xml配置

时间:2015-08-24 13:36:50

标签: spring tomcat log4j

我正在尝试使用log4j标准化以便在Tomcat 7中记录一些现有的Web应用程序,但我无法让它完全按照我的意愿去做。

我为每个网络应用设置了log4jConfigLocation上下文参数,web.xml设置了Log4jConfigListener。本身工作正常。但是,如果log4j.xml中还有一个WEB-INF/classes文件,那么 以及 之前的处理目标文件已处理。 (我通过设置log4j.debug属性找到了这个。)

我怀疑这取决于系统各个部分的初始化顺序,但我不希望任何遗留配置文件与新配置文件发生冲突(这会导致混淆和延迟)所以我该如何制作它完全忽略WEB-INF/classes/log4j.xml

WEB-INF/web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
  <listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
  </listener> 
</web-app>

META-INF/context.xml

<Context swallowOutput="true" useNaming="false">
  <Parameter name="log4jConfigLocation" value="META-INF/log4j.xml" override="false"/>
  <Parameter name="log4jExposeWebAppRoot" value="false" override="false"/>
  ...

META-INF/log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <!-- Component appender -->
  <appender name="ATLAS-CORE" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="${catalina.base}/logs/services/atlas-core.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
    </layout>
  </appender>
  ...

WEB-INF/classes/log4j.xml(使用“错误”的日志文件!):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <!-- Component appender -->
  <appender name="ATLAS-CORE" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="${catalina.base}/logs/services/booblyboo.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
    </layout>
  </appender>
  ...

来自Tomcat的STDOUT日志:

2015-08-24 14:17:58 Commons Daemon procrun stdout initialized
log4j: Trying to find [log4j.xml] using context classloader WebappClassLoader
  context: /atlas-core
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@531e3c55
.
log4j: Using URL [file:/C:/Dev/System/Tomcat-Atlas/webapps/atlas-core/WEB-INF/classes/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [com.procserve] additivity to [true].
log4j: Level value for com.procserve is  [info].
log4j: com.procserve level set to INFO
log4j: Class name: [org.apache.log4j.DailyRollingFileAppender]
log4j: Setting property [file] to [C:\Dev\System\Tomcat-Atlas/logs/services/booblyboo.log].
log4j: Setting property [append] to [true].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d [%t] %-5p %c - %m%n].
log4j: setFile called: C:\Dev\System\Tomcat-Atlas/logs/services/booblyboo.log, true
log4j: setFile ended
log4j: Appender [ATLAS-CORE] to be rolled at midnight.
log4j: Adding appender named [ATLAS-CORE] to category [com.procserve].
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.springframework] additivity to [true].
log4j: Level value for org.springframework is  [warn].
log4j: org.springframework level set to WARN
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.hibernate] additivity to [true].
log4j: Level value for org.hibernate is  [warn].
log4j: org.hibernate level set to WARN
log4j: Level value for root is  [warn].
log4j: root level set to WARN
log4j: Adding appender named [ATLAS-CORE] to category [root].
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [com.procserve] additivity to [true].
log4j: Level value for com.procserve is  [info].
log4j: com.procserve level set to INFO
log4j: Class name: [org.apache.log4j.DailyRollingFileAppender]
log4j: Setting property [file] to [C:\Dev\System\Tomcat-Atlas/logs/services/atlas-core.log].
log4j: Setting property [append] to [true].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d [%t] %-5p %c - %m%n].
log4j: setFile called: C:\Dev\System\Tomcat-Atlas/logs/services/atlas-core.log, true
log4j: setFile ended
log4j: Appender [ATLAS-CORE] to be rolled at midnight.
log4j: Adding appender named [ATLAS-CORE] to category [com.procserve].
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.springframework] additivity to [true].
log4j: Level value for org.springframework is  [warn].
log4j: org.springframework level set to WARN
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.hibernate] additivity to [true].
log4j: Level value for org.hibernate is  [warn].
log4j: org.hibernate level set to WARN
log4j: Level value for root is  [warn].
log4j: root level set to WARN
log4j: Adding appender named [ATLAS-CORE] to category [root].

1 个答案:

答案 0 :(得分:0)

大多数答案和一种解决方案。

加载log4j jar文件时,log4j会自动加载

WEB-INF/classes/log4j.xml,如https://logging.apache.org/log4j/1.2/manual.html#defaultInit(即FM)中所述。在网络应用程序开始阅读context.xml之前,或者对阅读内容做任何事情,因此设置log4jConfigLocation无效。

可以通过设置WEB-INF/classes/log4j.xml来自动停止加载log4j.defaultInitOverride。但是,这意味着当Web应用程序框架(本例中为Spring)启动时,没有Log4j设置生效。在处理上下文设置和初始化Log4j之前,Spring开始尝试记录事物。

理论上可以在加载log4j jar文件之前设置log4j.configuration,但我无法正常工作。

最后,我决定将默认日志记录配置放在WEB-INF/classes/log4j.xml中,而不是将其定义为Web应用程序的定义部分&#34; context&#34;。如果需要在部署中调整设置,则可以覆盖Tomcat Catalina上下文:

META-INF/context.xml

<?xml version='1.0' encoding='utf-8'?>
<!-- No schema/DTD can be provided for this file
     (see http://marc.info/?l=tomcat-user&m=104205666221372&w=2) -->

<!-- By default, the atlas-core webapp's context is configured by this file
     The entire file can be overridden by creating a file in
     .../tomcat-installation/conf/catalina/localhost/atlas-core.xml
     (For simplicity, copy this file there as a template) -->

<Context swallowOutput="true" useNaming="false">
    <!-- These parameters are set not be overridden by a context-param in web.xml,
         but see comment above for a different way to override this whole file -->

    <!-- If you override this file to change the logging settings, then you might
         want to uncomment the following line, and copy WEB-INF/classes/log4j.xml
         to .../tomcat-installation/conf/log4j/atlas-core.xml to make changes -->
    <!--
    <Parameter name="log4jConfigLocation" value="file:${catalina.base}conf/log4j/atlas-core.xml" override="false"/>
    -->
    <Parameter name="log4jRefreshInterval" value="600000" override="false"/>
    <Parameter name="log4jExposeWebAppRoot" value="false" override="false"/>
...

这确实意味着默认的log4j设置将在webapp初始化的最初阶段生效很短时间,但我认为这已经足够接近。