建立logproperties log level定為INFO
不顯示DEBUG信息
log輸出依次為文件
控制台
log
j
rootLogger=INFO
R
CONSOLE#DEBUG
CONSO
apache=true #################### Console Appender###################log
j
appender
CONSOLE=org
apache
log
j
ConsoleAppenderlog
j
appender
Threshold=DEBUGlog
j
appender
CONSOLE
Target=System
outlog
j
appender
CONSOLE
layout=org
apache
log
j
PatternLayoutlog
j
appender
CONSOLE
layout
ConversionPattern=[framework] %d
%c
%
r [%t] %
p %c %x
%m%n###################### File Appender
文件滿
kb
自動生成file
log
n#####################log
j
appender
R=org
apache
log
j
RollingFileAppenderlog
j
appender
R
File=d:\\file
loglog
j
appender
R
MaxFileSize=
KB# Keep one backup filelog
j
appender
R
MaxBackupIndex=
log
j
appender
R
layout=org
apache
log
j
PatternLayoutlog
j
appender
R
layout
ConversionPattern=%p %t %c
%m%n
建立初始化Action Servlet import nstants
*;import org
apache
log
j
*;public class StartupServlet extends Action { public ActionForward execute(ActionMapping mapping
ActionForm form
HttpServletRequest request
HttpServletResponse response) throws Exception{ try{//初始化log initLog(); }catch(Exception e){ e
printStackTrace(); return mapping
findForward(
failure
); } Logger logger = Logger
getLogger(this
getClass()); (
init log
); } private void initLog() throws Exception{//獲得log
properties絕對地址
URL myfile=this
getClass()
getResource(
/resources/log/properties
);//加載log配置文件log
properties if(myfile
getPath()!=null) Propnfigure(myfile
getPath()); else throw new Exception(
no log configure
); }}
寫log ublic class LoginAction extends Action { LoginContext loginContext=null; LoginForm loginForm=null; public ActionForward execute(ActionMapping mapping
ActionForm form
HttpServletRequest request
HttpServletResponse response) throws Exception{ Logger logger = Logger
getLogger(this
getClass()); (
login success
); return mapping
findForward(
success
); }}
log文件內容如下
INFO cessor
limas
web
action
StartupServlet
init security successfullyINFO
Processor
limas
web
action
LoginAction
login success
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28528.html