无法在 logback.xml 中使用 Spring 属性占位符 [英] Unable to use Spring Property Placeholders in logback.xml

查看:71
本文介绍了无法在 logback.xml 中使用 Spring 属性占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Logback 的 Spring Boot 控制台应用程序.所有属性(对于应用程序以及 Logback)都被外部化到类路径中的标准 application.properties 文件中.这些属性可以在应用程序本身中很好地获取,但不会在 logback.xml 文件中获取.看起来好像在 Spring Boot 启动之前处理了 logback.xml,因此不处理 EL 占位符.

I have a Spring Boot console app using Logback. All of the properties (for the app as well as for Logback) are externalized into a standard application.properties file in the classpath. These properties are picked up just fine in the app itself, but are not picked up in the logback.xml file. It appears as though the logback.xml is processed before Spring Boot fires up, therefore the EL placeholders are not processed.

以 FileNamePattern 为例,在 application.properties 中,我有这样的内容:

Using the FileNamePattern as an example, in the application.properties, I have something like this:

log.filePattern=/%d{yyyy/MM-MMMM/dd-EEEE}

在 logback.xml 中,我会有这个:

and in logback.xml, I'll have this:

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <FileNamePattern>${log.logDirectory}${log.filePattern}.log
    </FileNamePattern>
</rollingPolicy>

运行应用程序时,我会看到如下错误:

When running the app, I'll see errors such as:

ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:25 - 
RuntimeException in Action for tag [rollingPolicy]
java.lang.IllegalStateException: FileNamePattern
[log.logDirectory_IS_UNDEFINEDlog.filePattern_IS_UNDEFINED.log]
does not contain a valid DateToken

类似的代码在其他 Spring(不是 Spring Boot)应用程序中也能正常工作,所以我很好奇 Spring Boot 的行为是否有点不同.

Similar code works just fine in other Spring (not Spring Boot) apps, so I'm curious if Spring Boot just behaves a bit differently.

感谢@Gary 的回复!很高兴知道 Spring EL 和 Logback 的变量之间的区别......我以为是 Spring 负责为我解析这些变量.我确实有这个元素,但这让我开始思考.

Thanks for the reply @Gary! Good to know about the difference between Spring EL and Logback's variables...I had assumed it was Spring that was in charge of parsing those variables for me. I did have the element, but that got me to thinking.

我的 application.properties 文件在 jar 之外,所以 Logback 不知道在哪里可以找到它.通过将与 Spring 相关的属性保留在我的外部 application.properties 文件中,将与日志相关的属性移动到 application-internal.properties 文件中(位于 内部 jar),并将 Logback 指向 文件 (<property resource="application-internal.properties"/>) 一切正常!

My application.properties file was outside of the jar, so Logback had no idea where to find it. By keeping my Spring-related properties in my external application.properties file, moving the logging-related properties into an application-internal.properties file (located inside the jar), and pointing Logback to that file (<property resource="application-internal.properties" />) got everything working as expected!

推荐答案

${...} 不是 Spring 中的Spring EL";它们是属性占位符.

${...} is not "Spring EL" in Spring; they are property placeholders.

我认为您将 logback变量"与 Spring属性占位符"混淆了.

I think you are confusing logback "variables" with Spring "Property Placeholders".

他们只是碰巧使用相同的语法${...}.

They just happen to use the same syntax ${...}.

logback 对 Spring 属性占位符机制一无所知,反之亦然.您需要根据 logback 文档配置您的 logback 变量,而不是在 application.properties/application.yml 中,这是严格的 Spring(启动)概念.

logback knows nothing about the Spring property placeholder mechanism and vice-versa. You need to configure your logback variables according to the logback documentation and not in application.properties / application.yml which is strictly a Spring (boot) concept.

快速浏览一下 logback 文档后,添加

After a quick look at the logback docs, adding

<property resource="application.properties" />

logback.xml 应该可以工作.

这篇关于无法在 logback.xml 中使用 Spring 属性占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆