Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 753
scabbage Avatar asked Mar 30 '12 00:03

scabbage


People also ask

Where do I put the Logback file?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.

How do I setup a Logback XML file?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.

How do I enable debug in Logback?

debug=true to enable debugging of the logback setup. Unfortunately, there is no way to enable debugging via a System property. You have to use <configuration debug="true"> in the logback.


1 Answers

As I said in the comments:

Should be:

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

Not

<property file="${env}.properties"/>
like image 190
scabbage Avatar answered Oct 04 '22 20:10

scabbage