Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 462
saeedj Avatar asked Nov 08 '16 23:11

saeedj


2 Answers

In logback.xml include:

<property resource="application.properties" />

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

like image 110
Maciej Walkowiak Avatar answered Sep 22 '22 04:09

Maciej Walkowiak


I found that Spring has support of next tag <springProperty/> described here . It means that you can easily add variable from property files even this variable value spring resolves from environment/system variable.

like image 37
Andriy Rymar Avatar answered Sep 24 '22 04:09

Andriy Rymar