如何维护多个log4j.xml配置文件

时间:2016-05-02 11:27:36

标签: java xml log4j

如何在一个Web应用程序中维护多个log4j.xml配置文件。

例如: - 在Servlet中: -

 public void init(ServletConfig arg0) throws ServletException {
       PropertyConfigurator.configure("/home/madhavib/Desktop/Nar/Raju/log4j.properties");
 }

在Jsp: -

 public void jspInit() {

      DOMConfigurator.configure("/home/madhavib/Desktop/Nar/log4j.xml"); 
}

在另一个Jsp中: -

public void jspInit() {

     DOMConfigurator.configure("/home/madhavib/Desktop/Nar/log4j.xml"); 
}




**log4j configuration files (loaded in jsp 1):** 

<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <appender name="FileAppender"
  class="org.apache.log4j.FileAppender" >
  <!-- <param name="threshold" value="debug" /> -->
  <param name="File" value="/home/madhavib/apache-tomcat-7.0.42/logs/EntelProject/infojsp333.log" />
  <param name="DatePattern" value="'.'yyyy-MM-dd" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d %-5p [%t] %c{5}- %m%n" />
  </layout>
   <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="debug" />
            <param name="LevelMax" value="warn" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
  <!-- <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> -->
 </appender>




<appender name="console" class="org.apache.log4j.ConsoleAppender">
     <layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" 
    value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
     </layout>
</appender>



 <logger name="org.apache.jsp">
    <appender-ref ref="console"/>
  <appender-ref ref="FileAppender"/>
</logger> 
 <!--<root>

  <appender-ref ref="FileAppender"/>
</root> -->
  <logger name="pe1.com1.nextel1">
   <level value="info"/>
<appender-ref ref="FileAppender"/>


</logger> 

</log4j:configuration>

**log4j confuguration (loaded in jsp 2) :-**

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <appender name="FileAppender"
  class="org.apache.log4j.FileAppender" >
  <!-- <param name="threshold" value="debug" /> -->
  <param name="File" value="/home/madhavib/apache-tomcat-7.0.42/logs/EntelProject/examplejsp.log" />
  <param name="DatePattern" value="'.'yyyy-MM-dd" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d %-5p [%t] %c{5}- %m%n" />
  </layout>
   <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="debug" />
            <param name="LevelMax" value="warn" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
  <!-- <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> -->
 </appender>




<appender name="console" class="org.apache.log4j.ConsoleAppender">
     <layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" 
    value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
     </layout>
</appender>



 <logger name="org.apache.jsp">
    <appender-ref ref="console"/>
  <appender-ref ref="FileAppender"/>
</logger> 

  <logger name="pe.com.nextel">
   <level value="info"/>
<appender-ref ref="FileAppender"/>


</logger> 

</log4j:configuration>

**log4jconfiguration file(loaded in servlet) :-**


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <appender name="FileAppender"
  class="org.apache.log4j.FileAppender" >
  <!-- <param name="threshold" value="debug" /> -->
  <param name="File" value="/home/madhavib/apache-tomcat-7.0.42/logs/EntelProject/examplejsp.log" />
  <param name="DatePattern" value="'.'yyyy-MM-dd" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d %-5p [%t] %c{5}- %m%n" />
  </layout>
   <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="debug" />
            <param name="LevelMax" value="warn" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
  <!-- <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> -->
 </appender>




<appender name="console" class="org.apache.log4j.ConsoleAppender">
     <layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" 
    value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
     </layout>
</appender>



 <logger name="org.apache.jsp">
    <appender-ref ref="console"/>
  <appender-ref ref="FileAppender"/>
</logger> 

  <logger name="pe.com.nextel">
   <level value="info"/>
<appender-ref ref="FileAppender"/>


</logger> 

</log4j:configuration>

如何在一个Web应用程序中维护多个log4j.xml配置文件。

我已经给出了log4j配置信息文件,请查看并告诉我如何使用多个log4j文件进行适当的开发。

1 个答案:

答案 0 :(得分:0)

您可以通过某些方式传递多个log4j配置文件:

  1. 您可以设置系统属性:

    System.setProperty(“ log4j2.configurationFile”,“ log4j2-1.xml,log4j2-2.xml”);

  2. 您可以将配置文件作为“ log4j2.component.properties”放在资源部分下,并按如下所示定义系统属性:

    log4j2.configurationFile = log4j2-1.xml,log4j2-2.xml

请检查fasttext documentation的系统属性