WEB-INF/classes下建立一个文件:log4j.properties,内容如下:
log4j.rootCategory=WARN, A1, R
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# R is the RollingFileAppender that outputs to a rolling log
# file called rolling_log_file.log.
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log4j.log
# Define a pattern layout for the file.
# For more information on conversion characters (i.e. d,p,t,c,l,m,n)
# please see the PatternLayout class of the Log4j API.
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[DATE] %d{DATE}%n[PRIORITY] %p%n[NDC] %x%n[THREAD] %t%n[CATEGORY] %c%n[LOCATION] %l%n[MESSAGE] %m%n%n
# Set the max size of the file and the number of backup files
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
web.xml内容如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>log4j</servlet-name>
<description>no description</description>
<servlet-class>com.apache.jakarta.log4j.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</init-param>
</servlet>
</web-app>
将log4j-1.2.8.jar放到WEB-INF/lib目录下。
即可使用