Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use Spring Property Placeholders in logback.xml

I have a Spring Boot console app using Logback. All of the properties (for the app as well as for Logback) are externalized into a standard application.properties file in the classpath. These properties are picked up just fine in the app itself, but are not picked up in the logback.xml file. It appears as though the logback.xml is processed before Spring Boot fires up, therefore the EL placeholders are not processed.

Using the FileNamePattern as an example, in the application.properties, I have something like this:

log.filePattern=/%d{yyyy/MM-MMMM/dd-EEEE} 

and in logback.xml, I'll have this:

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">     <FileNamePattern>${log.logDirectory}${log.filePattern}.log     </FileNamePattern> </rollingPolicy> 

When running the app, I'll see errors such as:

ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:25 -  RuntimeException in Action for tag [rollingPolicy] java.lang.IllegalStateException: FileNamePattern [log.logDirectory_IS_UNDEFINEDlog.filePattern_IS_UNDEFINED.log] does not contain a valid DateToken 

Similar code works just fine in other Spring (not Spring Boot) apps, so I'm curious if Spring Boot just behaves a bit differently.

Solution:

Thanks for the reply @Gary! Good to know about the difference between Spring EL and Logback's variables...I had assumed it was Spring that was in charge of parsing those variables for me. I did have the element, but that got me to thinking.

My application.properties file was outside of the jar, so Logback had no idea where to find it. By keeping my Spring-related properties in my external application.properties file, moving the logging-related properties into an application-internal.properties file (located inside the jar), and pointing Logback to that file (<property resource="application-internal.properties" />) got everything working as expected!

like image 875
brmc72 Avatar asked Mar 28 '15 21:03

brmc72


People also ask

How do you use properties in Logback xml?

properties file in your logback configuration. The tag works in a similar way to Logback's standard tag, but rather than specifying a direct value you specify the source of the property (from the Environment). You can use the scope attribute if you need to store the property somewhere other than in local scope.

How do I enable Logback in spring boot?

By default, Spring Boot picks up the native configuration from its default location for the system (such as classpath:logback. xml for Logback), but you can set the location of the config file by using the "logging. config" property.

Where should Logback xml be stored?

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.

Does spring use Logback instead of log4j?

Spring Boot by default using Logback for its logging. There is no need for adding it explicitly: By default, If you use the 'Starter POMs', Logback will be used for logging.


2 Answers

Since Spring Boot 1.3 you have a better way of getting spring properties into your logback-spring.xml configuration:

Now you can just add a "springProperty" element.

<springProperty name="destination" source="my.loggger.extradest"/> <appender name="FILE" class="ch.qos.logback.core.FileAppender">     <file>${destination}</file>         ...     </file> </appender> 

https://github.com/spring-projects/spring-boot/commit/055ace37f006120b0006956b03c7f358d5f3729f

edit: thanks to Anders

.........

like image 186
Lasse L Avatar answered Sep 24 '22 11:09

Lasse L


${...} is not "Spring EL" in Spring; they are property placeholders.

I think you are confusing logback "variables" with Spring "Property Placeholders".

They just happen to use the same syntax ${...}.

logback knows nothing about the Spring property placeholder mechanism and vice-versa. You need to configure your logback variables according to the logback documentation and not in application.properties / application.yml which is strictly a Spring (boot) concept.

EDIT:

After a quick look at the logback docs, adding

<property resource="application.properties" /> 

to the logback.xml should work.

like image 34
Gary Russell Avatar answered Sep 25 '22 11:09

Gary Russell