log4j 使用方法

引用:http://zhidao.baidu.com/question/155329037.html

1.将log4j-1.2.14.jar加入你的项目中;
2.在src/下创建log4j.properties|log4j.xml文件;
3.在web.xml中配置log4j的信息,如下:
<context-param> 
  <param-name>log4jConfigLocation</param-name> 
  <param-value>/WEB-INF/classes/log4j.properties</param-value> 
   </context-param> 
 <listener> 
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
 </listener>
4.在项目webroot下创建你想要保存日志文件的文件夹及文件,如webroot/logs/web_app.log;
具体log4j.properties文件,给你一个示例吧,如下:
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 %p [%c] - %m%n
log4j.appender.stdout.layout.ConversionPattern=- %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${webapp.root}/logs/webapp.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three 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 %p [%c] - %m%n

log4j.logger.com.opensymphony.xwork2=ERROR 

# Control logging for other open source packages
log4j.logger.org.springframework=ERROR
log4j.logger.org.quartz=ERROR
log4j.logger.net.sf.ehcache=ERROR
log4j.logger.net.sf.navigator=ERROR
log4j.logger.org.apache.commons=ERROR
log4j.logger.org.apache.struts=ERROR

# Struts OgnlUtil issues unimportant warnings 
log4j.logger.com.opensymphony.xwork2.util.OgnlUtil=error 
log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack=error 
具体写法可根据自己的项目进行配置。
-----------------------------------------------------------------
引用:http://zhidao.baidu.com/question/171239533.html
log4j.rootLogger=WARN,C,F

log4j.appender.C=org.apache.log4j.ConsoleAppender 
log4j.appender.C.Target=System.out
log4j.appender.C.Threshold=DEBUG
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%p] - %m%n


log4j.appender.F=org.apache.log4j.DailyRollingFileAppender
log4j.appender.F.Append=true 
log4j.appender.F.File=E\:\\cybermall\\back\\back-
log4j.appender.F.DatePattern=yyyy-MM-dd'.txt'
log4j.appender.F.Threshold=WARN
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%p] - %m%n

复制以上内容,保存为log4j.properties,然后将其放在在项目 src目录下,再下载一个log4j的jar包 ,拷贝到web-inf/lib 下

其中:log4j.appender.F.File=E\:\\cybermall\\back\\back- 表示存放日志的路径和文件
log4j.appender.F.DatePattern=yyyy-MM-dd'.txt'表示每天生成一个文件
posted @ 2012-05-15 17:58  镇水古月  阅读(325)  评论(0编辑  收藏  举报