web.xml中关于log4j的设置及范例说明 以及关于webapp.root

log4j.appender.ROLLING_FILE.File=${webapp.root}../../logs/zteits22.log日志路径在

 

 

在web.xml有几个条目和log4j有关,它们是: 

  1. 1.
  2. <context-param>
  3.         <param-name>webAppRootKey</param-name>
  4.         <param-value>petclinic.root</param-value>
  5. </context-param>


  6. 2.
  7. <context-param>
  8.         <param-name>log4jConfigLocation</param-name>
  9.         <param-value>/WEB-INFclasseslog4j.properties</param-value>
  10. </context-param>




  11. 3.(该条目在petclinic中被注释掉了)
  12. <listener>
  13.    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  14. </listener>



我们知道,在web-application中使用log4j,有很多配置方式:
a.
用servlet或者ServletContextListener对log4j做配置。通常,你不需要亲自编写servlet或者 listener,比如直接利用log4j的com.apache.jakarta.log4j.Log4jInit类,Spring的 org.springframework.web.util.Log4jConfigServlet和 org.springframework.web.util.Se
rvletContextListener,这种方式配置灵活,可以通过参数条目自行指定log4j.properties的位置。

b.
把log4j 的默认配置文件(log4j.properties)放在classpath中,通常是/web-inf/classes目录下.这种方式是log4j的 默认配置方式,无须其他配置。缺点就是无法灵活的通过配置文件来指定log4j.properties的文件位置。在我们的petclinic项目中,因 为listener条目被注释,所以采用的也是这种缺省方式。


现在我们考虑listener条目没有被注释的情况,这种情况和注册Log4jConfigServlet的目的是一样的,只是必须在支持listener的servlet container中使用。

找 到.Log4jConfigServlet和ServletContextListener的源码,他们都在适当的地方 (callback method)调用了Log4jWebConfigurer.initLogging(getServletContext()); 定位到这个方法,第一句就是:WebUtils.setWebAppRootSystemProperty(servletContext);再定位到该 方法,方法很短:





  1. public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
  2.         String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
  3.         String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
  4.         String oldValue = System.getProperty(key);
  5.         if (oldValue != null) {
  6.             throw new IllegalStateException("WARNING: Web app root system property already set: " + key + " = " +
  7.                                                                 

  8.             oldValue + " - Choose unique webAppRootKey values in your web.xml files!");
  9.         }
  10.         String root = servletContext.getRealPath("/");
  11.         if (root == null) {
  12.             throw new IllegalStateException("Cannot set web app root system property when WAR file is not 

  13. expanded");
  14.         }
  15.         System.setProperty(key, root);
  16.         servletContext.log("Set web app root system property: " + key + " = " + root);
  17.     }




从代码看出,该方法其实就是把该web application的根目录的绝对文件路径作为属性保存在 System的属性列表中。该属性的名字,由web.xml文件中的名为"webAppRootKey"的参数值指出。如果不在web.xml中定义 webAppRootKey参数,那么属性名就是缺省的"webapp.root".在我们的petclinic项目中已经定义了 webAppRootKey参数,其值为"petclinic.root",因此,属性的名字就是"petclinic.root".

再 回到Log4jWebConfigurer.initLogging(getServletContext()),接下来的行为是从web.xml中获取 log4jConfigLocation和log4jRefreshInterval.前者指出log4j配置文件(有可能是xml格式)的位置和名字, 后者则指出重新都取配置文件的时间间隔.然后调用Log4jConfigurer.initLogging()方法对log4j做配置。从 Log4jConfigurer.initLogging()方法我们可以看到,针对不同格式的配置文件(properties或者xml), Log4jConfigurer采用不同的lo4j configurator类做配置,例如DOMConfigurator或者 PropertyConfigurator。

至此,关于petclinic中关于log4j的配置,我们已经基本上弄清楚了。可是System对象中的
petclinic.root属性在什么时候使用呢?在web-inf/classes下面的log4j.properties文件中,有这么一句:
log4j.appender.logfile.File=${petclinic.root}/WEB-INF/petclinic.log
这 样,我们就用上了petclinic.root属性了。从上面的分析可知,如果你不在web.xml中定义webAppRootKey参数,那么你就得把 log4j.appender.logfile.File=${petclinic.root}/WEB-INF/petclinic.log
中的petclinic.root变量改为webapp.root变量或者干脆换成别的绝对路径了。 
展开阅读全文

关于LOG4J设置子类的问题~~~

08-08

设置两个roogLogge的子类即:printLoger和fileLogerrnrn子类分别设置了rnlog4j.additivity.printLoger=falsern和rnlog4j.additivity.fileLoger=falsern以屏蔽roogLoger的两个Appender(CONSOLE,WRITEFILE)rn这样两个子类分别只进行控制台(printLoger)和文件(fileLoger)的输出。rnrn但是发现实际运行时,log4j.additivity.子类=false 并没有起做用,两个子类还是和rootLogger一样都进行控制台和文件输出。rnrn请问是不是additivity用法不对??? 有哪位碰到过这类问题,谢谢!!1rnrn我的配置文件如下:rnrn#rn#级别: High level > Low levelrn# FATAL->ERROR->WARN->INFO->DEBUGrn#说明:logger输出为从低->高,即logger若为DEBUG级别将输出所有级别信息rnrnrn#定义根logger,控制台输入及写文件rnlog4j.rootLogger=DEBUG,CONSOLE,WRITEFILErnrn#定义只控制台输出loggerrnrnlog4j.additivity.printLoger=falsernlog4j.logger.printLoger=DEBUG,CONSOLErn#定义只写文件logerrnlog4j.additivity.fileLoger=falsernlog4j.logger.fileLoger=DEBUG,WRITEFILErnrnrn#定义控制台输出Appenderrnlog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderrnlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutrnlog4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d ---[%C4:%L] %m [%t] %n%nrnrn#定义写文件Appenderrnlog4j.appender.WRITEFILE=org.apache.log4j.DailyRollingFileAppenderrnlog4j.appender.WRITEFILE.File=c:/knowbase.logrnlog4j.appender.WRITEFILE.layout=org.apache.log4j.PatternLayoutrnlog4j.appender.WRITEFILE.layout.ConversionPattern=[%-5p] %d ---[%C4:%L] %m [%t] %n%nrnlog4j.appender.WRITEFILE.Append=truernlog4j.appender.WRITEFILE.DatePattern='.'yyyy-MM-ddrnrnrnrn 论坛

关于log4j?

11-26

为什么总有这个提示:rnlog4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.rnConvertUtils).rnlog4j:WARN Please initialize the log4j system properly.rnrn这是我的加载它的程序:rn ServletConfig config = getServletConfig();rn ServletContext application = config.getServletContext();rn String realPath = application.getRealPath(File.separator);rn String realPathError = null;rn if (realPath == null) rn realPathError = "application.getRealPath returned null";rn realPath = File.separator;rn rnrn String lastCharacter = realPath.substring(realPath.length()-1,rn realPath.length());rn if (!lastCharacter.equals(File.separator)) rn realPath = realPath + File.separator;rn rn rn PropertyConfigurator.configureAndWatch(realPath rn + "WEB-INF" rn + File.separatorrn + "Logging.properties");rn logger = Logger.getLogger(Login.class);rn logger.error("ssssssssssss");rn logger.info("aaaaaaaaa");rn logger.debug("xxxxxxxx");rnrn它会创建communitymedical.log文件但是里面什么也没有,我的信息没有.rnrn# -------------------------------------------------------------------rn# This is the logging configuration file for the Track+ application.rn# $Id: Logging.properties,v 1.2 2004/10/29 13:36:41 friedj Exp $#rn# -------------------------------------------------------------------rn# We use Log4J for all Track+ logging and we embed the log4jrn# properties within this application configuration.rn#rn# Usually, there is no need to change this.rn# -------------------------------------------------------------------rnrn# This first category is required and the categoryrn# must be named 'default'. This is used for all loggingrn# where an explicit category is not specified.rnlog4j.rootCategory = WARN, defaultrnrnlog4j.appender.default = org.apache.log4j.FileAppenderrn# log4j.appender.default = org.apache.log4j.ConsoleAppenderrnlog4j.appender.default.file = F:\\Tomcat5\\logs\\Torque.logrnlog4j.appender.default.layout = org.apache.log4j.PatternLayoutrnlog4j.appender.default.layout.conversionPattern = %-5p %c - %m%nrn#log4j.appender.default.layout = org.apache.log4j.SimpleLayoutrnrnlog4j.logger.org.apache.torque = ERROR, CAtrackrn#log4j.logger.org.apache.torque = ERROR, FAtrackrnrn# One nice file appenderrnlog4j.appender.FAtrack = org.apache.log4j.FileAppenderrnlog4j.appender.FAtrack.file = F:\\Tomcat5\\logs\\communitymedical.log rnlog4j.appender.FAtrack.layout = org.apache.log4j.PatternLayoutrnlog4j.appender.FAtrack.layout.conversionPattern = %d [%t] %-5p %c - %m%nrnlog4j.appender.FAtrack.append = falsernrn# One nice console appenderrnlog4j.appender.CAtrack = org.apache.log4j.ConsoleAppenderrn# log4j.appender.CAtrack.layout = org.apache.log4j.SimpleLayoutrnlog4j.appender.CAtrack.layout = org.apache.log4j.PatternLayoutrnlog4j.appender.CAtrack.layout.conversionPattern = %d [%t] %-5p %c - %m%nrnrn# The logger for Track+ properrnlog4j.logger.com.aurel.track = DEBUGE,FAtrackrn# Here we can switch on and off logging for the different packagesrnlog4j.logger.com.aurel.track.admin = WARNrnlog4j.logger.com.aurel.track.attachment = WARNrnlog4j.logger.com.aurel.track.dropdowns = WARNrnlog4j.logger.com.aurel.track.dropdowns.AccessBeans = WARNrnlog4j.logger.com.aurel.track.user = WARNrnlog4j.logger.com.aurel.track.persist = WARNrnlog4j.logger.com.aurel.track.persist.TSite = WARNrnlog4j.logger.com.aurel.track.persist.TWorkItem = WARNrnlog4j.logger.com.aurel.track.item = WARNrnlog4j.logger.com.aurel.track.item.SaveItemAction = WARNrnlog4j.logger.com.aurel.track.item.EditItemForm = WARNrnlog4j.logger.com.aurel.track.util = WARNrnlog4j.logger.com.aurel.track.util.MailHandler = WARNrnlog4j.logger.com.aurel.track.util.DetectBrowser = WARNrnlog4j.logger.com.aurel.track.report = WARNrnlog4j.logger.com.aurel.track.report.charting.ReportChartAction = WARNrnlog4j.logger.com.aurel.track.report.charting.ReportChartOneAction = WARNrnlog4j.logger.com.aurel.track.report.charting.ReportChart = WARNrnlog4j.logger.com.aurel.track.report.gantt = WARNrnlog4j.logger.com.aurel.track.report.DateFilter = WARNrn#rn# Uncomment the following line if you have not yet rn# a connection to your SMTP server:rn# log4j.logger.com.aurel.track.util.JavaMailBean = WARNrnrn#rn# The test harnish logging behaviourrnlog4j.logger.com.aurel.track.DataBaseUtils = INFOrnlog4j.logger.com.aurel.track.LogonTest = INFOrnlog4j.logger.com.aurel.track.RegisterNewUserTest = INFOrnlog4j.logger.com.aurel.track.AdministrationTest = INFOrnlog4j.logger.com.aurel.track.EditItemTest = DEBUGrnlog4j.logger.com.aurel.track.ReportTest = INFOrnrn# log4j.logger.com.aurel.track.tql.TqlQueryAction = WARNrnlog4j.logger.com.aurel.track.tql.TqlQueryAction = WARNrnlog4j.logger.com.aurel.track.tql.interpreter.TqlInterpreter = WARNrnrn 论坛

没有更多推荐了,返回首页