如何配置和使用Log4?
Log4配置和用法
对JAVA WEB 应用来说,我们可以有两种方案。一种是配置成 log4j.properties ,一种是配置成 log4j.xml形式。我们这里采用的是log4j.xml形式。现分别叙述如下:
<1>配置成log4j.properties 需要Log4jInit.java初始化Sevlet类,Web.xml,log4j.properties
(1)Log4jInit.java初始化Sevlet类:
public class Log4jInit extends HttpServlet {
public void init() {
//通过web.xml来动态取得配置文件
String prefix = getServletContext().getRealPath("/WEB-INF/class/");
String file = getInitParameter("log4j-init-file");
// 如果没有给出相应的配置文件,则不进行初始化
if(file != null) {
PropertyConfigurator.configure(prefix+file);//(1)
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) {
}
}
(2)Web.xml需要配置这个Servlet。
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>pub.Log4jInit</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
因为log4j的初始化要在所有的log4j调用之前完成,所以在web.xml文件中,我们一定要把对应的Servlet定义的load-on-startup应设为1,以便在Web容器启动时即装入该Servlet。
(3)配置log4j.properties
一个有用的log4j.properties文件模板
##Log4J的配置之简单使它遍及于越来越多的应用中了
##Log4J配置文件实现了输出到控制台、文件、回滚文件、发送日志邮件、输出到数据库日志表、自定义标签等全套功能。择其一二使用就够用了。
##此文件(log4j.properties)内容来自网络,非本文作者原创。
log4j.rootLogger=DEBUG, CONSOLE,A1
log4j.addivity.org.apache=true
# 应用于控制台
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
#应用于文件
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=file.log
log4j.appender.FILE.Append=false
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# Use this layout for LogFactor 5 analysis
# 应用于文件回滚
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold=ERROR
log4j.appender.ROLLING_FILE.File=rolling.log
log4j.appender.ROLLING_FILE.Append=true
log4j.appender.ROLLING_FILE.MaxFileSize=10KB
log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#应用于socket
log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender
log4j.appender.SOCKET.RemoteHost=localhost
log4j.appender.SOCKET.Port=5001
log4j.appender.SOCKET.LocationInfo=true
# Set up for Log Facter 5
log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout
log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n
# Log Factor 5 Appender
log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender
log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
# 发送日志给邮件
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold=FATAL
log4j.appender.MAIL.BufferSize=10
log4j.appender.MAIL.From=web@www.wuset.com
log4j.appender.MAIL.SMTPHost=www.wusetu.com
log4j.appender.MAIL.Subject=Log4J Message
log4j.appender.MAIL.To=web@www.wusetu.com
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# 用于数据库
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
log4j.appender.DATABASE.user=root
|
我们这么采用的是:
#log4j.properties
log4j.rootLogger=debug, A1
#RollingFileAppender
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=d:/log/picchr.log
log4j.appender.A1.MaxFileSize=100KB
log4j.appender.A1.MaxBackupIndex=1
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
#backup need:debug, A1 ,R
#log4j.appender.R=org.apache.log4j.RollingFileAppender
#log4j.appender.R.File=backup.log
#log4j.appender.R.MaxFileSize=100KB
#log4j.appender.R.MaxBackupIndex=1
#log4j.appender.R.layout=org.apache.log4j.TTCCLayout
#ConsoleAppender
log4j.appender.B1=org.apache.log4j.ConsoleAppender
log4j.appender.B1.layout=org.apache.log4j.PatternLayout
log4j.appender.B1.layout.ConversionPattern=%-4r %-5p [%t] %
log4j.appender.B1.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n |
完成了这三个步骤这后,我们就可以象在application开发中一样在webapplication任何地方应用log4j了
其中 下面是在javabean中的应用的一个例子。
importorg.apache.log4j.Logger;
publicclassInfoForm{
staticLoggerlogger=Logger.getLogger(InfoForm.class);
protectedStringtitle;
protectedStringcontent;
publicInfoForm(){}
publicvoidsetTitle(Objectvalue){
logger.debug("nepalon:title="+title);
title=value;
}
publicStringgetTitle(){
logger.debug("nepalon:title="+title);
returntitle;
}
publicvoidsetContent(Stringvalue){
content=value;
logger.debug("nepalon:content()="+content);
}
publicStringgetContent(){
logger.debug("nepalon:content=/n"+content);
returncontent;
}
}
<2>配置成log4j.xml ,不需要Log4jInit.java初始化Sevlet类,Web.xml等
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- Standard Log4j Logging file for Spring Application -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Make sure a copy of what we log goes to the console -->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/> <param name="Threshold" value<span lang="EN-US" style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt; mso- |
本文地址:http://www.45fan.com/a/question/67910.html