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 ("[User:" + emp.getEmpNumber () + "out of the system]");

4. Look log file info

2010/04/28 01:10:45:031 INFO [com.jtosa.struts.action.EmployeeAction] - [user: test password: test log]
2010/04/28 01:10:47:468 INFO [com.jtosa.struts.action.EmployeeAction] - [User: test Password: test Login failed, reason: user or password is incorrect]
2010/04/28 01:10:53:140 INFO [com.jtosa.struts.action.EmployeeAction] - [user: test password: 123 log]
2010/04/28 01:10:53:156 INFO [com.jtosa.struts.action.EmployeeAction] - [User: test Password: 123 Login successful]
2010/04/28 01:10:59:156 INFO [com.jtosa.struts.action.EmployeeAction] - [user: test out the system]

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

分类:Java 时间:2010-04-28 人气:418
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.478 (s). 12 q(s)