![](/img/trans.png)
[英]spring boot logback-spring.xml maxFileSize not working with SizeAndTimeBasedRollingPolicy
[英]Reloading logback-spring.xml with new <maxFileSize></maxFileSize> without restarting the spring boot app
我有一个在C4-Large EC2实例上运行的spring boot应用程序,由于特殊要求,我们需要根据需要更改日志文件<maxFileSize>10MB</maxFileSize>
的最大大小,而无需重新启动spring boot应用程序。 当前,logback-spring.xml位于资源文件夹下,并在启动应用程序时加载。
如果可能的话,我建议在这种情况下不要使用日志记录扩展名(即使用logback.xml而不是logback-spring.xml), 因为它不支持配置扫描 。
在可以使用-Dlogback.configurationFile=/path/to/config.xml
指定的外部位置(不在jar内)使用常规logback.xml文件,您应该能够利用logback的配置扫描 ,例如: <configuration scan="true" scanPeriod="30 seconds">
。
这样,您无需重新启动应用程序即可反映出对logback.xml的更改(例如,更新的最大文件大小)。
经过几次尝试,我得以达到目标-
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${xyz.log.fsize}</maxFileSize>
这里${xyz.log.fsize}
是System属性 ,因此为了重新加载logback-spring.xml而不重新启动spring boot应用程序,我创建了一个post调用-
@PostMapping("/changeSize/{size}")
public void fsize(@PathVariable("size") String size, HttpServletResponse response) {
try {
System.setProperty("xyz.log.fsize", size);
LOG.info("Change in file size is observed reloading the loggers with the latest configs...");
InputStream logConfStream = MQConsumerController.class.getResourceAsStream(File.separator
+ LOGBACK_SPRING_XML);
String logConfString = CharStreams.toString(new InputStreamReader(logConfStream));
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
configurator.doConfigure(new ByteArrayInputStream(logConfString.getBytes()));
LOG.info("file size updated to -> {}", size);
response.setStatus(HttpServletResponse.SC_OK);
} catch (Exception e) {
LOG.error("Error while changing the logs file size {}", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
现在,当您启动应用程序时,使用VM参数-Dxyz.log.fsize=10MB
启动它
应用启动并运行后,进行发布通话-
http://localhost:8080/changeSize/100MB
和瞧! 您的logback-spring.xml会在不启动应用程序的情况下即时重新加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.