还得按套路来,继续写我的白开水博客
第一步:引入jar包
commons-logging commons-logging 1.2 log4j log4j 1.2.17 ch.qos.logback logback-classic 1.1.2 org.slf4j jcl-over-slf4j 1.7.5
如果我告诉你:我在我的controller或是Service里面直接
private static Logger logger = Logger.getLogger(Test.class);
你是不是就不往下看了?然而并不是,还是要有点儿干货的。
第二步:在web.xml中配置一个Servlet
log4j-init com.common.util.Log4jInit log4j-init-file /WEB-INF/classes/log4j.properties 3
下面详细讲一下这个配置中的内容
1、贴出Log4jInit的源码
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.PropertyConfigurator;/** * Servlet implementation class Log4jInit */public class Log4jInit extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } @Override public void init() throws ServletException { super.init(); String prefix = getServletContext().getRealPath("/"); String file = getInitParameter("log4j-init-file"); if (file != null) { System.out.println("read log4j.properties:"+prefix + file); PropertyConfigurator.configure(prefix + file); } }}
2、关于log4j.properties的位置,这个可能要根据你自己项目的位置来微调,同时你采用的服务有关,tomcat和jboss是不一样的,其他的没试过
/WEB-INF/classes/log4j.properties
3、现在应该贴一下log4j.properties中的内容了
################ OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALLlog4j.rootLogger=ERROR, stdout, D### \u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout# Pattern to output the caller's file name and line number.log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} method:%l%n%m%n### \u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u6587\u4EF6\uFF1Alog.log ####log4j.appender.C.Threshold=WARN#RollingFileAppender\u4E3A\u5FAA\u73AF\u8986\u76D6\u6A21\u5F0F\uFF0C\u5907\u4EFD\u6587\u4EF6\u4E2A\u6570\u4E3AMaxBackupIndex\u6307\u5B9A\u7684\u503Clog4j.appender.D=org.apache.log4j.RollingFileAppender#log4j.appender.D=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.File=${catalina.home}/logs/log.loglog4j.appender.D.MaxFileSize=1MB# Keep one backup filelog4j.appender.D.MaxBackupIndex=100log4j.appender.D.layout=org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} method\:%l%n%m%n ###\u663E\u793Amybatis SQL\u8BED\u53E5\u90E8\u5206,\u8FD8\u9700\u5C06log4j.rootLogger\u8BBE\u4E3ADEBUGlog4j.logger.com.ibatis=ERRORlog4j.logger.org.apache.ibatis.jdbc.ScriptRunner=ERRORlog4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUG
其中log4j.rootLogger对应的日志级别debug/info/error的级别我就不叨叨了。叨叨一下这个
private static Logger logger = Logger.getLogger(Test.class);logger.error(">>----"+entry.getKey().substring(1)+"********失败!!!");
记录日志尽量写logger.error,如果你写了logger.info在debug的模式下,日志可能是打不出来了,如果你非要较真,肯定是要重写log4j.properties的。
4、普及一下web.xml配置中的这句话
3
我特意查了一下,官方的解释应该如下:
Servlet specification:
The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.-
load-on-startup标记容器是否在启动的时候实例化并调用其init()方法的优先级。
-
它的值表示servlet应该被载入的顺序
-
当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
-
如果值小于0或未指定时,则表示只有在第一次请求的容器才在该servlet调用初始化函数
-
正值越小,servlet的优先级越高,应用启动时就越先加载。
-
值相同时,容器就会自己选择顺序来加载。
所以,<load-on-startup>x</load-on-startup>,中x的取值1,2,3,4,5代表的是优先级,而非启动延迟时间。
5、下班啦。。。