How to get environment properties from application.properties in logback.groovy in spring boot project?

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

I cannot embed Environment or ApplicationContext in groovy scripts.

Are there any workarounds?

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

Src / core / 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 / core / resources / application.yml

 --- spring: profiles: active: development 
+7
spring spring-boot environment-variables groovy logback
source share
2 answers

logback.groovy needs to be evaluated very early, because otherwise the code to load the spring configuration, beans instance, etc. couldn't write anything. This is why @Inject cannot work.

I see 2 options:

  • You can easily load application.properties into logback.groovy, but it’s far from trivial to use all the configuration override mechanisms that spring considers
  • Create a spring bean that changes the programmaticaly logback configuration on initialization

Another approach is to externalize the log configuration during production with -Dlogback.configurationFile = / path / to / config.groovy. Placing the configuration file in a well-known location (for example, / etc / my-app) makes it easy to change log levels during production without redeployment (or even re-launch).

+2
source share

Sorry to resurrect this thread, but so far this is the thread I found while looking for a solution, I would like to share a workaround.

I used a custom converter and conversion rule to pull properties in a class path resource (e.g. application.properties):

In logback.groovy:

 conversionRule('springApplicationName', CustomSpringApplicationNameConverter) def patternExpression = '%green([%d{YYYY-MM-dd HH:mm:ss.SSS}]) [%t] %highlight(%-5level) %magenta([%springApplicationName]) - %cyan(%logger{36}) -- %msg%n' 

and then the "patternExpression" used in the desired appender

and my own converter class (in groovy):

 class CustomSpringApplicationNameConverter extends ClassicConverter { @Override String convert(ILoggingEvent event) { ClassPathResource classPathResource = new ClassPathResource('application.properties') Properties applicationProperties = new Properties() applicationProperties.load(classPathResource.inputStream) String springApplicationName = applicationProperties.getProperty('spring.application.name') if (!springApplicationName) { System.err.println('Could not find entry for \'spring.application.name\' in \'application.properties\'') } springApplicationName } } 
0
source share

All Articles