标签:
第一步:首先建立一个WEB工程,去log4j官网下log4j的JAR包导入到工程的lib目录下
### 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
可以单指定日志输出到某个文件:
解释下几个常用的,其它的在详细的,请自己去网上查找资料:
log4j.rootLogger = debug,stdout,E 意思是父类logger 会以比debug 级别高的日志输出到 stdout,E 两个地方,子类也一样
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级别高的日志信息);
#appender名字定义
log4j.rootLogger=INFO,FILE,consoleAppender,RollingFile,MAIL,DailyRollingFile,DATABASE
log4j.addivity.org.apache=true
#配置日志信息输出目的地
#log4j.appender.appenderName = fully.qualified.name.of.appender.class
#(如:log4j.appender.RollingFile = org.apache.log4j.RollingFileAppender)
# 1.org.apache.log4j.ConsoleAppender(控制台)
# 2.org.apache.log4j.FileAppender(文件)
# 3.org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
# 4.org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)
# 5.org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
#配置日志信息的格式
#log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
#(如:log4j.appender.RollingFile.layout = org.apache.log4j.HTMLLayout)
# 1.org.apache.log4j.HTMLLayout(以HTML表格形式布局),
# 2.org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
# 3.org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
# 4.org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
#日记记录的优先级:priority,优先级由高到低分为
# OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。
# Log4j建议只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG这五个级别。
# ERROR 为严重错误 主要是程序的错误
# WARN 为一般警告,比如session丢失
# INFO 为一般要显示的信息,比如登录登出
# DEBUG 为程序的调试信息
#输出格式:layout中的参数都以%开始,后面不同的参数代表不同的格式化信息(参数按字母表顺序列出):
# %c 输出所属类的全名,可在修改为 %d{Num} ,Num类名输出的维(如:"org.apache.elathen.ClassName",%C{2}将输出elathen.ClassName)
# %d 输出日志时间其格式为 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss}
# %l 输出日志事件发生位置,包括类目名、发生线程,在代码中的行数
# %n 换行符
# %m 输出代码指定信息,如info(“message”),输出message
# %p 输出优先级,即 FATAL ,ERROR 等
# %r 输出从启动到显示该log信息所耗费的毫秒数
# %t 输出产生该日志事件的线程名
#ConsoleAppender,控制台输出
#FileAppender,文件日志输出
#SMTPAppender,发邮件输出日志
#SocketAppender,Socket日志
#NTEventLogAppender,Window NT日志
#SyslogAppender,
#JMSAppender,
#AsyncAppender,
#NullAppender
第三步:配置容器起动时候加载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 {
//如果直接 className.class 日志输出到全局的 即rootLogger 指定的文件中
Logger logger = Logger.getLogger(Test.class.getName());
//如果指定logger名字,则是把日志,输出到pay-log 指定的日志文件中去
Logger logger = Logger.getLogger(“pay-log”);
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
第二次关闭容器,修改系统时间;在启动容器,最后生成日志文件如下:
log_;log_2011-06-26.log;log_2011-06-28.log;log_2011-06-30.log
内容这里选一个文件里的内容:
2011-06-30 21:59:28[test.jsp] [http-8080-2:0] - [ERROR] asdfasdf
2011-06-30 21:59:28[test.jsp] [http-8080-2:16] - [INFO] Test Logging in JSP
2011-06-30 21:59:28[test.jsp] [http-8080-2:16] - [FATAL] Test fatal logging in jsp page.
2011-06-30 21:59:28[test.jsp] [http-8080-2:16] - [ERROR] 解析数字出现异常
java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:90)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
2011-06-30 21:59:37[com.tchzt.logTest.MyLog4j] [http-8080-2:8641] - [ERROR] java.io.FileNotFoundException: F:\tt.txt (系统找不到指定的文件。)
2011-06-30 21:59:37[com.tchzt.logTest.Test] [http-8080-2:8656] - [INFO] 日志信息开始!
2011-06-30 21:59:37[com.tchzt.logTest.Test] [http-8080-2:8656] - [INFO] 日志信息结束!
2011-06-30 21:59:37[com.tchzt.logTest.Test] [http-8080-2:8656] - [ERROR] 解析数字出现异常
java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.tchzt.logTest.Test.doGet(Test.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u012230055/article/details/46698565