在JBOSS4.0下,配用自己的LOG4J.PROPETIES

1. 把 D:\jboss-4.0.5.GA\server\default\conf 里面的 log4j.xml 改名为 jboss-log4j.xml

2. 编辑 D:\jboss-4.0.5.GA\server\default\conf 里面的 jboss-service.xml 找到

   <!-- ==================================================================== -->
   <!-- Log4j Initialization                                                 -->
   <!-- ==================================================================== -->

   <mbean code="org.jboss.logging.Log4jService"
      name="jboss.system:type=Log4jService,service=Logging"
      xmbean-dd="resource:xmdesc/Log4jService-xmbean.xml">
      <attribute name="ConfigurationURL">resource:log4j.xml</attribute>
      <!-- Set the org.apache.log4j.helpers.LogLog.setQuiteMode. As of log4j1.2.8
      this needs to be set to avoid a possible deadlock on exception at the
      appender level. See bug#696819.
      -->
      <attribute name="Log4jQuietMode">true</attribute>
      <!-- How frequently in seconds the ConfigurationURL is checked for changes -->
      <attribute name="RefreshPeriod">60</attribute>
   </mbean>
把 resource:log4j.xml 改为 resource:jboss-log4j.xml

3. 在自己的应用程序的 WEB-INF 目录中添加一个 jboss-web.xml 内容如下

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
  <class-loading java2ClassLoadingCompliance="false">
  <loader-repository>
      ConfigTest:loader=ConfigTest.war
      <loader-repository-config>java2ParentDelegation=false
      </loader-repository-config>
   </loader-repository>
</class-loading>
</jboss-web>

4. 把 log4j.jar 加到 build-path 中

5. 在自己的应用程序的 WEB-INF 目录中添加一个 log4j.properties 内容如下:

#Global variable definition
LOG_LEVEL=DEBUG
LOG_FILE1=D:/jboss-4.0.5.GA/server/default/log/configtest1.log
LOG_FILE2=D:/jboss-4.0.5.GA/server/default/log/configtest2.log

# com.microsoft Logger
log4j.logger.org.stephencat.test=${LOG_LEVEL}, logfile1
log4j.additivity.org.stephencat.test=false

# com.microsoft Logger
log4j.logger.org.stephencat.cat=${LOG_LEVEL}, logfile2
log4j.additivity.org.stephencat.cat=false

# Console Appender Definition
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %c(%t) %x:%m%n
#log4j.appender.stdout.layout.ConversionPattern=%-5p:[%d{MM/dd HH:mm:ss,SSS}] %c(%t) %x:%m%n

# File Appender Definition
log4j.appender.logfile1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile1.File=${LOG_FILE1}
log4j.appender.logfile1.DatePattern='.'yyyy-MM-dd
log4j.appender.logfile1.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile1.layout.ConversionPattern=%-5p:[%d{ISO8601}] %c(%t) %x:%m%n

# File Appender Definition
log4j.appender.logfile2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile2.File=${LOG_FILE2}
log4j.appender.logfile2.DatePattern='.'yyyy-MM-dd
log4j.appender.logfile2.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile2.layout.ConversionPattern=%-5p:[%d{ISO8601}] %c(%t) %x:%m%n
这里分别定义了两个命名控件:
logfile1 对应命名空间(Java 代码中的 package)org.stephencat.test.*
logfile2 对应命名空间 org.stephencat.cat.*

6. 建立一个用于初始化的 InitServlet ,配置如下方法:

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occure
     */
    public void init(ServletConfig config) throws ServletException {
        // Put your code here
        String isRelativePosition = config.getInitParameter("relativePosition");
        String logConfiguration = config.getInitParameter("logConfiguration");
        String root = "";
        if(isRelativePosition.equals("true")){
            root = config.getServletContext().getRealPath("/");
        }
        PropertyConfigurator.configure(root + logConfiguration);
    }
在 web.xml 配置这个 Servlet 的初始化参数(声明 log4j.properties 的位置)和启动优先级:

  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>InitServlet</servlet-name>
    <servlet-class>org.stephencat.test.InitServlet</servlet-class>
    <init-param>
        <param-name>relativePosition</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>logConfiguration</param-name>
        <param-value>WEB-INF/log4j.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
7. 在 org.stephencat.test.TestServlet (映射为 /test)中加入:

    static Logger log = Logger.getLogger(TestServlet.class.getName());
8. 在 org.stephencat.cat.Test2Servlet(映射为 /test2)中加入:

    static Logger log = Logger.getLogger(Test2Servlet.class.getName());
9. 重新启动 JBoss ,会出现如下错误消息:

11:53:21,549 ERROR [STDERR] log4j:ERROR A "org.jboss.logging.appender.FileAppend
er" object is not assignable to a "org.apache.log4j.Appender" variable.
11:53:21,549 ERROR [STDERR] log4j:ERROR The class "org.apache.log4j.Appender" wa
s loaded by
11:53:21,549 ERROR [STDERR] log4j:ERROR [WebappClassLoader
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
java.net.FactoryURLClassLoader@dcc4e2
] whereas object of type
11:53:21,549 ERROR [STDERR] log4j:ERROR "org.jboss.logging.appender.FileAppender
" was loaded by [org.jboss.system.server.NoAnnotationURLClassLoader@a97b0b].
11:53:21,549 ERROR [STDERR] log4j:ERROR Could not instantiate appender named "FI
LE".
这是因为自己的 log4j.jar 与 JBoss 的 log4jService 有点冲突,但这段错误消息实际上可以忽略

10. 访问以下网址:

http://localhost/..../test

http://localhost/..../test2

将分别在以下日志文件中输出日志内容:

D:/jboss-4.0.5.GA/server/default/log/configtest1.log
D:/jboss-4.0.5.GA/server/default/log/configtest2.log

文件名和物理路径可在 log4j.properties 中修改。

引用自:http://www.cnblogs.com/stephencat/archive/2006/12/04/581348.html

你可能感兴趣的:(apache,log4j,xml,Web,jboss)