我在spring工程下的web.xml文件中配置log4j相关属性,代码如下
<!-- 日志记录 --> <context-param> <!-- 日志配置路径 --> <param-name>log4jConfigLocation</param-name> <param-value> classpath:log4j.properties </param-value> </context-param> <context-param> <!-- 日志页面刷新间隔 --> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
一开始我把classpath:log4j.properties写成了classpath*:log4j.properties就报错了,如下
java.lang.IllegalArgumentException: Invalid 'log4jConfigLocation' parameter: Log4j config file [F:\tomcat\webapps\hoteldemo\classpath*:log4j.properties] not found
求解答,找了好多资料都不是我想要的,谢谢了
人家log4j读取web.xml的变量逻辑凭啥一定要跟spring一致呢,这个又没形成标准,具体你还是自己看看Log4jConfigListener里面源码怎么写的######
[F:\tomcat\webapps\hoteldemo\classpath*:log4j.properties] not found
首先系统这样说就是找不到你所指向的路径,
但是contextConfigLocation配置的时候是结合web.xml的配置文件内容一起加载的,系统自动识别支持你这样的classpath*的写法 单写的话肯定是不行的 因为没有描述文件让系统知道你写的代码 所以加载不上就报错
log4j.rootCategory=debug, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n log4j.category.org.springframework.beans.factory=error
主要常用的属性也就这么多了
######[F:\tomcat\webapps\hoteldemo\classpath*:log4j.properties] not found
首先系统这样说就是找不到你所指向的路径,
但是contextConfigLocation配置的时候是结合web.xml的配置文件内容一起加载的,系统自动识别支持你这样的classpath*的写法 单写的话肯定是不行的 因为没有描述文件让系统知道你写的代码 所以加载不上就报错
log4j.rootCategory=debug, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n log4j.category.org.springframework.beans.factory=error
主要常用的属性也就这么多了
这个我已经写了的,写在log4j.properties中,log4j.properties与applicationContext.xml等配置文件都在同一目录下,我今天看了一下源码,Log4jConfigListener的代码有如下内容
public class Log4jConfigListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { Log4jWebConfigurer.initLogging(event.getServletContext()); } public void contextDestroyed(ServletContextEvent event) { Log4jWebConfigurer.shutdownLogging(event.getServletContext()); } }
public static void initLogging(ServletContext servletContext) { // Expose the web app root system property. if (exposeWebAppRoot(servletContext)) { WebUtils.setWebAppRootSystemProperty(servletContext); } // Only perform custom log4j initialization in case of a config file. String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM); if (location != null) { // Perform actual log4j initialization; else rely on log4j's default initialization. try { // Resolve system property placeholders before potentially // resolving a real path. location = SystemPropertyUtils.resolvePlaceholders(location); // Leave a URL (e.g. "classpath:" or "file:") as-is. if (!ResourceUtils.isUrl(location)) { // Consider a plain file path as relative to the web // application root directory. location = WebUtils.getRealPath(servletContext, location); } // Write log message to server log. servletContext.log("Initializing log4j from [" + location + "]"); // Check whether refresh interval was specified. String intervalString = servletContext.getInitParameter(REFRESH_INTERVAL_PARAM); if (intervalString != null) { // Initialize with refresh interval, i.e. with log4j's watchdog thread, // checking the file in the background. try { long refreshInterval = Long.parseLong(intervalString); Log4jConfigurer.initLogging(location, refreshInterval); } catch (NumberFormatException ex) { throw new IllegalArgumentException("Invalid 'log4jRefreshInterval' parameter: " + ex.getMessage()); } } else { // Initialize without refresh check, i.e. without log4j's watchdog thread. Log4jConfigurer.initLogging(location); } } catch (FileNotFoundException ex) { throw new IllegalArgumentException("Invalid 'log4jConfigLocation' parameter: " + ex.getMessage()); } } }
public static final java.lang.String CLASSPATH_URL_PREFIX = "classpath:"; // Field descriptor #73 Ljava/lang/String; public static final java.lang.String FILE_URL_PREFIX = "file:"; // Field descriptor #73 Ljava/lang/String; public static final java.lang.String URL_PROTOCOL_FILE = "file"; // Field descriptor #73 Ljava/lang/String; public static final java.lang.String URL_PROTOCOL_JAR = "jar"; // Field descriptor #73 Ljava/lang/String; public static final java.lang.String URL_PROTOCOL_ZIP = "zip";
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。