java web配置Log4j出现 setFile出错处理

本文介绍了解决Java Web应用中Log4j配置导致的FileNotFoundException问题。主要原因是Log4jConfigListener配置顺序错误及缺少指定的日志文件夹。通过调整监听器顺序并在WEB-INF下创建logs文件夹,配合正确的webAppRootKey配置,可以有效避免此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java web中经常出现的一个错误

java.io.FileNotFoundException: /WEB-INF/logs/logs.log 

 

配置的输出的文件目录如下

log4j.appender.logfile.File=${b5m-client-admin.root}/WEB-INF/logs/logs.log

 

出现这个问题有两个原因

1.Log4jConfigListener 配置放在了ContextLoaderListener的下面

2.WEB-INF 下面没有建立一个logs文件夹

 

下面给出一个配置,webAppRootKey

<context-param>

<param-name>webAppRootKey</param-name>

<param-value>client-admin.root</param-value>

  </context-param>

  

  <context-param>

  <param-name>log4jConfigLocation</param-name>

  <param-value>classpath:log4j.properties</param-value>

  </context-param>

  

  <listener>

  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

  </listener>

  

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

 

log4j.properties文件

log4j.rootLogger=INFO,A1,logfile

log4j.appender.A1=org.apache.log4j.ConsoleAppender 

log4j.appender.A1.layout=org.apache.log4j.PatternLayout 

log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %C:%L [\u65E5\u5FD7\u4FE1\u606F] %m%n

 

log4j.appender.logfile=org.apache.log4j.RollingFileAppender

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

log4j.appender.logfile.MaxFileSize=5MB

log4j.appender.logfile.MaxBackupIndex=5

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

log4j.appender.logfile.layout.ConversionPattern=%d %p [%c][%t] - %m%n

 

关于webAppRootKey 可以参考这篇文章 http://teamojiao.iteye.com/blog/422119

 

java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1967) at org.apache.logging.log4j.util.PropertiesUtil.partitionOnCommonPrefixes(PropertiesUtil.java:555) at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.build(PropertiesConfigurationBuilder.java:174) at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory.getConfiguration(PropertiesConfigurationFactory.java:56) at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory.getConfiguration(PropertiesConfigurationFactory.java:35) at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:557) at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:481) at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:323) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:695) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:716) at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:270) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:309) at org.apache.log4j.LogManager$PrivateManager.getContext(LogManager.java:244) at org.apache.log4j.LogManager.getRootLogger(LogManager.java:89) at org.slf4j.impl.Reload4jLoggerFactory.<init>(Reload4jLoggerFactory.java:67) at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72) at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45)
最新发布
07-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值