从logback配置文件中读取环境变量
我有这个 logback.xml 文件:
<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 上显示正确的路径)。
问题是 logback 似乎无法正确读取它,它将日志存储在 MY_HOME_IS_UNDEFINED/logs/my.log
下
我做错了什么?多谢!
编辑:我犯了一个错误,将 OSC_HOME 放在我真正的意思是 MY_HOME 的位置。对此感到抱歉
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>
And ${MY_HOME}
is a defined system variable (echo $MY_HOME
on linux shows the correct path).
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!
EDIT: I made a mistake and put OSC_HOME where I really meant MY_HOME. Sorry about that
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
与其他人所说的相反,logback 文档明确指出“在替换期间,会查找属性首先是本地作用域,其次是上下文作用域,第三是系统属性作用域,第四个和最后一个操作系统环境”。因此,如果该属性在环境中定义,logback 就会找到它。
我在 Eclipse 中运行项目时遇到了同样的问题。如果这是您遇到的问题,可以通过转到运行配置 -> 来解决。环境并将
MY_HOME
添加到环境变量中。不太确定为什么它默认不加载本机环境。甚至还有一个名为“将环境附加到本机环境”的选项,它似乎对我没有任何影响。
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.
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 上下文。
logback.xml
LoggerStartupListener.java
There is an alternative way to read environment variables from config file. you can put your custom variables to logback context with context listener.
logback.xml
LoggerStartupListener.java
您可能指的是
MY_HOME
。在您的配置文件中,有OSC_HOME
的引用。具体参见Logback的变量替换规则。您可以将环境变量作为 Java 系统属性传递,然后 Logback 将执行变量替换。您可以在命令行中将其作为 JVM 选项传递。例如:
或者您可以在配置文件本身中定义 MY_HOME。
You perhaps mean
MY_HOME
. In your config file there is reference forOSC_HOME
. See Variable substitution rules of Logback for details.You can pass environment variable as a Java System property and then Logback will perform the variable substitution. You can pass this as JVM option in your command line. For example:
Or You can define MY_HOME in your config file itself.
如果您使用 Eclipse,则必须重新启动它才能获取环境变量,但您不能使用:File ->重新启动
相反,您实际上必须完全关闭它,然后再次启动。
If you're using Eclipse you have to restart it to pick up environment variables, but you can't use: File -> Restart
Instead you actually have to fully shut it down and then start it back up again.
事情实际上按照设计进行:logback 在进行变量替换时根本不读取环境变量。引用文档:
因此,要么使用上述解决方案之一,要么使用
OSC_HOME_IS_UNDEFINED
:)Things are actually working as designed: logback doesn't read environment variables at all when doing variable substitution. Quoting the documentation:
So, either use one of the mentioned solutions or get
OSC_HOME_IS_UNDEFINED
:)您可以使用标签在 logback.xml 中声明变量,而不是使用环境变量。
Instead of using environmental variables, you can use tag to declare variables in logback.xml.