Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define logback variables/properties before logback auto-load logback.xml?

Tags:

java

logback

My company has an environment management tool that enables you to look up properties from the environment programmatically in Java. I want to leverage this tool to configure logback. For example, suppose I have a logback.xml as follows (the file appender section in particular):

<?xml version="1.0" encoding="UTF-8"?> <configuration>     <!-- console appender -->     <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">         <encoder>             <pattern>%d{yyyy-MM-dd/HH:mm:ss.SSS} [%thread] %-5level %logger{20}: %msg%n</pattern>         </encoder>     </appender>      <!-- file appender -->     <appender name="FILE" class="ch.qos.logback.core.FileAppender">         <file>${LOG_FILE:-/default/log/file/path</file>         <encoder>             <pattern>%d{yyyy-MM-dd/HH:mm:ss.SSS} [%thread] %-5level %logger{20}: %msg%n</pattern>         </encoder>     </appender>      <root level="DEBUG">         <appender-ref ref="CONSOLE" />         <appender-ref ref="FILE" />     </root> </configuration> 

So, in this case, I want to look up the LOG_FILE property from the environment (or OS, if you will), and pass it to logback before logback loads the logback.xml so that it will know the value of LOG_FILE. So, how can I achieve that? BTW, I know how to define a file appender programmatically, but that's not what I want here.

Thank you very much.

like image 841
JBT Avatar asked Jun 16 '14 00:06

JBT


People also ask

How do I read application properties in Logback xml?

xml , you can use <springProperty> to access properties from Spring's environment including those configured in application. properties . This is described in the documentation: The tag allows you to surface properties from the Spring Environment for use within Logback.

How do I change the Logback configuration file?

Setting the location of the configuration file via a system property. You may specify the location of the default configuration file with a system property named "logback. configurationFile" . The value of this property can be a URL, a resource on the class path or a path to a file external to the application.

Where should the Logback xml be?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.


2 Answers

Define a property in logback.xml and load it into the "context":

<property scope="context" name="logfolder" value="${location.of.the.log.folder}" /> 

Then define your appender referencing the property:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">     <file>${logfolder}/logfile.log</file>     <append>true</append>     <encoder>         <pattern>[%d{ISO8601}] [%p] [%t] [%c] [%m]%n</pattern>     </encoder> </appender> 

From the documentation:

A property with context scope is inserted into the context and lasts as long as the context or until it is cleared. Once defined, a property in context scope is part of the context. As such, it is available in all logging events, including those sent to remote hosts via serialization.

So the default scope, which is "local" may well be sufficient.

like image 61
Alexandre Santos Avatar answered Sep 18 '22 11:09

Alexandre Santos


After quite a bit of scratching my head, I am settling with the following solution.

First, put logback.xml outside classpath so that logback will not automatically load anything.

Second, add the settings from the environment to system properties so that logback can look them up when parsing logback.xml.

Third, programmatically configure logback in the application code. (The official logback documentation has one nice example of that. )

Done.

like image 31
JBT Avatar answered Sep 21 '22 11:09

JBT