Manage environment-specific property logs in the external application properties file

In my spring-boot application, I use logback for logging. The application uses the external application.properties file to configure application-specific properties for the environment, and starts with the option: -spring.config.location=path/to/file . I would like the log configuration to also read properties, so that all the properties of the environment are managed in one place.

I tried the one described here below, but since the properties file is not in the classpath, I get an error message:

 java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.action.PropertyAction - Could not find resource [application.properties] 

Is there something I am missing?

UPDATE:

Adding this configuration works:

  <property file="path/to/file" /> 

But I would like to avoid hardcoding the file path.

+8
java spring spring-boot logback
source share
4 answers

add a system environment variable, so the login returns to this location for the configuration file

 logback.configurationFile=path/to/config.xml 
+3
source share

Since you are using Spring, you can use Spring extensions for the log: logback-ext-spring

What this library does is basically giving you the ability to delegate actual logging to Spring managed objects, giving you access to the Spring context. You can create and configure applications in the Spring configuration, where you will have access to the Environment variables, as well as to application.properties .

You can find more detailed information on the GitHub page, but here is an example configuration based on the example given in the link where the template for the console application is extracted from the "consolePattern" environment "consolePattern" :

logback.xml:

 <configuration> <appender name="consoleAppender" class="ch.qos.logback.ext.spring.DelegatingLogbackAppender"/> <root level="INFO"> <appender-ref ref="consoleAppender"/> </root> </configuration> 

LogbackConfig.java:

 import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.encoder.PatternLayoutEncoder; import ch.qos.logback.core.ConsoleAppender; import ch.qos.logback.ext.spring.ApplicationContextHolder; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class LogbackConfig { @Autowired private Environment environment; @Bean public static ApplicationContextHolder applicationContextHolder() { return new ApplicationContextHolder(); } @Bean public static LoggerContext loggerContext() { return (LoggerContext) LoggerFactory.getILoggerFactory(); } @Bean (initMethod = "start", destroyMethod = "stop") public static PatternLayoutEncoder encoder (LoggerContext ctx) { PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(ctx); encoder.setPattern(environment.getProperty("consolePattern"); return encoder; } @Bean (initMethod = "start", destroyMethod = "stop") public static ConsoleAppender consoleAppender (LoggerContext ctx, PatternLayoutEncoder encoder) { ConsoleAppender appender = new ConsoleAppender(); appender.setContext(ctx); appender.setEncoder(encoder); return appender; } } 

Important
Note that the name of the Spring bean must match the name of the appender in logback.xml , in the example "consoleAppender" . Also remember to specify initMethod and destroyMethod .

Hope this helps you find a way to not recode the path.

+2
source share

If you use spring boot to run your application, you can:

logback.xml

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <property file="${configuration.file.path}" /> ....... </configuration> 

You can change the "configuration.file.path", but you must change this name after starting your application.

You can use the properties defined in the properties file as follows:

 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} - ${property.defined.in.property.file} - %msg%n </Pattern> </layout> </appender> <root level="${level.defined.in.property.file}"> <appender-ref ref="STDOUT" /> </root> 

And add the parameter to the virtual machine when running your application

 -Dlogging.config=/some/path/logback.xml -Dconfiguration.file.path=/other/path/application.properties 

logging.config is a spring-boot configuration, so you must keep this name. You can change this file in each execution without changing the configuration in your application.

if you run your application manually on the command line, it should look like:

 java -Dlogging.config=/some/path/logback.xml -Dconfiguration.file.path=/other/path/application.properties -jar app-1.0.0.jar ..... 

Performing this task, the entire logging configuration is dynamic, and you provide it when the application starts. You may have different instances with different configurations.

+2
source share

We do not use spring-boot , but perhaps this approach may be useful:

We created this LogbackConfigurator . This class is executed when the application starts, listening to ContextRefreshedEvent:

 public class LogbackConfigurator implements ApplicationListener<ContextRefreshedEvent> { // configure your configuration file here private static final String LOGBACK_CONFIGURATION = "some/where/application.properties"; @Override public void onApplicationEvent(ContextRefreshedEvent event) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); try { // creating JoranConfigurator JoranConfigurator joranConfigurator = new JoranConfigurator(); // setting logger context for JoranConfiguratior joranConfigurator.setContext(loggerContext); // resetting context, override default configuration loggerContext.reset(); // configuring Logback with resource joranConfigurator.doConfigure(new Resource(LOGBACK_CONFIGURATION).getInputStream()); } catch (Exception exception) { // exception handling } } } 

To execute the bean, we added this line to our Java configuration:

 @Bean public LogbackTestConfigurator logbackConfigurator() { return new LogbackTestConfigurator(); } 
+1
source share

All Articles