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

问题描述

我有一个使用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作为示例。属性,我有这样的东西:

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变量之间的区别......我原以为是负责为我解析这些变量。我确实拥有该元素,但这让我思考。

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没有想法在哪里找到它。通过在我的外部application.properties文件中保留与Spring相关的属性,将与日志记录相关的属性移动到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.

我认为你将回弹变量与SpringProperty Placeholders混淆。

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 Property Place的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 01:04