web project use log4j output log information

1. set log4j properties file

 

# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout ,logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss:SSS} %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
#log4j.appender.logfile.File=${TOMCAT_HOME}/webapps/jtosa/WebRoot/logs/jtosaLog.log
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/jtosaLog.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep five backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss:SSS} %p [%c] - %m%n


log4j.category.org.apache.commons.dbcp=INFO
log4j.category.com.ibatis=INFO
log4j.category.org.springframework=INFO
log4j.category.com.jtosa=INFO


2. config project web.xml

 

    <!-- log4j configure -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>WEB-INF/log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
        </listener-class>
    </listener>

 

3. test


    private Logger logger = Logger.getLogger(EmployeeAction.class);

   logger.info("[用户: " + emp.getEmpNumber() + " 退出系统]");

 

4. look log file info

 

2010/04/28 01:10:45:031 INFO [com.jtosa.struts.action.EmployeeAction] - [用户: test 使用密码: test进行登录]
2010/04/28 01:10:47:468 INFO [com.jtosa.struts.action.EmployeeAction] - [用户: test 密码: test 登录失败,原因: 用户或密码有误]
2010/04/28 01:10:53:140 INFO [com.jtosa.struts.action.EmployeeAction] - [用户: test 使用密码: 123 进行登录]
2010/04/28 01:10:53:156 INFO [com.jtosa.struts.action.EmployeeAction] - [用户: test 密码: 123登录成功]
2010/04/28 01:10:59:156 INFO [com.jtosa.struts.action.EmployeeAction] - [用户: test 退出系统]

 

remark : project use framework(spring + ibatis + struts2 + Extjs)

 

 

你可能感兴趣的:(apache,Web,log4j,struts,ibatis)