sjngm
sjngm

Reputation: 12871

Empty Default String for Property in logback.xml

Our project structure regarding the logback.xmls looks like this:

src\main\resources\logback.xml
src\main\resources\config\dev\logback.xml
src\main\resources\config\sjngm\dev\logback.xml
src\main\resources\config\int\logback.xml
src\main\resources\config\local\logback.xml
src\main\resources\config\prod\logback.xml

where the first one references to the environment specific one:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
  <contextName>sjngm</contextName>
  <jmxConfigurator />
  <include resource="config/${extra}${env:-local}/logback.xml" />
</configuration>

Note that extra is not defined most of the times, which is why used this for a while:

  <include resource="config/${extra:-}${env:-local}/logback.xml" />

This stopped working at one point, can't remember which version of logback. So we changed it to

  <include resource="config/${extra:-./}${env:-local}/logback.xml" />

which also worked for quite a while.

Now we switched to Spring Boot 1.5.4 (contains logback-classic 1.1.11 and logback-core 1.1.11) and it stopped working again. The latest error message is:

11:08:15,020 |-WARN in ch.qos.logback.core.joran.action.IncludeAction - Could not find resource corresponding to [config/./local/logback.xml]

If I go back to

  <include resource="config/${extra:-}${env:-local}/logback.xml" />

the message is

11:19:28,778 |-WARN in ch.qos.logback.core.joran.action.IncludeAction - Could not find resource corresponding to [config/extra_IS_UNDEFINEDlocal/logback.xml]

Note that logback still uses "local" as a default string for env, so not all is broken.

What do I do now? Basically I want to tell logback that I want an empty string where extra would be.

Upvotes: 0

Views: 3766

Answers (2)

Dave The Dane
Dave The Dane

Reputation: 889

This worked for me:

<property name="extra" value="${logback.myApp.extra:- }" />

Logback seems to trim Whitespace out of the value. So the default value of Space did the trick.

Embedded Whitespace is preserved, which may lead to a FileNotFoundException if Tabs were embedded, but embedded Spaces were ok.

Setting a Property in a Java Initialiser had the desired effect:

System.setProperty("logback.myApp.extra", "\t    \tEXTRA_EXTRA_EXTRA\t    \t");

The Tabs & Spaces were removed from the Property, which was assigned the value EXTRA_EXTRA_EXTRA

(the Java Initialiser must be invoked before any Logging has taken place & may contain no Logging itself)
You could of course set the Property on the Java Command line.

P.S. if the Property is undefined & you omit the Space (${logback.myApp.extra:-}), it is assigned the value:

logback.myApp.extra_IS_UNDEFINED

...so it may be wise to add a suitable comment:

<property name="extra" value="${logback.myApp.extra:- }" /><!-- N.B. Empty Default value must contain @ least 1 Space!! -->

Upvotes: 0

sjngm
sjngm

Reputation: 12871

This also doesn't work:

<property name="defaultExtra" value="" />
<include resource="config/${extra:-${defaultExtra}}${env:-local}/logback.xml" />

as an empty string seems to always result in an undefined property.

The only working thing I can come up with is this:

<if condition='isDefined("extra")'>
  <then>
    <include resource="config/${extra}${env:-local}/logback.xml" />
  </then>
  <else>
    <include resource="config/${env:-local}/logback.xml" />
  </else>
</if>

plus this into the pom.xml:

<dependency>
  <groupId>org.codehaus.janino</groupId>
  <artifactId>janino</artifactId>
</dependency>

Isn't this nice?! So why did they have to break what was working nicely???

Upvotes: 1

Related Questions