简体   繁体   中英

How to get Environment properties from application.properties into logback.groovy in Spring Boot project?

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).

Sorry to resurrect this thread but while since this is thread is what I found while looking for a solution, I wanted to share a workaround.

I have used a Custom converter and conversion rule to pull out properties in classpath resource (ie 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 'patternExpression' used in desired appender

and my custom 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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM