从logback配置文件读取环境变量 [英] Read environment variables from logback configuration file
问题描述
我有这个logback.xml文件:
I have this logback.xml file:
<configuration debug="true" scan="true" scanPeriod="60 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${MY_HOME}/logs/mylog.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/my.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</Pattern>
</layout>
</appender>
<root level="TRACE">
<appender-ref ref="FILE"/>
</root>
</configuration>
和 $ {MY_HOME}
是一个定义系统变量( echo $ MY_HOME
在linux上显示正确的路径)。
And ${MY_HOME}
is a defined system variable (echo $MY_HOME
on linux shows the correct path).
事情是,logback似乎没有正确阅读它,它将日志存储在 MY_HOME_IS_UNDEFINED / logs / my.log
The thing is that logback doesnt seem to read it properly, it stores the logs under MY_HOME_IS_UNDEFINED/logs/my.log
我做错了什么?非常感谢!
What am I doing wrong? Thanks a lot!
编辑:我犯了一个错误,把OSC_HOME放在我的意思是MY_HOME。对不起,
I made a mistake and put OSC_HOME where I really meant MY_HOME. Sorry about that
推荐答案
与其他人说的相反, logback文档明确说明在替换期间,首先在本地范围内查找属性,在上下文范围中,第二个在系统属性范围第三,而 在OS环境第四和最后 。所以如果在环境中定义属性,logback会找到它。
Contrary to what the others have said, the logback documentation explicitly states that "During substitution, properties are looked up in the local scope first, in the context scope second, in the system properties scope third, and in the OS environment fourth and last". So if the property is defined in the environment, logback will find it.
在Eclipse中运行我的项目时,我遇到了同样的问题。如果这是您遇到的问题,可以通过运行配置 - >环境并将 MY_HOME
添加到环境变量中来修复。
I was having the same issue when running my project in Eclipse. If that's the issue you're having, it can be fixed by going to Run Configurations -> Environment and adding MY_HOME
to the environment variables.
不确定为什么默认情况下不加载本机环境。甚至有一个名为将环境附加到本地环境的选项,这对我来说似乎没有任何影响。
Not really sure why it isn't loading the native environment by default. There's even an option called "Append environment to native environment" which doesn't seem to have any effect for me.
这篇关于从logback配置文件读取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!