访问 logback.xml 中的应用程序属性 [英] Accessing the application properties in logback.xml

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

问题描述

是否可以在 log back xml 中访问 spring boot 的应用程序属性.

Is it possible to access application properties of spring boot in log back xml.

application.properties

dummy.property=hello

logback.xml

${dummy.property}

这不起作用.

有没有人知道它是否可行.

Does any one have idea if it will work.

推荐答案

如果您将配置文件命名为 logback-spring.xml,而不是 logback.xml,您可以使用 访问 Spring 环境中的属性,包括在 application.properties 中配置的属性.这是文档中描述的:

If you name your configuration file logback-spring.xml, rather than logback.xml, you can use <springProperty> to access properties from Spring's environment including those configured in application.properties. This is described in the documentation:

该标签允许您从 Spring Environment 中显示属性以在 Logback 中使用.如果您想从 logback 配置中的 application.properties 文件访问值,这会很有用.该标签的工作方式与 Logback 的标准标签类似,但不是指定直接值,而是指定属性的来源(来自环境).如果您需要将属性存储在本地作用域以外的其他地方,您可以使用 scope 属性.

The tag allows you to surface properties from the Spring Environment for use within Logback. This can be useful if you want to access values from your application.properties file in your logback configuration. The tag works in a similar way to Logback’s standard tag, but rather than specifying a direct value you specify the source of the property (from the Environment). You can use the scope attribute if you need to store the property somewhere other than in local scope.

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
    <remoteHost>${fluentHost}</remoteHost>
    ...
</appender>

这篇关于访问 logback.xml 中的应用程序属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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