Spring boot logback.xml externalisation

Viewed 555

I am creating a Spring-boot application in which I would like to externalise the logback file. Reason I am doing this is to provide the flexibility of the consumer to decide in which location they would get the logs for the API. In order to achieve this I have added logging.config entry in my application.properties as below

logging.config=file:${CONFIG_HOME}apilogback.xml

CONFIG_HOME would be a environment variable that points to a common location for all the configuration files. Sample value given to CONFIG_HOME is C:/SWS/apache-tomcat-8.5.16/appconfig/ When I am running my API using tomcat rather than picking up the value of logging.config mentioned in the application.properties file it is picking up the default value defined in catalina.bat. Default value in catalina.bat is as follows :

LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"

To my surprise this seems to be working fine while running in a unix environment but not in a windows environment. Is there a way to make sure we are loading value of logging.config from application.properties of my API.

.

1 Answers

Setting the environment variable LOGGING_CONFIG effectively overrides the value in application.properties (cf. Externalized Configuration). All you can do is to set the value in a source with higher priority, e.g. as system property:

try {
    if (System.getProperty("logging.config") == null) {
        final String config_home = System.getenv("CONFIG_HOME");
        if (config_home != null) {
            System.setProperty("logging.config", "file://" + configHome + "/apilogback.xml");
        }
    }
} catch (AccessControlException ex) {
    // SecurityManager in place...
}

However the name clash between the environment variable used by catalina.sh/catalina.bat and Spring has already been corrected in recent versions of Tomcat (8.5.54 and 9.0.34), where the variable was renamed CATALINA_LOGGING_CONFIG.

The problem affects only Windows (and only if you use catalina.bat/startup.bat, running Tomcat as a service through procrun is not affected), because batch files don't have a concept of local environment variables:

set LOGGING_CONFIG=foobar

on Windows exports the environment variable to all children processes. On UNIX:

LOGGING_CONFIG=foobar

is available only to the current shell, if you want to export it to children processes (and those created using exec) you must use:

export LOGGING_CONFIG

Remark: You should call the environment variable in a different way, e.g. <your application's name>_CONFIG_HOME> so that you are sure you don't overwrite a variable used by another application. Better even, use a Spring configuration property, so you'll be able to set CONFIG_HOME in more than one way.

Related