捕风之巢

统计

留言簿(3)

java友情链接

阅读排行榜

评论排行榜

spring 对Log4j的增强!

 Spring最擅长的,就是在别家的蛋糕上再加些cream,让你J2EE without Spring的时候心痒痒。

log4j,可以有如下的cream:
1. 动态的改变记录级别和策略,不需要重启Web应用,如《Effective Enterprise Java》所说。
2. 把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径。
3. 可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。

在web.xml 添加

<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>

在上文的配置里,
Log4jConfigListener会去WEB-INF/log4j.propeties 读取配置文件;
开一条watchdog线程每60秒扫描一下配置文件的变化;
并把web目录的路径压入一个叫webapp.root的系统变量。

然后,在log4j.properties 里就可以这样定义logfile位置


log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log

如果有多个web应用,怕webapp.root变量重复,可以在context-param里定义webAppRootKey。

在jboss中启动时出错,
2006-03-22 00:58:38,983 INFO [STDOUT] log4j:ERROR A "org.jboss.logging.util.OnlyOnceErrorHandler" object is not assignable to a "org.apache.log4j.spi.ErrorHandler" variable.
2006-03-22 00:58:38,983 INFO [STDOUT] log4j:ERROR The class "org.apache.log4j.spi.ErrorHandler" was loaded by
2006-03-22 00:58:38,983 INFO [STDOUT] log4j:ERROR [WebappClassLoader
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
java.net.FactoryURLClassLoader@40f7a8
] whereas object of type
2006-03-22 00:58:38,983 INFO [STDOUT] log4j:ERROR "org.jboss.logging.util.OnlyOnceErrorHandler" was loaded by [org.jboss.system.server.NoAnnotationURLClassLoader@120a47e].
2006-03-22 00:58:39,133 INFO [STDOUT] log4j:ERROR Could not create an Appender. Reported error follows.
2006-03-22 00:58:39,133 INFO [STDOUT] java.lang.ClassCastException


我log4j.properties如下:
log4j.rootLogger=DEBUG,stdout,R
#log4j.rootLogger=DEBUG,R

#--------------------stdout--------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %c - %m%n

#--------------------R--------------------------------
#log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
#this log file will be stored in web server's /bin directory,modify to your path which want to store.
log4j.appender.R.File=${webapp.root}/WEB-INF/logs/MeetPortal.log
#log4j.appender.R.datePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.R.datePattern='.'yyyy-MM-dd
log4j.appender.R.append=true
## Keep one backup file
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %c - %m%n
#[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
按各位高手帮我看哈,谢谢谢!!!!!!!!!!!!!!!!!!!!!!!!111
===============================================
The error above is caused by jboss problem, see
http://jira.atlassian.com/browse/JRA-6823.
"

One way to solve the classloader issue is by reverting the JBoss configuration
to the old classloading model.
Edit the file
<jboss>/server/default/jbossweb-tomcat55.sar/META-INF/jboss-service.xml
Change the following lines:
<attribute name="Java2ClassLoadingCompliance">true</attribute>
<attribute name="UseJBossWebLoader">true</attribute>
(both are default set to false in jboss-4.0.2).

posted on 2006-10-25 08:53 捕风 阅读(1340) 评论(0)  编辑  收藏 所属分类: web开发


只有注册用户登录后才能发表评论。


网站导航: