Websphere部署项目使用Log4j指定Log信息存放路径

今天部署一个JSF项目到Websphere上,因为本机开发都是用Tomcat,所以Log4j.properties文件信息如下:

  1. #
  2. # $Revision:1.2 $
  3. # $Date:2010/07/19
    03:29:48 $
  4. #
  5. log4j.rootLogger=INFO, DefLog
  6.  
  7. #catalina.home 为读取系统环境变量的值
  8. log4j.appender.DefLog=org.apache.log4j.RollingFileAppender
  9. log4j.appender.DefLog.File=${catalina.home}/logs/flow.log
  10. log4j.appender.DefLog.DatePattern=‘.’yyyy-MM-dd‘.log’
  11. log4j.appender.DefLog.maxBackupIndex=10
  12. log4j.appender.DefLog.maxFileSize=10MB
  13. log4j.appender.DefLog.Append=true
  14. log4j.appender.DefLog.layout=org.apache.log4j.PatternLayout
  15. log4j.appender.DefLog.layout.ConversionPattern=%5p [%t] [%d] (%c) – %m%n

然后用Ant打包项目生成war档部署在Webphere上,输入地址访问报500内部错误,然后查找log信息,费了好大的劲在以下目录下找到了log档:

(安装目录)\IBM\WebSphere\AppServer\profiles\AppSrv01\logs\server1 SystemOut.log

路径比较难找,所以为了方便起见,我想指定简单的log信息路径如:D:\flow.log,该怎么办呢?

1. 先改动Log4j的配置

  1. #
  2. # $Revision:1.2 $
  3. # $Date:2010/07/19
    03:29:48 $
  4. #
  5. log4j.rootLogger=INFO, DefLog
  6.  
  7. log4j.appender.DefLog=org.apache.log4j.RollingFileAppender
  8. log4j.appender.DefLog.File=D:/logs/flow.log
  9. log4j.appender.DefLog.DatePattern=‘.’yyyy-MM-dd‘.log’
  10. log4j.appender.DefLog.maxBackupIndex=10
  11. log4j.appender.DefLog.maxFileSize=10MB
  12. log4j.appender.DefLog.Append=true
  13. log4j.appender.DefLog.layout=org.apache.log4j.PatternLayout
  14. log4j.appender.DefLog.layout.ConversionPattern=%5p [%t] [%d] (%c) – %m%n

在WEB-INF所在的路径建立文件夹:META-INF/service,再建立名为org.apache.commons.logging.LogFactory的文件,其内容为

  1. org.apache.commons.logging.impl.LogFactoryImpl

在web.xml文件里添加以下代码:

  1. <listener>
  2. <listener-class>com.system.Log4jInit</listener-class>
  3. </listener>

Log4jInit.java代码如下:

  1. package com.system;
  2.  
  3. import javax.servlet.ServletContextEvent;
  4. import javax.servlet.ServletContextListener;
  5.  
  6. import org.apache.commons.logging.LogFactory;
  7. import org.apache.commons.logging.impl.Log4JLogger;
  8. import org.apache.log4j.PropertyConfigurator;
  9.  
  10. public
    class Log4jInitimplements ServletContextListener {
  11.  
  12. public
    void contextInitialized(ServletContextEvent event) {
  13. String path = event.getServletContext().getRealPath(“WEB-INF/classes/log4j.properties”);
  14. LogFactory.getFactory().setAttribute(“org.apache.commons.logging.Log”, Log4JLogger.class.getName());
  15. PropertyConfigurator.configure(path);
  16.  
  17. // System.out.println(“log properties path : ” + path);
  18. // Log logger = LogFactory.getLog(this.getClass());
  19. // logger.error(“Log4jInit initial………”);
  20. }
  21.  
  22. public
    void contextDestroyed(ServletContextEvent arg0) {
  23. }
  24.  
  25. }

以下文章点击率最高

Loading…


发表评论

电子邮件地址不会被公开。 必填项已用*标注