Log4j配置及应用实例

1、编写log4j配置文件(如log.config):
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, FILE
log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n

# LOGFILE is set to be a File appender using a PatternLayout.
#WORKDIR setted by system.setProperty!
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=${WORKDIR}/log/adapter.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=INFO
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

2、使用LogServlet初始化log4j的配置参数:
package com.cait.adapter.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class LogServlet extends HttpServlet {

 /**
  *
  */
 private static final long serialVersionUID = -1837292475877367091L;

 static Logger logger = Logger.getLogger(LogServlet.class);

 public LogServlet() {
 }

 public void init(ServletConfig sc) throws ServletException {

  String prefix = "";
  if (sc != null) {
   prefix = sc.getServletContext().getRealPath("/");
  } else {
   prefix = "D:/Project_java/Any3Adapter/web";
  }
  String file = sc.getInitParameter("logConfigFile");
  
  if (System.getProperty("WORKDIR") == null) {
   System.setProperty("WORKDIR", prefix);
  }
  
  Properties props = new Properties();
  try {
   FileInputStream istream = new FileInputStream(prefix + file);
   props.load(istream);
   istream.close();
   String logFile = prefix + props.getProperty("log4j.appender.file.File");// 设置路径
   props.setProperty("log4j.appender.file.File", logFile);
   PropertyConfigurator.configure(props);
  }
  catch (IOException e) {
   System.out.println("Could not read configuration file [" + file + "].");
   System.out.println("Ignoring configuration file [" + file + "].");
   return;
  }
 }

}

3、设置web.xml的servlet选项,保证配置被加载
 <servlet>
  <servlet-name>LogServlet</servlet-name>
  <servlet-class>com.cait.adapter.servlet.LogServlet</servlet-class>
  <init-param>
   <param-name>logConfigFile</param-name>
   <param-value>WEB-INF/config/log.config</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

4、测试用例:
....
   Logger log=Logger.getLogger(this.getClass());
   log.info("LogServlet Test:    testOK");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值