我正在尝试在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 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:
@H_404_47@
Logback tries to find a file called logback.groovy in the classpath.
@H_502_29@If no such file is found,logback tries to find a file called logback-test.xml in the classpath.
@H_502_29@If no such file is found,it checks for the file logback.xml in the classpath.
@H_502_29@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.
@H_502_29@If none of the above succeeds,logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
@H_502_29@听起来我的配置文件不在类路径中.通常,在大多数框架中,默认情况下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; } } }
LogbackConfigurator.configure(path);
运行此类后,应配置您的logback,就好像系统能够正常查找配置文件一样.请注意,如果您不想将位置硬编码到系统中,还可以使用
-Dproperty=value
和System.getProperties().get(keyname)动态指定备用文件路径的备用位置.您还可以在Spring配置中配置要注入的位置,但我个人不建议您在注入之前通常需要配置日志记录,这样如果在注入期间发生任何日志记录事件,它们将被适当地记录.