建立logproperties
log level定为INFO不显示DEBUG信息log输出依次为文件控制台
logjrootLogger=INFORCONSOLE#DEBUGCONSOapache=true #################### Console Appender###################logjappenderCONSOLE=orgapachelogjConsoleAppenderlogjappenderThreshold=DEBUGlogjappenderCONSOLETarget=SystemoutlogjappenderCONSOLElayout=orgapachelogjPatternLayoutlogjappenderCONSOLElayoutConversionPattern=[framework] %d %c %r [%t] %p %c %x %m%n###################### File Appender文件满kb自动生成filelogn#####################logjappenderR=orgapachelogjRollingFileAppenderlogjappenderRFile=d:\\fileloglogjappenderRMaxFileSize=KB# Keep one backup filelogjappenderRMaxBackupIndex=logjappenderRlayout=orgapachelogjPatternLayoutlogjappenderRlayoutConversionPattern=%p %t %c %m%n
建立初始化Action Servlet
import nstants*;import orgapachelogj*;public class StartupServlet extends Action { public ActionForward execute(ActionMapping mappingActionForm formHttpServletRequest requestHttpServletResponse response) throws Exception{try{//初始化log initLog();}catch(Exception e){ eprintStackTrace(); return mappingfindForward(failure);}Logger logger = LoggergetLogger(thisgetClass()); (init log);}private void initLog() throws Exception{//获得logproperties绝对地址URL myfile=thisgetClass()getResource(/resources/log/properties);//加载log配置文件logpropertiesif(myfilegetPath()!=null) Propnfigure(myfilegetPath());elsethrow new Exception(no log configure); }}
写log
ublic class LoginAction extends Action {LoginContext loginContext=null; LoginForm loginForm=null; public ActionForward execute(ActionMapping mappingActionForm formHttpServletRequest requestHttpServletResponse response) throws Exception{Logger logger = LoggergetLogger(thisgetClass()); (login success);return mappingfindForward(success); }}
log文件内容如下
INFO cessor limaswebactionStartupServlet init security successfullyINFO Processor limaswebactionLoginAction login success