Search code examples
javamavenlogginglogback

Logback variable substitution could not locate property file on the classpath


I really like Logback's support to log into a DB. However, I'm having trouble using Logback's variable substitution feature, more specifically, from a property file on the classpath.

My reference: http://logback.qos.ch/manual/configuration.html#variableSubstitution

So I have a multi-module Maven project. In my web module (which generates a .war file), I have my Logback conf files in the following dir:

src/main/resources
- logback.xml
- local.properties
- dev.properties

My logback.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property file="${env}.properties"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%X{messageId}] %-5level %logger{0} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
            <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <driverClass>${logback.db.driverClassName}</driverClass>
                <jdbcUrl>${logback.db.url}</jdbcUrl>
                <user>${logback.db.user}</user>
                <password>${logback.db.password}</password>
            </dataSource>
        </connectionSource>
    </appender>

    <root level="debug">
        <appender-ref ref="DB" />
    </root>
</configuration>

So when I start my Tomcat server, I would pass in the ${env} like this:

-Denv=local

However, I got the following error when I brought up the server:

17:45:22,782  WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0 DriverManagerDataSource:107 - Could not load driverClass logback.db.driverClassName_IS_UNDEFINED
java.lang.ClassNotFoundException: logback.db.driverClassName_IS_UNDEFINED
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)

It looks like Logback Joran was having trouble locating the property file.

Can anyone let me know what I did wrong?


Solution

  • As I said in the comments:

    Should be:

    <property resource="${env}.properties"/>
    

    Not

    <property file="${env}.properties"/>