如何在Spring Boot项目中将application.properties的环境属性转换为logback.groovy? [英] How to get Environment properties from application.properties into logback.groovy in Spring Boot project?

查看:746
本文介绍了如何在Spring Boot项目中将application.properties的环境属性转换为logback.groovy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将 application.properties / application.yml 中定义的属性注入到 中的 logback.groovy 脚本中> Spring Boot 项目。



我不能在Groovy脚本中注入 Environment ApplicationContext

是否有任何解决方法?



我不是 寻找像 System.getProperty('spring.profiles.active') $ b

src / main / resources / logback.groovy

  import org.springframework.core.env.Environment 

@Inject private Environment env; //这不起作用。如何让env在这里?
printlnspring.profiles.active:$ {env.getProperty('spring.profiles.active')}

appender(STDOUT,ConsoleAppender){
编码器(PatternLayoutEncoder){
pattern =%green(%d {HH:mm:ss.SSS})[%thread]%highlight(% - 5level)%cyan(%logger {36}) - %msg% (spring.profiles.active)?。equalsIgnoreCase(prod)){
root INFO ,[STDOUT,FILE]
} else {
root INFO,[STDOUT]
}

src / main / resources / application.yml

  --- 
spring:
profiles:
active:development


解决方案

logback.groovy需要非常早地进行评估,否则加载spring配置,实例化bean等的代码无法记录任何内容。这就是为什么 @Inject 无法工作。



我看到2个选项:


  1. 您可以轻松地在logback.groovy中加载 application.properties ,但采用Spring提供的所有配置覆盖机制并不是微不足道的事情

  2. 创建一个spring bean,用于在初始化时更改logback配置程序

另一种方法是外部化使用 -Dlogback.configurationFile = / path / to / config.groovy 生成logback配置。将配置文件放在众所周知的位置(如 / etc / my-app )可以轻松更改生产日志级别,而无需重新部署(或重新启动)。


Trying to inject properties defined in application.properties/application.yml into logback.groovy script in Spring Boot project.

I cannot Inject Environment or ApplicationContext into groovy scripts.

Are there any workarounds?

I am not looking for solutions like System.getProperty('spring.profiles.active')

src/main/resources/logback.groovy

import org.springframework.core.env.Environment

@Inject private Environment env; //this is not working. how to get env here?
println "spring.profiles.active : ${env.getProperty('spring.profiles.active')}"

appender("STDOUT", ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = "%green(%d{HH:mm:ss.SSS}) [%thread] %highlight(%-5level) %cyan(%logger{36}) - %msg%n"
    }
}

if(System.getProperty("spring.profiles.active")?.equalsIgnoreCase("prod")) {
    root INFO, ["STDOUT", "FILE"]
} else {
    root INFO, ["STDOUT"]
}

src/main/resources/application.yml

---
spring:
    profiles:
        active: development

解决方案

logback.groovy needs to be evaluated very early because otherwise the code for loading the spring configuration, instantiating beans, etc. could not log anything. That's why @Inject can't work.

I see 2 options:

  1. You could easily load application.properties in logback.groovy, but it's far from trivial to take all the configuration override mechanisms that spring provides into account
  2. Create a spring bean that changes the logback configuration programmaticaly when initialized

A different approach is to externalize the logback configuration on production with -Dlogback.configurationFile=/path/to/config.groovy. Putting the config file in a well known location (like /etc/my-app) makes it easy to change log levels in production without re-deploying (or even re-starting).

这篇关于如何在Spring Boot项目中将application.properties的环境属性转换为logback.groovy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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