Specify default value for a logback property, in spring-boot

Viewed 9962

In spring-boot application, I am trying to config a default dir for logback.

Usually, in logback.xml I would config it this way:

<property name="logFile.dir" value="${catalina.home:-/tmp}/logs" />

The separator is :-.

But, in application.properties:

I have to config it this way:

logging.file=${catalina.home:/tmp}/logs/sportslight.log

Need to change the separator from :- to :.

The questions are:

  • In logback.xml, which is the correct separator, :- or :?
  • In application.properties, why only : works, is it because spring-boot would handle it first before pass the value to logback?
2 Answers

In logback.xml or logback-spring.xml, you can set default value for both system property and project property (or you can say spring property).

1) For system property, you can simply go with the :- syntax. In the following example, the default level of ThresholdFilter is ERROR.

<configuration>

  <appender name="sentry" class="io.sentry.logback.SentryAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>${sentryLevel:-ERROR}</level>
    </filter>
  </appender>

</configuration>

You can override it by starting the java process with, such as -DsentryLevel=INFO.

2) For project property/spring property, you can set defaultValue in the springProperty element.

<configuration>

  <springProperty scope="context" name="sentryLevel" source="your.prop.path" defaultValue="ERROR"/>

  <appender name="sentry" class="io.sentry.logback.SentryAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>${sentryLevel}</level>
    </filter>
  </appender>

</configuration>

You can override it by changing the application.properties or application.yml.

your.prop.path=INFO
Related