Spring-XD does not read logback.xml

I am trying to run a job in Spring -XD located in the following path:

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

My jar, located under MyJobName/lib , contains the logback.xml file in its root path. Unfortunately, Spring -XD seems to completely ignore this file. When I run work through my IDE (IntelliJ), the log works fine, but when I run it using Spring -XD, it completely ignores my SiftingAppender.

Here is what my logback.xml file looks like:

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%5p %-25logger{25} %m %n</pattern> </encoder> </appender> <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> <discriminator> <key>publication.run.id</key> <defaultValue>unknown</defaultValue> </discriminator> <sift> <appender name="FILE-${publication.run.id}" class="ch.qos.logback.core.FileAppender"> <file>/data/${publication.run.id}/logs/process.log</file> <append>true</append> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%5p %-25logger{25} %m %n</pattern> </layout> </appender> </sift> </appender> <logger name="com.bitwiseor"> <level value="INFO" /> </logger> <logger name="org.springframework"> <level value="INFO" /> </logger> <root> <level value="INFO" /> <appender-ref ref="SIFT" /> <appender-ref ref="CONSOLE" /> </root> </configuration> 

I want to put this logback.xml file in the / spring -xd / xd / config folder or in another configuration folder, but am not trying to work. I tried to view Spring -XD docs but found nothing.

Any insight would be appreciated.

+5
source share
2 answers

You must put Logback directly in the classpath . See here :

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

We begin by discussing the initialization steps that should be performed to restore:

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

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

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

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

  • If none of the above is true, the log is automatically configured automatically using BasicConfigurator, which will cause the log output to be directed to the console.

It seems to me that your configuration file is not in the classpath . Typically, the configuration directory is not in the path to the default classes in most frameworks, in many cases these are /config/<files> , which are in the class path, and you must specify them with /config/name when loading them from using ClassLoader . However, Logback does not do this, so if you want to save the files in the config directory, you need to download them manually.

Essentially, you can tell JoranConfigurator to download the file from a custom location in the classpath, handle errors, etc., to make sure you find your file. See more details .

This is how I load my log configuration, you can probably adapt it to your system. In this case, resource is the path in your class path wherever you decide to place your logback.xml file. In this case, it will probably be /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; } } } 

Typically, this method could be called one of the first lines in main with something like:

 LogbackConfigurator.configure(path); 

Once this class has been started, your log should be configured as if the system could normally find the configuration file. Note that you can also use -Dproperty=value and System.getProperties().get(keyname) to specify an alternative location for the path to the log file dynamically if you do not want to hardcode the location to your system.

You can also configure the location that should be included in the Spring configuration, but I personally do not recommend it, since you usually need to configure the registration before the injection occurs, so that if any registration events occur during the injection, they will be recorded accordingly.

+6
source

It looks like your logback configuration is having some minor issues.

Try entering the configuration below into your path to the application class; should solve the problem.

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%5p %-25logger{25} %m %n</pattern> </encoder> </appender> <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> <discriminator> <key>publication.run.id</key> <defaultValue>unknown</defaultValue> </discriminator> <sift> <appender name="FILE-${publication.run.id}" class="ch.qos.logback.core.FileAppender"> <file>/data/${publication.run.id}/logs/process.log</file> <append>true</append> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%5p %-25logger{25} %m %n</pattern> </layout> </appender> </sift> </appender> <logger name="com.bitwiseor" level="INFO" /> <logger name="org.springframework" level="INFO" /> <root level="INFO"> <appender-ref ref="SIFT" /> <appender-ref ref="CONSOLE" /> </root> </configuration> 

Note the part below changes according to the log configuration link:

 <logger name="com.bitwiseor" level="INFO" /> <logger name="org.springframework" level="INFO" /> <root level="INFO"> <appender-ref ref="SIFT" /> <appender-ref ref="CONSOLE" /> </root> 
+4
source

All Articles