从 logback 配置文件中读取环境变量

Read environment variables from logback configuration file(从 logback 配置文件中读取环境变量)
本文介绍了从 logback 配置文件中读取环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 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} 是一个已定义的系统变量(linux 上的 echo $MY_HOME 显示正确的路径).

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 文档明确声明在替换期间,首先在本地范围内查找属性,然后在上下文范围内查找,然后在系统属性范围内查找第三个,在操作系统环境中查找第四个也是最后一个".所以如果该属性是在环境中定义的,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 中运行我的项目时遇到了同样的问题.如果这是您遇到的问题,可以通过转到 Run Configurations -> Environment 并将 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 配置文件中读取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)