还得按套路来,继续写个人白开水博客java
第一步:引入jar包web
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</version> </dependency>
若是我告诉你:我在个人controller或是Service里面直接sql
private static Logger logger = Logger.getLogger(Test.class);
你是否是就不往下看了?然而并非,仍是要有点儿干货的。apache
第二步:在web.xml中配置一个Servlettomcat
<servlet> <servlet-name>log4j-init</servlet-name> <servlet-class>com.common.util.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>3</load-on-startup> </servlet>
下面详细讲一下这个配置中的内容mybatis
一、贴出Log4jInit的源码app
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); } } }
二、关于log4j.properties的位置,这个可能要根据你本身项目的位置来微调,同时你采用的服务有关,tomcat和jboss是不同的,其余的没试过ide
<param-value>/WEB-INF/classes/log4j.properties</param-value>
三、如今应该贴一下log4j.properties中的内容了函数
################ OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL log4j.rootLogger=ERROR, stdout, D ### \u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.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\u503C log4j.appender.D=org.apache.log4j.RollingFileAppender #log4j.appender.D=org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File=${catalina.home}/logs/log.log log4j.appender.D.MaxFileSize=1MB # Keep one backup file log4j.appender.D.MaxBackupIndex=100 log4j.appender.D.layout=org.apache.log4j.PatternLayout log4j.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\u4E3ADEBUG log4j.logger.com.ibatis=ERROR log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=ERROR log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
其中log4j.rootLogger对应的日志级别debug/info/error的级别我就不叨叨了。叨叨一下这个this
private static Logger logger = Logger.getLogger(Test.class); logger.error(">>----"+entry.getKey().substring(1)+"********失败!!!");
记录日志尽可能写logger.error,若是你写了logger.info在debug的模式下,日志多是打不出来了,若是你非要较真,确定是要重写log4j.properties的。
四、普及一下web.xml配置中的这句话
<load-on-startup>3</load-on-startup>
我特地查了一下,官方的解释应该以下:
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表明的是优先级,而非启动延迟时间。
五、下班啦。。。