Spring 启动 - 部署为 WAR 时从 WEB-INF/ 文件夹加载 'logback.xml'
Spring Boot - Loading 'logback.xml' from the WEB-INF/ folder when deploying as WAR
我正在努力将传统的 Spring MVC 应用程序更新为 Spring 引导(部署为 WAR)。
一个要求是,为了保持与我们的远程安装的兼容性,logback.xml
文件 需要 位于部署的 webapp 中的以下位置:
webapp/WEB-INF/logback.xml
在项目源代码中,如果我将文件放在 src/main/webapp/WEB-INF/logback.xml
中,它会成功复制到 webapp/WEB-INF/logback.xml
,但我找不到从该位置加载和使用它的方法。
我已经尝试将 Spring 引导 logging.config
属性 设置为以下内容:
logging.config=/WEB-INF/logback.xml
希望自动配置会处理它,但这失败了,没有错误消息,而是加载了默认的 Spring 引导日志记录配置。
使用 Spring 引导处理此问题的正确方法是什么? (欢迎使用示例代码)
事实证明这需要破解:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
// In case of regular deployments, use WEB-INF/logback.xml
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
configureLogging(servletContext);
LoggerFactory.getLogger(getClass()).info("Done configuring");
}
// manually configure logging
private void configureLogging(final ServletContext servletContext) {
try {
String realPath = servletContext.getRealPath("/WEB-INF/logback.xml");
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
configurator.doConfigure(realPath);
} catch (JoranException je) {
// StatusPrinter will handle this
}
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
此外,我在 src/main/resources
:
中有 logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="logback-include.xml" />
</configuration>
和logback-include.xml在src/main/resources
中有一些共同的配置:
<?xml version="1.0" encoding="UTF-8"?>
<included>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<!-- put your loggers here -->
<logger name="org.springframework.web" additivity="false" level="INFO">
<appender-ref ref="CONSOLE"/>
</logger>
<!-- put your root here -->
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</included>
最后,src/main/webapp/WEB-INF
中的 logback.xml 导入了通用配置,就像 logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="logback-include.xml" />
</configuration>
将 logback.xml
放入 src/main/webapp/WEB-INF/
会导致文件被复制到 webapp/WEB-INF/
,这是类路径中 webapp/WEB-INF/classes/
之上的目录级别。尝试将其放入 application.properties
:
logging.config=classpath:../logback.xml
这样 logback.xml
将在初始化期间正确定位。查看 Common Application Properties 中 classpath:
的使用示例,它用于表示类路径在 application.properties
.
中的位置
尽管说实话,我宁愿在 src/main/webapp/WEB-INF/
中使 logback.xml
成为 link 到 classes/logback.xml
的符号(这对部署有意义(并且将在您的远程安装中很有用),因为 webapp/WEB-INF/logback.xml
然后 link 到 webapp/WEB-INF/classes/logback.xml
)并通过将 logback.xml
在 src/main/resources
中(将在 webapp/WEB-INF/classes/
中复制)。由于我不知道您的远程安装的细节以及您所说的兼容性是什么意思,所以这可能有效也可能无效。
我正在努力将传统的 Spring MVC 应用程序更新为 Spring 引导(部署为 WAR)。
一个要求是,为了保持与我们的远程安装的兼容性,logback.xml
文件 需要 位于部署的 webapp 中的以下位置:
webapp/WEB-INF/logback.xml
在项目源代码中,如果我将文件放在 src/main/webapp/WEB-INF/logback.xml
中,它会成功复制到 webapp/WEB-INF/logback.xml
,但我找不到从该位置加载和使用它的方法。
我已经尝试将 Spring 引导 logging.config
属性 设置为以下内容:
logging.config=/WEB-INF/logback.xml
希望自动配置会处理它,但这失败了,没有错误消息,而是加载了默认的 Spring 引导日志记录配置。
使用 Spring 引导处理此问题的正确方法是什么? (欢迎使用示例代码)
事实证明这需要破解:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
// In case of regular deployments, use WEB-INF/logback.xml
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
configureLogging(servletContext);
LoggerFactory.getLogger(getClass()).info("Done configuring");
}
// manually configure logging
private void configureLogging(final ServletContext servletContext) {
try {
String realPath = servletContext.getRealPath("/WEB-INF/logback.xml");
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
configurator.doConfigure(realPath);
} catch (JoranException je) {
// StatusPrinter will handle this
}
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
此外,我在 src/main/resources
:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="logback-include.xml" />
</configuration>
和logback-include.xml在src/main/resources
中有一些共同的配置:
<?xml version="1.0" encoding="UTF-8"?>
<included>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<!-- put your loggers here -->
<logger name="org.springframework.web" additivity="false" level="INFO">
<appender-ref ref="CONSOLE"/>
</logger>
<!-- put your root here -->
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</included>
最后,src/main/webapp/WEB-INF
中的 logback.xml 导入了通用配置,就像 logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="logback-include.xml" />
</configuration>
将 logback.xml
放入 src/main/webapp/WEB-INF/
会导致文件被复制到 webapp/WEB-INF/
,这是类路径中 webapp/WEB-INF/classes/
之上的目录级别。尝试将其放入 application.properties
:
logging.config=classpath:../logback.xml
这样 logback.xml
将在初始化期间正确定位。查看 Common Application Properties 中 classpath:
的使用示例,它用于表示类路径在 application.properties
.
尽管说实话,我宁愿在 src/main/webapp/WEB-INF/
中使 logback.xml
成为 link 到 classes/logback.xml
的符号(这对部署有意义(并且将在您的远程安装中很有用),因为 webapp/WEB-INF/logback.xml
然后 link 到 webapp/WEB-INF/classes/logback.xml
)并通过将 logback.xml
在 src/main/resources
中(将在 webapp/WEB-INF/classes/
中复制)。由于我不知道您的远程安装的细节以及您所说的兼容性是什么意思,所以这可能有效也可能无效。