Spring Boot未加载logback JNDI选择器 [英] logback JNDI selector is not loaded in Spring boot

查看:82
本文介绍了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.效果很好.

I have a issue that blocked me for days. I explain: i have 2 apps share one server. I call them appA and appB. We use Spring framework and we use logback for the logging system. I configured the logback JNDI context selector, as explainning in the doc: http://logback.qos.ch/manual/contextSelector.html . and i set the logback file as: logback-appB.xml and logback-appA.xml in the server. It works well..

但是现在我们将appA迁移到spring boot,而appB停留在spring.我不知道为什么appA无法加载我在web.xml中配置的JNDI上下文名称:

But now we migrate the appA to spring boot, and the appB stay in spring. I dont know why the appA can not load the JNDI context name that i configured in the web.xml:

<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");结果是null.我不知道为什么,Spring引导力量会使用它们的上下文吗?

then i debuged the line 54 in the class ch.qos.logback.classic.selector.ContextJNDISelector: contextName = JNDIUtil.lookup(ctx, "java:comp/env/logback/context-name"); the result is aways null. I dont know why, is the Spring boot force to use their context?

我认为也许我可以使用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行中进行调试,字符串configLocation不再为null.

I think maybe i can use the logging.config to force use the logback config file with these try
logging.config=Z:/DEV/...../logback-appA.xml, or logging.config=file:Z:/DEV/...../logback-appA.xml or logging.config=classepath:logback-appA.xml, none of them can load the file. debug in the line 67 of the class org.springframework.boot.logging.logback.LogbackLoggingSystem, the String configLocation is aways null.

即使我设置了logging.config = Z:/DEV/.../logback-spring.xml,也无法加载il.

even i set the logging.config=Z:/DEV/...../logback-spring.xml, il can not be loaded.

我想我不能使用 spring Active 配置文件,因为 appB 不使用 spring boot.

i think i can't use the spring Active profile, as the appB do not use spring boot.

spring boot的版本是2.1.7.如果有人可以提供帮助,则非常感谢.

The version of spring boot is 2.1.7. If some one can help, thanks so much.

推荐答案

我遇到了同样的问题,经过数天的搜索,发现该链接最有帮助: http://rostislav-matl.blogspot.com/2013/03/renamed-logback-config-file-for-web.html

I was having the same problem and after days of searching this link proved to be most helpful: http://rostislav-matl.blogspot.com/2013/03/renamed-logback-config-file-for-web.html

在您的spring初始值设定项中,您可以覆盖onStartup以添加自定义侦听器:

In your spring initializer you can override onStartup to add a custom listener:

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    // Add the customer listener to configure logback
    servletContext.addListener(CustomServletContextListener.class);
    super.onStartup(servletContext);
  }

,然后实现一个自定义侦听器,该侦听器将加载回发配置:

and then implement a custom listener which loads the logback configuration:

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);
      }
    }
  }
}

这篇关于Spring Boot未加载logback JNDI选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆