Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the application properties in logback.xml

Tags:

spring-boot

Is it possible to access application properties of spring boot in log back xml.

application.properties

dummy.property=hello 

logback.xml

${dummy.property} 

This did not work.

Does any one have idea if it will work.

like image 969
Patan Avatar asked Apr 20 '16 12:04

Patan


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 edit Logback xml?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.

How do I specify the path for Logback xml?

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 can I find Logback xml?

xml or logback. xml can be located directly under any folder declared in the class path. For example, if the class path reads "c:/java/jdk15/lib/rt.


2 Answers

If you name your configuration file logback-spring.xml, rather than logback.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. This can be useful if you want to access values from your application.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.

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"/> <appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">     <remoteHost>${fluentHost}</remoteHost>     ... </appender> 
like image 71
Andy Wilkinson Avatar answered Sep 22 '22 12:09

Andy Wilkinson


According to the http://logback.qos.ch/manual/configuration.html#variableSubstitution

Variables can be defined one at a time in the configuration file itself or loaded wholesale from an external properties file or an external resource.
...
The property is not declared in the configuration file, thus logback will look for it in the System properties.

Logback can use system properties or properties defined explicitely. So you need to tell logback to use application.properties file

<property resource="application.properties" /> 
like image 28
Evgeny Avatar answered Sep 22 '22 12:09

Evgeny