我正在尝试在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文件的样子:
我想把这个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配置中配置要注入的位置,但我个人不建议您在注入之前通常需要配置日志记录,这样如果在注入期间发生任何日志记录事件,它们将被适当地记录.
总结 以上是内存溢出为你收集整理的java – Spring-XD不读取logback.xml全部内容,希望文章能够帮你解决java – Spring-XD不读取logback.xml所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)