公司启动全国支票影像系统,有这样一个功能是和消息中间件打交道,需要记录每一个报文出错情况,因为方便后期开发人员的调试,我选用了log4j作为日志记录,好了废话不多说了!
第一步:首先建立一个WEB工程,去log4j官网下log4j的JAR包导入到工程的lib目录下
第二步:在src目录下建一个log4j.properties 文件,文件命名可以由自己,只是记加载时候和这里名字一致就行;
log4j.properties 里边的内容如下:
### set log levels ###
log4j.rootLogger = debug,stdout,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 = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.E= org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =${catalina.home}/logs/log_
log4j.appender.E.DatePattern=yyyy-MM-dd'.log'
log4j.appender.E.Threshold =DEBUG
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss}[%c] [%t:%r] - [%p] %m%n
解释下几个常用的,其它的在详细的,请自己去网上查找资料:
stdout是一个日志记录配置,它的意思是把日志输出到控制台上,也就是这个配置属性--->log4j.appender.stdout.Target = System.out ,到时候到了生产上可以注解掉就可以了。
E是一个日志记录配置,它是把日志文件记录到文件中去,log4j.appender.E.File =${catalina.home}/logs/log_ 是放日志文件的路径,其中${catalina.home}是容器的路径
log4j.appender.E= org.apache.log4j.DailyRollingFileAppender 这个属性配置是一天产生一个新的日志文件(还有其它配置方式,这个是用的最多的)
log4j.appender.E.DatePattern=yyyy-MM-dd'.log' 这个属性是以时间为名字的文件日志,形式如下:
log_2011-06-26.log,log_2011-06-28.log,不过第一个生成的日志文件的名字是:log_
log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss}[%c] [%t:%r] - [%p] %m%n 这个是输出日志内容格式,详细情况请自己查阅资料;
log4j.appender.E.Threshold =DEBUG 这个是输出日志级别(意思是只输出比DEBUG级别高的日志信息);
第三步:配置容器起动时候加载log4j配置文件:
建立一个servlet内容如下:
package com.tchzt.logTest;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;
public class Log4jInit extends HttpServlet {
private static final long serialVersionUID = 1L;
public void destroy() {
super.destroy();
}
public Log4jInit() {
super();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
String file =this.getInitParameter("log4j");//从web.xml配置读取,名字一定要和web.xml配置一致
if(file != null){
PropertyConfigurator.configure(file);
}
}
}
web.xml配置:
<servlet>
<servlet-name>Log4jInit</servlet-name>
<servlet-class>com.tchzt.logTest.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>//这个是名字是下边路径配置的标识(好像KEY一样)
<param-value>/WEB-INF/classes/log4j.properties</param-value>//这是容器初始化时候加载log4j配置文件的路径(这好像一个value);
</init-param>
</servlet>
好了基本上配置完了开始下测试了:
测试类:建立一个servlet内容如下:
package com.tchzt.logTest;
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.Logger;
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Logger logger = Logger.getLogger(Test.class.getName());
MyLog4j.getSomething();
System.out.println("================97987==============");
logger.info("日志信息开始!");
logger.info("日志信息结束!");
try {
Integer.parseInt("a");
} catch (NumberFormatException e) {
logger.error("解析数字出现异常",e);
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
this.doGet(request, response);
}
}
这个servlet 的web.xml配置如下:
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.tchzt.logTest.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/servlet/Test</url-pattern>
</servlet-mapping>
启动容器:输出URL:http://localhost:8080/Log4jWebTest/servlet/Test
第二次关闭容器,修改系统时间;在启动容器,最后生成日志文件如下: