1,导入log4j-1.2.15.jar
2,在src下面新建log4j.properties
其内容为
### 设置###
log4j.rootLogger = debug,stdout,D,E
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出DEBUG 级别以上的日志到=E://logger_data/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logger_data/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=E://logger_data/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logger_data/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
3,添加servlet 容器加载时初始化
packagecom.mucfc;importjava.io.File;importjava.io.IOException;importjavax.servlet.ServletConfig;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.log4j.BasicConfigurator;importorg.apache.log4j.PropertyConfigurator;
@WebServlet("/Log4JInitServlet")public class Log4JInitServlet extendsHttpServlet {private static final long serialVersionUID = 1L;publicLog4JInitServlet() {super();
}public void init(ServletConfig config) throwsServletException {
System.out.println("Log4JInitServlet 正在初始化 log4j日志设置信息");
String log4jLocation= config.getInitParameter("log4j-properties-location");
ServletContext sc=config.getServletContext();if (log4jLocation == null) {
System.err.println("*** 没有 log4j-properties-location 初始化的文件, 所以使用 BasicConfigurator初始化");
BasicConfigurator.configure();
}else{
String webAppPath= sc.getRealPath("/");
String log4jProp= webAppPath +log4jLocation;
File yoMamaYesThisSaysYoMama= newFile(log4jProp);if(yoMamaYesThisSaysYoMama.exists()) {
System.out.println("使用: " + log4jProp+"初始化日志设置信息");
PropertyConfigurator.configure(log4jProp);
}else{
System.err.println("*** " + log4jProp + " 文件没有找到, 所以使用 BasicConfigurator初始化");
BasicConfigurator.configure();
}
}super.init(config);
}protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
}protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
}
}
4,添加servlet 测试类
packagecom.mucfc;importjava.io.IOException;importjavax.servlet.ServletConfig;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.log4j.Logger;
@WebServlet("/Log4JTestServlet")public class Log4JTestServlet extendsHttpServlet {private static final long serialVersionUID = 1L;private static Logger logger = Logger.getLogger(Log4JTestServlet.class);publicLog4JTestServlet() {super();
}public void init(ServletConfig config) throwsServletException {
}protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//记录debug级别的信息
logger.debug("This is debug message.");//记录info级别的信息
logger.info("This is info message.");//记录error级别的信息
logger.error("This is error message.");
}protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
doGet(request,response);
}
}
5,修改web.xml
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0">
log4j
Log4JInitServlet
com.mucfc.Log4JInitServlet
log4j-properties-location
/WEB-INF/classes/log4j.properties
1
Log4JTestServlet
/test
Log4JTestServlet
com.mucfc.Log4JTestServlet
6,tomcat跑起来后访问
http://localhost:8089/test
后台输出
磁盘上面生成的日志文件
原文:https://www.cnblogs.com/rdchen/p/13573416.html