微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring-XD不读取logback.xml

我正在尝试在Spring-XD中运行一个位于以下路径下的作业:

/spring-xd/xd/modules/job/MyJobName (I'll call this path MyJobName below)

我的jar位于MyJobName / lib下,在其根路径中包含文件logback.xml.不幸的是,spring-xd似乎完全无视该文件.当我通过我的IDE(IntelliJ)运行作业时,日志记录工作正常,但是当我使用spring-xd运行它时,它完全忽略了我的SiftingAppender.

这是我的logback.xml文件的样子:

figuration>

    

我想把这个logback.xml文件放在/ spring-xd / xd / config下,或者放在另一个配置文件夹下,但是我尝试的都没有.我尝试查看Spring-XD文档,但一无所获.

任何见解将不胜感激.

最佳答案
您必须将Logback直接放在类路径中.见here

Logback can be configured either programmatically or with a configuration script expressed in XML or Groovy format. By the way,existing log4j users can convert their log4j.properties files to logback.xml using our PropertiesTranslator web-application.

Let us begin by discussing the initialization steps that logback follows to try to configure itself:

  • Logback tries to find a file called logback.groovy in the classpath.

  • If no such file is found,logback tries to find a file called logback-test.xml in the classpath.

  • If no such file is found,it checks for the file logback.xml in the classpath.

  • If no such file is found,and the executing JVM has the ServiceLoader (JDK 6 and above) the ServiceLoader will be used to resolve an implementation of com.qos.logback.classic.spi.Configurator. The first implementation found will be used. See ServiceLoader documentation for more details.

  • If none of the above succeeds,logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

听起来我的配置文件不在类路径中.通常,在大多数框架中,认情况下config目录不在类路径中,在很多情况下,它是/ config /< files>在类路径中,当使用ClassLoader加载它们时,必须使用/ config / name指定它们.但是,Logback不会这样做,所以如果要将文件存储在config目录中,则必须手动加载它们.

基本上,您可以告诉JoranConfigurator从类路径中的自定义位置加载文件,处理错误等等,以确保您找到了您的文件.见here for more details.

以下是我加载Logback配置的方法,您可以将其调整到您的系统中.在这种情况下,resource是您的类路径中的路径,无论您决定放置logback.xml文件.在这种情况下,它可能是/spring-xd/xd/config/logback.xml.

public class LogbackConfigurator {
    private static final Logger LOG =
            LoggerFactory.getLogger(LogbackConfigurator.class);

    public static boolean configure(String resource) throws JoranException {
        final InputStream configInputStream = LogbackConfigurator.class.getResourceAsstream(resource);
        final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);
        //  the context was probably already configured by default configuration rules
        loggerContext.reset();

        if(configInputStream != null) {
            try {
                configurator.doConfigure(configInputStream);
            } catch (JoranException e) {
                e.printstacktrace();
            }
            StatusPrinter.printInCaSEOfErrorsOrWarnings(loggerContext);
            return true;
        } else {
            LOG.error("Unable to find logback file: {}",resource);
            StatusPrinter.printInCaSEOfErrorsOrWarnings(loggerContext);
            return false;
        }
    }
}

通常,此方法将被称为main中的第一行,其中包括

LogbackConfigurator.configure(path);

运行此类后,应配置您的logback,就好像系统能够正常查找配置文件一样.请注意,如果您不想将位置硬编码到系统中,还可以使用-Dproperty=value和System.getProperties().get(keyname)动态指定备用文件路径的备用位置.

您还可以在Spring配置中配置要注入的位置,但我个人不建议您在注入之前通常需要配置日志记录,这样如果在注入期间发生任何日志记录事件,它们将被适当地记录.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐