spring - 在 Spring Boot 中未加载 logback JNDI 选择器
问题描述
我有一个问题阻止了我好几天。我解释说:我有 2 个应用程序共享一台服务器。我称它们为 appA 和 appB。我们使用 Spring 框架并使用 logback 作为日志系统。我配置了 logback JNDI 上下文选择器,如文档中所述: http ://logback.qos.ch/manual/contextSelector.html 。我将 logback 文件设置为:服务器中的 logback-appB.xml 和 logback-appA.xml。它运作良好..
但是现在我们将appA迁移到spring boot,而appB留在spring中。我不知道为什么 appA 无法加载我在 web.xml 中配置的 JNDI 上下文名称:
<env-entry>
<env-entry-name>logback/context-name</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>appA</env-entry-value>
</env-entry>
然后我调试了 ch.qos.logback.classic.selector.ContextJNDISelector 类中的第 54 行: contextName = JNDIUtil.lookup(ctx, "java:comp/env/logback/context-name"); 结果是空的。我不知道为什么,是 Spring Boot 强制使用它们的上下文吗?
我想也许我可以使用 logging.config 来强制使用 logback 配置文件,这些尝试
logging.config=Z:/DEV/...../logback-appA.xml 或 logging.config=file:Z: /DEV/...../logback-appA.xml 或 logging.config=classepath:logback-appA.xml,它们都不能加载文件。在 org.springframework.boot.logging.logback.LogbackLoggingSystem 类的第 67 行调试,String configLocation 为空。
即使我设置了logging.config=Z:/DEV/...../logback-spring.xml,我也无法加载。
我想我不能使用 spring Active 配置文件,因为 appB 不使用 spring boot。
spring boot的版本是2.1.7。如果有人可以提供帮助,非常感谢。
解决方案
我遇到了同样的问题,经过几天的搜索,这个链接被证明是最有帮助的: http ://rostislav-matl.blogspot.com/2013/03/renamed-logback-config-file-for-web.html
在您的 spring 初始化程序中,您可以覆盖 onStartup 以添加自定义侦听器:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Add the customer listener to configure logback
servletContext.addListener(CustomServletContextListener.class);
super.onStartup(servletContext);
}
然后实现一个加载 logback 配置的自定义侦听器:
public class CustomServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent contextEvent) {
// Get the war/context name from this context event
String contextName = contextEvent.getServletContext().getContextPath().substring(1);
// Get the path of the logback configuration file
URL configFileURL = Thread.currentThread().getContextClassLoader().getResource("logback-" + contextName + ".xml");
// If the URL exists in the classpath
if (configFileURL != null) {
try {
// Reset the existing logger context and set the name to match the current context
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
loggerContext.setName(contextName);
// Update the logger context and configure the logger based on the configuration file
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(configFileURL);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
推荐阅读
- artificial-intelligence - 为什么这个 8 谜题是可解的?
- sql - 在一个 postgresql 表中使用多个唯一约束
- c# - Uno 平台中资源文件的内容构建操作
- pandas - 删除列值无效的行 Date Pandas
- json - 查询未返回数据的 JSON 数据 - Oracle Database 12c (12.1.0.2.0)
- sql - GROUP BY 和 NULL (SQL / BigQuery) 的问题
- r - 用数字代替月份缩写
- typescript - TypeScript 中的类
- android - 为什么我可以使用 android:background="@{!aHomeViewModel.isHaveRecord?#FFFFF:#F3F3F8}" 绑定数据?
- perl - Why is the perl hash overwriting previous data?