java 如何让 logback 读取名称为变量的属性文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11105652/
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-31 03:49:22  来源:igfitidea点击:

How do I make logback read a properties file which name is a variable?

javalogginglogback

提问by Wojtek B.

I am using http://logback.qos.ch/

我正在使用http://logback.qos.ch/

I am running my java process with a parameter for example -Dproperties.url=myappproperties-production.propertiesor -Dproperties.url=myappproperties-development.propertiesdepending on the environemnt it is run in.

例如,我正在使用参数运行我的 java 进程,-Dproperties.url=myappproperties-production.properties或者-Dproperties.url=myappproperties-development.properties取决于它运行的环境。

Problem: how to make logback pick up my properties file?

问题:如何让 logback 获取我的属性文件?

If the properties file name is static I would do (works fine):

如果属性文件名是静态的,我会这样做(工作正常):

<configuration>
    <property resource="myappproperties-development.properties" />
    (...)
</configuration>

But I need something that is dynamic (this does not work):

但我需要一些动态的东西(这不起作用):

<configuration>
    <property resource="${properties.url}" />
    (...)
</configuration>

采纳答案by Ceki

The value of the resource file can be a property itself. In other words,

资源文件的值可以是属性本身。换句话说,

<configuration debug="true">
    <property resource="${properties.url}" />
    (...)
</configuration>

should work. BYW, set the debugattribute of <configuration>element to trueto see logback's internal messages on the console. Which version of logback are you using?

应该管用。BYW,将元素的debug属性设置为在控制台上查看logback的内部消息。您使用的是哪个版本的 logback?<configuration>true

回答by Damien Gallagher

How would you handle this if the properties url value was in inverted commas? e.g.

如果属性 url 值在引号中,您将如何处理?例如

-Dproperties.url="myappproperties-production.properties"

I just tried this and it didnt work

我刚试过这个,但没有用

The reason I ask is because the latest version of the Amazon AMI in Amazon Elastic Beanstalk adds inverted commas around a property value - even if a user doesnt specify this

我问的原因是因为 Amazon Elastic Beanstalk 中最新版本的 Amazon AMI 在属性值周围添加了引号 - 即使用户没有指定这个

回答by Sohail

This is the Spring recommended way:

这是Spring推荐的方式:

<configuration>

    <springProfile name="local">
        <!-- configuration to be enabled when the "local" profile is active -->
        <property resource="application-local.properties" />
    </springProfile>

    <springProfile name="dev">
        <!-- configuration to be enabled when the "dev" profile is active -->
        <property resource="application-dev.properties" />
    </springProfile>

</configuration>

Reference docs: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

参考文档:https: //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html