java Logback 变量替换无法在类路径上找到属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9935648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:57:40  来源:igfitidea点击:

Logback variable substitution could not locate property file on the classpath

javamavenlogginglogback

提问by scabbage

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.

我真的很喜欢 Logback 对登录数据库的支持。但是,我在使用 Logback 的变量替换功能时遇到了问题,更具体地说,是从类路径上的属性文件中。

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

我的参考: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:

所以我有一个多模块 Maven 项目。在我的 Web 模块(生成 .war 文件)中,我的 Logback conf 文件位于以下目录中:

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

My logback.xml file looks like this:

我的 logback.xml 文件如下所示:

<?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:

因此,当我启动 Tomcat 服务器时,我会像这样传入 ${env}:

-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.

看起来 Logback Joran 在定位属性文件时遇到了问题。

Can anyone let me know what I did wrong?

谁能让我知道我做错了什么?

回答by scabbage

As I said in the comments:

正如我在评论中所说:

Should be:

应该:

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

Not

不是

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