如何在Spring Boot项目中将application.properties的环境属性转换为logback.groovy? [英] How to get Environment properties from application.properties into logback.groovy in Spring Boot project?
问题描述
application.properties / application.yml
中定义的属性注入到 中的 logback.groovy
脚本中> Spring Boot 项目。 我不能在Groovy脚本中注入 Environment
或 ApplicationContext
。
是否有任何解决方法?
我不是 寻找像 src / main / resources / logback.groovy src / main / resources / application.yml logback.groovy需要非常早地进行评估,否则加载spring配置,实例化bean等的代码无法记录任何内容。这就是为什么 @Inject 无法工作。 我看到2个选项: 另一种方法是外部化使用 -Dlogback.configurationFile = / path / to / config.groovy 生成logback配置。将配置文件放在众所周知的位置(如 / etc / my-app )可以轻松更改生产日志级别,而无需重新部署(或重新启动)。 Trying to inject properties defined in I cannot Inject Are there any workarounds? I am not looking for solutions like src/main/resources/logback.groovy src/main/resources/application.yml
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: 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屋! System.getProperty('spring.profiles.active')$
$ b
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]
}
---
spring:
profiles:
active:development
application.properties/application.yml
into logback.groovy
script in Spring Boot project. Environment
or ApplicationContext
into groovy scripts. System.getProperty('spring.profiles.active')
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"]
}
---
spring:
profiles:
active: development