【原创】Log4j 日志文件 使用

log4j.properties 代码

log4j.rootLogger=INFO,stdout,R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

#其中log4j提供4种布局
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

#R 输出到文件 RollingFileAppender的扩展,可以提供一种日志的备份功能。
log4j.appender.R=org.apache.log4j.RollingFileAppender


#日志文件的名称
log4j.appender.R.File=${webappHome}logs/wisdomxf.log

#日志文件的大小
log4j.appender.R.MaxFileSize=20KB

#保存一个备份文件
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout

log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n





package com.zwj.tools;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @模块功能:读取log4j 配置文件
 * @UserName 周文俊
 * @Mail [email protected]
 * @Date 2010-11-19
 * @Version V1.0
 */
public class Log4jInit extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(Log4jInit.class);

	public void init() {
		String prefix = getServletContext().getRealPath("/");
		String file = getInitParameter("log4j-init-file");
		System.setProperty("webappHome", prefix);
		if (file != null) {
			PropertyConfigurator.configure(prefix + file);
			
			logger.info(">>>>>>>>>Log4j 日志记录器 已启动..............");
		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res) {
	}
}


web.xml
<!-- log4j日志 -->
  <servlet>
    <servlet-name>log4j-init</servlet-name>
    <servlet-class>com.zwj.tools.Log4jInit</servlet-class>
    <init-param>
      <param-name>log4j-init-file</param-name>
      <param-value>WEB-INF/classes/log4j.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>




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