LOG4J安装配置实录
具体工作做完了,学习以下Log4j的使用。以前别人配好了我用,现在学着配置一下,并记录下全部过程。
0、首先当然要下载log4j.jar了
下载地址:http://logging.apache.org/log4j/1.2/download.html
我下载的是log4j-1.2.15.jar
1、新建Web Project
工程名:log4j
2、给工程添加Log4j
从0步下载的文件中找到log4j-1.2.15.jar文件,将其添加到工程的lib目录下。这步很简单的,但要确定jar包添加到了工程的构建路径。
3、创建并配置 log4j.properties文件
在src源文件目录下添加log4j.properties文件。
编辑文件内容:
4、配置web.xml
5、创建初始化Log4jInit.java
6、至此,log4j配置成功。在项目中就可以正常引用了。使用方法如下:
注:以上配置的是使用控制台打印日志信息的方式。
例如:上例将在控制台打印如下:
0、首先当然要下载log4j.jar了
下载地址:http://logging.apache.org/log4j/1.2/download.html
我下载的是log4j-1.2.15.jar
1、新建Web Project
工程名:log4j
2、给工程添加Log4j
从0步下载的文件中找到log4j-1.2.15.jar文件,将其添加到工程的lib目录下。这步很简单的,但要确定jar包添加到了工程的构建路径。
3、创建并配置 log4j.properties文件
在src源文件目录下添加log4j.properties文件。
编辑文件内容:
#Console config
#OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL
#log4j.rootLogger=error, stdout, R
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=<%d>{yyyy MMM dd HH:mm:ss,SSS} <%-5p> <%c> <%t> - %m(%F:%M:%L)%n
#log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.R.File=f:\\qc.log
#log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
log4j.rootLogger=DEBUG,CONSOLE
#,A1,im
log4j.addivity.org.apache=true
# 应用于控制台
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=DEBUG
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=f:\\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=f:\\rolling.log
#文件位置,也可以用变量${java.home}、rolling.log
log4j.appender.ROLLING_FILE.Append=true
#true:添加 false:覆盖
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.appender.DATABASE.password=
#log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')
#log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
#log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=SampleMessages.log4j
log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j'
log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout
#自定义Appender
#log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender
#log4j.appender.im.host = mail.cybercorlin.net
#log4j.appender.im.username = username
#log4j.appender.im.password = password
#log4j.appender.im.recipient = corlin@cybercorlin.net
#log4j.appender.im.layout=org.apache.log4j.PatternLayout
#log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL
#log4j.rootLogger=error, stdout, R
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=<%d>{yyyy MMM dd HH:mm:ss,SSS} <%-5p> <%c> <%t> - %m(%F:%M:%L)%n
#log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.R.File=f:\\qc.log
#log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
log4j.rootLogger=DEBUG,CONSOLE
#,A1,im
log4j.addivity.org.apache=true
# 应用于控制台
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=DEBUG
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=f:\\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=f:\\rolling.log
#文件位置,也可以用变量${java.home}、rolling.log
log4j.appender.ROLLING_FILE.Append=true
#true:添加 false:覆盖
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.appender.DATABASE.password=
#log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')
#log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
#log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=SampleMessages.log4j
log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j'
log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout
#自定义Appender
#log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender
#log4j.appender.im.host = mail.cybercorlin.net
#log4j.appender.im.username = username
#log4j.appender.im.password = password
#log4j.appender.im.recipient = corlin@cybercorlin.net
#log4j.appender.im.layout=org.apache.log4j.PatternLayout
#log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
4、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>test.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>test.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
5、创建初始化Log4jInit.java
package test;
import org.apache.log4j.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class Log4jInit extends HttpServlet {
public void init() {
String prefix =getServletContext().getRealPath("/");
String file = getInitParameter("log4j");//配置文件位置
if(file != null) {
PropertyConfigurator.configure(prefix+file);
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
{ }
}
import org.apache.log4j.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class Log4jInit extends HttpServlet {
public void init() {
String prefix =getServletContext().getRealPath("/");
String file = getInitParameter("log4j");//配置文件位置
if(file != null) {
PropertyConfigurator.configure(prefix+file);
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
{ }
}
6、至此,log4j配置成功。在项目中就可以正常引用了。使用方法如下:
package test;
import org.apache.log4j.Logger;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
public class Log4jTest {
private static Logger log = Logger.getLogger(Log4jTest.class);
public void test() {
log.debug("debug");
log.trace("trace");
log.debug("befor say hi");
log.info("info");
log.warn("warn");
log.fatal("fatal");
log.error("error");
}
}
import org.apache.log4j.Logger;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
public class Log4jTest {
private static Logger log = Logger.getLogger(Log4jTest.class);
public void test() {
log.debug("debug");
log.trace("trace");
log.debug("befor say hi");
log.info("info");
log.warn("warn");
log.fatal("fatal");
log.error("error");
}
}
注:以上配置的是使用控制台打印日志信息的方式。
例如:上例将在控制台打印如下:
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] DEBUG test.Log4jTest - debug
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] DEBUG test.Log4jTest - befor say hi
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] INFO test.Log4jTest - info
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] WARN test.Log4jTest - warn
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] FATAL test.Log4jTest - fatal
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] ERROR test.Log4jTest - error
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] DEBUG test.Log4jTest - befor say hi
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] INFO test.Log4jTest - info
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] WARN test.Log4jTest - warn
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] FATAL test.Log4jTest - fatal
[framework] 2008-07-10 09:37:02,203 - test.Log4jTest -0 [http-8081-1] ERROR test.Log4jTest - error