如何使用log4j.xml在stuts2中创建日志文件,并为不同的级别和不同的Web项目创建单独的日志文件

时间:2015-06-18 10:31:26

标签: java xml jboss struts2 log4j

我试图构建一些东西,但它将数据存储在一个文件中,并且仅在服务器启动时才传递一些参数。

创建的文件与server.log文件相同。还有一件事是我使用jboss server。并且xml文件位于/home/stpl/Desktop/jboss-4.2.2.GA_new/server/default/conf folder下。

这是我的xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">


<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

   <appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
        <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
        <param name="File" value="${jboss.server.log.dir}/server.log"/>
        <param name="Append" value="false"/>
        <param name="DatePattern" value="'.'yyyy-MM-dd"/>
        <layout class="org.apache.log4j.PatternLayout">
            <!-- The default pattern: Date Priority [Category] Message\n -->
            <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
        </layout>
   </appender>
   <appender name="LMS_LOG" class="org.jboss.logging.appender.DailyRollingFileAppender">
        <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
        <param name="File" value="${jboss.server.log.dir}/LMS/lms.log"/>
     <param name="Threshold" value="DEBUG"/>
     <param name="Append" value="true"/>
     <!-- Rollover at midnight each day --> 
        <param name="DatePattern" value="'_'yyyy-MM-dd"/>
        <layout class="org.apache.log4j.PatternLayout">         
             <param name="ConversionPattern" value="%d   %5p   [%c] %t  %x %m%n"/>     
        </layout>
   </appender>
   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
        <param name="Target" value="System.out"/>
        <param name="Threshold" value="INFO"/>
        <layout class="org.apache.log4j.PatternLayout">
            <!-- The default pattern: Date Priority [Category] Message\n -->
             <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
        </layout>
   </appender>
  <appender name="FTP" class="org.jboss.logging.appender.DailyRollingFileAppender">
        <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
        <param name="File" value="${jboss.server.log.dir}/EMS/ftp.log"/>
    <param name="Append" value="true"/>
    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
        <layout class="org.apache.log4j.PatternLayout">         
            <param name="ConversionPattern" value="%d   %5p   [%c] %t  %x %m%n"/>     
        </layout>
         <filter class="org.apache.log4j.filter.LevelRangeFilter">
                <param name="LevelMin" value="INFO"/>
                <param name="LevelMax" value="FATAL"/>
        </filter>
  </appender>

   <!-- Limit the org.apache category to INFO as its DEBUG is verbose -->
   <category name="org.apache">
      <priority value="INFO"/>
   </category>

   <!-- Limit the jacorb category to WARN as its INFO is verbose -->
   <category name="jacorb">
      <priority value="WARN"/>
   </category>

   <!-- Limit the org.jgroups category to WARN as its INFO is verbose -->
   <category name="org.jgroups">
      <priority value="WARN"/>
   </category>

   <!-- Limit the org.quartz category to INFO as its DEBUG is verbose -->
   <category name="org.quartz">
      <priority value="INFO"/>
   </category>

   <category name="org.jboss.management">
      <priority value="INFO"/>
   </category>

   <category name="org.jboss.serial">
      <priority value="INFO"/>
   </category>

   <category name="com.skilrock" additivity="false">
     <priority value="DEBUG"/>
     <appender-ref ref="LMS_LOG"/>  
     <appender-ref ref="CONSOLE"/> 
     <appender-ref ref="FILE"/>
    </category>

     <root>
        <appender-ref ref="FTP"/>
     </root>
</log4j:configuration>

1 个答案:

答案 0 :(得分:0)

我通过在jboss / server / conf文件夹中的server.log文件中编写日志配置来完成此操作。

日志文件由级别分隔。以下是我的xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<!-- ===================================================================== -->
<!--                                                                       -->
<!--  Log4j Configuration                                                  -->
<!--                                                                       -->
<!-- ===================================================================== -->

<!-- $Id: jboss-log4j.xml 65459 2007-09-19 00:25:51Z dimitris@jboss.org $ -->

<!--
   | For more configuration infromation and examples see the Jakarta Log4j
   | owebsite: http://jakarta.apache.org/log4j
 -->

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

   <!-- ================================= -->
   <!-- Preserve messages in a local file -->
   <!-- ================================= -->

   <!-- A time/date based rolling appender -->
   <appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="File" value="${jboss.server.log.dir}/server.log"/>
      <param name="Append" value="false"/>

      <!-- Rollover at midnight each day -->
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>

      <!-- Rollover at the top of each hour
      <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
      -->

      <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>

         <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
         <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
          -->
      </layout>
   </appender>

   <!-- ============================== -->
   <!-- Append messages to the console -->
   <!-- ============================== -->

   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="Target" value="System.out"/>
         <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

<appender name="FTP" class="org.jboss.logging.appender.DailyRollingFileAppender">
        <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <param name="Threshold" value="DEBUG"/> 
        <param name="File" value="${jboss.server.log.dir}/EMS/Debug.log"/>
    <param name="Append" value="true"/>
    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
        <layout class="org.apache.log4j.PatternLayout">         
            <param name="ConversionPattern" value="%d   %5p   [%c] %t  %x %m%n"/>     
        </layout>
  </appender>

 <appender name="errorAppender" class="org.apache.log4j.FileAppender"> 
        <param name="File" value="${jboss.server.log.dir}/EMS/error.log"/> 
        <param name="Threshold" value="error"/> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1} : %m%n"/> 
        </layout> 
  </appender> 


   <!-- Limit the org.apache category to INFO as its DEBUG is verbose -->
   <category name="org.apache">
      <priority value="INFO"/>
   </category>

   <!-- Limit the jacorb category to WARN as its INFO is verbose -->
   <category name="jacorb">
      <priority value="WARN"/>
   </category>

   <!-- Limit the org.jgroups category to WARN as its INFO is verbose -->
   <category name="org.jgroups">
      <priority value="WARN"/>
   </category>

   <!-- Limit the org.quartz category to INFO as its DEBUG is verbose -->
   <category name="org.quartz">
      <priority value="INFO"/>
   </category>

   <category name="org.jboss.management">
      <priority value="INFO"/>
   </category>

   <category name="org.jboss.serial">
      <priority value="INFO"/>
   </category>


      <root>
    <appender-ref ref="FTP"/>
    <appender-ref ref="errorAppender"/>
    </root>

</log4j:configuration>