0
votes

I'm creating a Java Spring Boot 2.1 application that uses Maven to add the spring-boot-starter dependency. As written in the documentation, this enables the default logback configuration. This configuration can be found freely in this repository and contains syntax that is a bit difficult for me to understand.

1. The mysterious dash

I know Spring properties are set by using the following syntax

${property.name:someDefaultValue}

However, in the xml files for the logback configuration, there is always a dash right after the colon. Why is this? for example:

<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}/>"

2. The changing property names

It is also unclear to me where all these property names are coming from. In the documentation it is mentioned to fill our application.properties or application.yml with properties such as:

logging.file = /app/logs/mylog.log

How and where is the property with name logging.file translated to the property with name LOG_FILE as used in the logback xml file?

3. The missing property

Specifically this property has no equivalent property in application.properties (or .yml) to configure it

<totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0} </totalSizeCap>

If I want to change this property to some other value, I'd have to somehow set the LOG_FILE_TOTAL_SIZE_CAP property. How can I do this while still using the original default logback configuration xml provided by spring boot?

4. We're stuck with the default appenders

The only appender logic in the logback xml files is:

<root level="INFO">

<appender-ref ref="CONSOLE"/>

<appender-ref ref="FILE"/>

</root>

And the application.properties allow no way to change this. Although I must admit this is very useful default, it sometimes doesn't work. It seems like overkill to have to completely overwrite the entire logback xml configuration for one small tweak to an appender or policy. If I were to provide multiple custom logback/xml configuration files on my classpath, would logback "merge" them, or simply pick one and ignore the others? How does overwriting/merging work, which xml 'wins'?

1

1 Answers

1
votes

1. The mysterious dash

For logback: https://logback.qos.ch/manual/configuration.html. Specifically:

Under certain circumstances, it may be desirable for a variable to have a default value if it is not declared or its value is null. As in the Bash shell, default values can be specified using the ":-" operator. For example, assuming the variable named aName is not defined, "${aName:-golden}" will be interpreted as "golden".

2. The changing property names

This should help with the mappings: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html. Also poke around org.springframework.boot.logging.LoggingSystemProperties.

3. The missing property

I agree with your assessment! Maybe doing some digging around with LoggingSystemProperties could help here (in the apply() method?). But more likely to be resolved by providing your own logback.xml.

4. We're stuck with the default appenders

This is also true. See 83.1 of https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html. Practically speaking, every project I've ever worked with has provided its own log4j.xml, logback.xml etc.