Spring-boot: Access environment properties from logback.groovy

Created on 29 Jan 2016  路  15Comments  路  Source: spring-projects/spring-boot

in a nutshell, the equivalent of springProperty(in logback-spring.xml) ; because i thought spring could be XML-free ;)

declined

Most helpful comment

+1 - I need to pass the spring.application.name to my central logging system. I cant see a way to do that without having something similar to springProperty

All 15 comments

The XML support is currently provided in LogbackLoggingSystem.configureByResourceUrl and works by plugging in SpringBootJoranConfigurator instead of Logback's default JoranConfigurator.

It looks like Groovy support is provided by GafferConfigurator and I'm not sure how we'd hook into that.

In an ideal world, how would you like to see environment properties accessed from logback.groovy?

I would expect a variable $environment or even $springProperties

I can't see an easy way to offer that unfortunately. Perhaps someone with more knowledge of Logback will have some suggestions.

Would it be possible to access environment variables from logback.groovy with vanilla groovy?
Something like this maybe:

def env = System.getenv()
def profiles = env["SPRING_PROFILES_ACTIVE"]

@kiview Those are environment variables from the OS. We're interested in getting properties from Spring Framework's Environment abstraction.

+1 - I need to pass the spring.application.name to my central logging system. I cant see a way to do that without having something similar to springProperty

+1 - same situation as @mohdaliiqbal

+1

As a workaround, I have used a Custom converter and conversion rule to pull out properties in classpath respurce (i.e. 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
    }

+1

+1

+1

+1

UPDATE: another workaround for this is to create a filter (wire in spring's filter chain), and inject property into MDC https://logback.qos.ch/manual/mdc.html. I haven't implemented this yet, but will attempt it soon

I'm going to close this issue since there's no obvious way for us to implement this. If anyone has success and wishes to contribute a PR we'll certainly consider it.

Was this page helpful?
0 / 5 - 0 ratings