寫個初始化的SERVLET package net
welovetennis
util
log;
import javax
servlet
*;
import javax
servlet
http
*;
import org
apache
log
j
*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c)
</p>
* <p>Company: </p>
* @author not attributable
* @version
*/
public class Log
jInit extends HttpServlet {
//Initialize global variables
public void init() throws ServletException {
String prefix = getServletContext()
getRealPath(
/
);
String file = getInitParameter(
log
j
);
// if the log
j
init
file is not set
then no point in trying
System
out
println(
log
j start
);
if(file != null) {
Propnfigure(prefix+file);
}
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request
HttpServletResponse response) {
}
//Clean up resources
public void destroy() {
}
}
修改WEBXML <servlet>
<servlet
name>log
jinit</servlet
name>
<servlet
class>net
welovetennis
util
log
Log
jInit</servlet
class>
<init
param>
<param
name>log
j</param
name>
<param
value>WEB
INF/log
j
properties</param
value>
</init
param>
<load
on
startup>
</load
on
startup>
</servlet>
在WEBINF下放配制文件logjproperties並指定輸出路徑 log
j
rootLogger=INFO
A
R
log
j
appender
A
=org
apache
log
j
ConsoleAppender
log
j
appender
A
layout=org
apache
log
j
PatternLayout
log
j
appender
A
layout
ConversionPattern=%
d{yyyy
MM
dd HH:mm:ss} [%c]
[%p] %m%n
log
j
appender
R=org
apache
log
j
RollingFileAppender
log
j
appender
R
File=c:/project/david/racquet/logs/log
j
log
log
j
appender
R
MaxFileSize=
KB
log
j
appender
R
MaxBackupIndex=
log
j
appender
R
layout=org
apache
log
j
PatternLayout
log
j
appender
R
layout
ConversionPattern=%p %t %c
%m%n
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28858.html