如何将活动 spring 配置文件注入 logback

How to inject active spring profile into logback

我正在使用 spring 引导项目。

环境:

ch.qos.logback:logback-core:jar:1.1.5
ch.qos.logback:logback-classic:jar:1.1.5
org.springframework.boot:spring-boot-starter-logging:jar:1.3.3.RELEASE

在我的项目中,我使用 application.yml 的属性(application-dev.yml 和 application-production.yml)

由于 Logback Spring 扩展在 Spring 之前启动,我无法将 spring.profiles.active 注入 logback.xml 文件。

这是我的 logback.xml 文件的简化版本:

<configuration scan="true">

   <property name="LOG_PATH" value="/var/log/" />
   <property name="APP_NAME" value="xyz" />
   <property name="PROFILE" value="-${spring.profiles.active}" />
   <property name="CHARSET" value="utf-8" />
   <property name="PATTERN" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />

   <appender name="APP-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <file>${LOG_PATH}${APP_NAME}${PROFILE}.log</file>
      <encoder>
         <charset>${CHARSET}</charset>
         <Pattern>${PATTERN}</Pattern>
      </encoder>
   </appender>

   <logger name="a.b.c" level="INFO">
      <appender-ref ref="APP-FILE" />
   </logger>

   <root level="INFO">
      <appender-ref ref="APP-FILE"/>
   </root>

我正在寻找的个人资料是 属性 spring.profiles.active.

我的目标是在目录 /var/log 上有一个日志文件 xyz-devxyz-production 但我得到 xyz-spring.profiles.active_IS_UNDEFINED.log 而不是 course.

方法:

1 - 使用像这样的组件:

@Component
public class InitializationService implements ApplicationListener<ContextRefreshedEvent> {

// inject spring profile active into logback.xml

}

当然不起作用,因为 logback Spring 扩展在 Spring 启动应用程序之前启动。

2 - 在 logback.xml 上使用 属性 像这样

<file>${LOG_PATH}${APP_NAME}${PROFILE}.log</file>

结果为xyz-spring.profiles.active_IS_UNDEFINED.log

这是我为我的项目所做的。在你的 logback.xml

里面
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

<property resource="application.properties"/>

然后您可以使用 application.properties 文件中定义的属性。 ${MY-PROPERTY} 在我的 application.properties 中,我有一个 属性 作为日志文件的名称。

这不适用于 application.yml => YAML 属性 文件,因为 yaml 文件是在 logback init 之后解释的。

  • 由于您使用的是 Spring 引导,因此您可以定义 logging.file=blah.log。参见 official docs
  • 或者你可以使用 vanilla Logback 并传递一个系统变量:-DLOG_FILE=blah.log.

为什么你不应该使用配置文件:

您尝试做的不是 spring 配置文件的创建目的。它们需要在启动期间将 on/off bean 切换为 activate/deactivate 行为。 一般不推荐使用配置文件,因为您测试的内容与生产中的内容不同(尽管有时别无选择)。请参阅 the original announcement 注意事项 部分。

您使用配置文件的另一个问题是可以同时激活多个配置文件。创建配置文件是为了切换 on/off 不相关的小部分。例如。 spring.profiles.active=cache-off,perf-monitoring-on 或类似的东西。

综上所述,您仍然可以定义配置文件并在该配置文件中设置 logging.file=prod.log 属性。这仍然是 非常 糟糕的,而不是应该如何使用配置文件,但这样更好。

回答有点晚,但我通过将我的 "logback.xml" 文件重命名为 "logback-spring.xml" 成功地记录了 Spring 配置文件,并像这样访问配置文件(很多)简化版

<springProperty scope="context" name="ACTIVE_PROFILE" source="spring.profiles.active"/>

<appender name="GRAYLOG" class="com.github.pukkaone.gelf.logback.GelfAppender">

    <additionalField>environment=${ACTIVE_PROFILE}</additionalField>

</appender>

<root level="WARN">
    <appender-ref ref="GRAYLOG" />
</root>

好像"logback-spring.xml"可以提取个人资料信息

具体documentation is here.