Spring MVC(非Spring Boot)外部Log4j2配置日志无法持续记录问题
问题:Tomcat部署Spring MVC应用时外部Log4j2配置失效
环境与需求
- 部署环境:Tomcat
- 应用类型:非Spring Boot的Spring MVC Web应用
- 核心需求:通过
log4j.configurationFile参数加载外部Log4j2配置文件,同时读取服务器文件系统中的外部app-config.yaml配置文件
异常现象
- 内部配置正常:将
log4j2.xml放入项目资源目录时,日志输出、滚动策略、自动重配置功能均正常工作 - 外部配置异常:使用外部
log4j2.xml时出现以下问题:- 应用启动后仅生成包含部分初始化日志的文件(如Spring上下文启动、资源解析相关日志)
- 后续业务逻辑无任何日志输出
- 运行时新增Appender配置不会生成对应日志文件
monitorinterval配置的自动重加载功能完全失效
现有配置实现
外部YAML配置加载代码(PropertyConfig.java)
@Configuration public class PropertyConfig { public static final String ENV_HOME_VARIABLE = "APP_HOME"; public static final String MAIN_CONFIG_FILE_NAME = "app-config.yaml"; @Bean public static PropertySourcesPlaceholderConfigurer properties(@Autowired ConfigurableEnvironment conf) throws MalformedURLException { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); String configPath = "file:" + System.getenv(ENV_HOME_VARIABLE) + "/" + MAIN_CONFIG_FILE_NAME; yaml.setResources(new UrlResource(configPath)); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject()); Map map = conf.getSystemProperties(); yaml.getObject().forEach((key, value) -> { map.put(key, value); }); return propertySourcesPlaceholderConfigurer; } }
app-config.yaml中的Log4j2配置
log4j: configurationFile: file:/home/user/appHome/app-log4j2.xml
外部log4j2.xml配置
<?xml version="1.0" encoding="UTF-8"?> <Configuration monitorinterval="30" status="WARN" > <Appenders> <!-- Rolling File Appender --> <RollingFile name="rollingFileApplication" bufferSize="0" bufferedIO="false" append="true"> <FileName>/home/user/appHome/log/app.log</FileName> <FilePattern>/home/user/appHome/log/archive/app%d{yyyy-MM-dd}.log.gz</FilePattern> <PatternLayout> <Pattern>%d{yyyy-MM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> <DefaultRolloverStrategy> <Delete basePath="/home/user/appHome/log/archive/" maxDepth="1"> <IfAll> <IfFileName glob="app_*.log*" /> <IfLastModified age="7d" /> </IfAll> </Delete> </DefaultRolloverStrategy> </RollingFile> </Appenders> <Loggers> <Root level="TRACE"> <AppenderRef ref="rollingFileApplication" level="TRACE"/> </Root> <Logger name="ua" additivity="true" level="TRACE"> <appender-ref ref="rollingFileApplication"/> </Logger> </Loggers> </Configuration>
当前日志输出示例
2022-07-28 12:53:39 PM [localhost-startStop-1] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started 2022-07-28 12:53:40 PM [localhost-startStop-1] TRACE org.springframework.core.io.support.PathMatchingResourcePatternResolver - Found Equinox FileLocator for OSGi bundle URL resolution 2022-07-28 12:54:10 PM [localhost-startStop-1] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started 2022-07-28 12:54:10 PM [localhost-startStop-1] TRACE org.springframework.core.io.support.PathMatchingResourcePatternResolver - Found Equinox FileLocator for OSGi bundle URL resolution 2022-07-28 12:54:25 PM [localhost-startStop-1] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started 2022-07-28 12:54:25 PM [localhost-startStop-1] TRACE org.springframework.core.io.support.PathMatchingResourcePatternResolver - Found Equinox FileLocator for OSGi bundle URL resolution
问题求助
请问该问题的原因是什么?如何解决?可提供更多相关信息,感谢任何建议或意见。
内容的提问来源于stack exchange,提问作者Olexyi
相关产品推荐
相关产品推荐

