Application Specific Logging in Jboss 5.1

2019-07-27 06:56发布

问题:

I added following configuration in my jboss AS'S CONFIG directory

Entry in jboss-log4j.xml file

<appender name="YYY" class="org.apache.log4j.FileAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"></errorHandler>
      <param name="Append" value="false"/>
      <param name="File" value="${jboss.server.home.dir}/log/app1.log"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>
  <category name="com.XXX">
  <priority value="INFO" />
     <appender-ref ref="YYY"></appender-ref>
   </category>

I have seen app1.log been created on JBoss start up.I do not have an app specific log4j.properties or log4j.xml file inside my application war file.All I wanted is classes from com.XXX should log to app1.log.But its not happening.

Cant I achieve it with out having a log4j.properties in my application war. My Jboss is configured in such a way that application specific log4j.properties should be override with jboss-log4j.xml file.

Any help appreciated

回答1:

You can definitely do this. In $JBOSS_HOME/server/$PROFILE/conf/jboss-log4j.xml, make the following modifications:-

 <!-- define the appender --> 
 <appender name="YYY" class="org.apache.log4j.RollingFileAppender">
            <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
            <param name="Append" value="true"/>
            <param name="MaxFileSize" value="500KB"/>
            <param name="MaxBackupIndex" value="1"/>
            <param name="File" value="${jboss.server.log.dir}/app1.log"/>

            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern" value="%d{MM/dd/yy HH:mm:ss,SSS} %-5p [%c{1}] %m%n"/>
            </layout>
    </appender>

<!-- add the category for your classes with package name com.XXX for ERROR level -->
   <category name="com.XXX">
         <priority value="ERROR"/>
         <appender-ref ref="YYY"/>
    </category>

You shouldn't even need to restart the JBoss instance. Please note $jboss.server.log.dir ($JBOSS_HOME/server/$PROFILE/log) is passed as a JVM argument in the run.sh or as a command line parameter to run.sh.

If you are not seeing any output then it probably is that your application does not have log4j instantiated correctly or your package name is different or you are hitting the wrong application or simply you are not hitting the log statements in the code execution. Make sure that your class has log4j instantiated correctly & under the correct package name. For instance:

package com.XXX;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static Logger log = Logger.getLogger(MyServlet.class);

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            log.error("My Test Log Message");
}


回答2:

In addition to the configuration you mentioned, you need to make sure your application war does not include any log4j.jar or commons-logging.jar files.