How to pass properties from application.properties to logback config file

saeedj picture saeedj · Nov 9, 2016 · Viewed 10k times · Source

Overview:

I am using Sentry appender in my logback.xml file and I want to pass plenty of tags as parameters from application.properties file to logback config file.

logback.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

    <appender name="SENTRY" class="com.getsentry.raven.logback.SentryAppender">
        <dsn>
            https://e0a61232c92f42ffa34c22914d676a8e:[email protected]/112817
        </dsn>
        <springProfile name="dev">
            <tags>env:dev,app:${app.name},platform:aws</tags>
        </springProfile>
        <springProfile name="stage">
            <tags>env:dev</tags>
        </springProfile>
        <springProfile name="test">
            <tags>env:test</tags>
        </springProfile>

        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
    </appender>

    <root level="ERROR">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="SENTRY"/>
    </root>

</configuration> 

application.properties:

security.ignored=/**

logging.level.root = DEBUG

spring.profiles.active=dev
app.name=retailServices

Note: the spring.profiles.active property in application.properties is mapped to springProfile tag in logback config file.


But the issue is the fact that the "app.name" property cannot be found in logback.xml file. If I use this property as system properties it works but I want to pass it to config file from application.properties.

So any solution, feedback and idea would be highly appreciated.

Answer

Maciej Walkowiak picture Maciej Walkowiak · Nov 9, 2016

In logback.xml include:

<property resource="application.properties" />

And then you can refer properties in a standard way, for example ${app.name}.