How to enable xml configuration in logback.groovy

I am writing a Spring Boot application and need the flexibility of managing my log configuration with Groovy. In Spring, download all I have to do is create src / main / resources / logback.groovy and it is automatically used for configuration.

What I would like to do starts with the Spring boot default boot configuration and simply overrides or changes the settings as needed.

If I used logback.xml instead of logback.groovy, I could do something like the following.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>

Is there something similar to the include line above that I can use in logback.groovy? I can look at the contents of base.xml and other files to see how to manually replicate them, but this will add some template code that I would like to avoid.

Thanks for any help you can provide.

+4
source share
2 answers

There is a tool that translates this file logback.xmlinto an equivalent one logback.groovy. In your case, this led to:

//
// Built on Thu Jul 16 09:35:34 CEST 2015 by logback-translator
// For more information on configuration files in Groovy
// please see http://logback.qos.ch/manual/groovy.html

// For assistance related to this tool or configuration files
// in general, please contact the logback user mailing list at
//    http://qos.ch/mailman/listinfo/logback-user

// For professional support please see
//   http://www.qos.ch/shop/products/professionalSupport

import static ch.qos.logback.classic.Level.DEBUG

logger("org.springframework.web", DEBUG)

When it comes to <include>, it is not supported for groovy configurations.

+4
source

How do you feel, instead of adding / overriding the configuration, are you loading it again?

You can create a Spring Bean that will see if the log file is in the location you specify, and if so, reload it with

Example

@Component
public class LoggingHelper {

    public static final String LOGBACK_GROOVY = "logback.groovy";

    @PostConstruct
    public void resetLogging() {
        String configFolder = System.getProperty("config.folder");
        Path loggingConfigFile = Paths.get(configFolder, LOGBACK_GROOVY);
        if (Files.exists(loggingConfigFile) && Files.isReadable(loggingConfigFile)) {

            LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
            ContextInitializer ci = new ContextInitializer(loggerContext);
            loggerContext.reset();
            try {
                ci.configureByResource(loggingConfigFile.toUri().toURL());
            } catch (JoranException e) {
                // StatusPrinter will handle this
            } catch (MalformedURLException e) {
                System.err.println("Unable to configure logger " + loggingConfigFile);
            }
            StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
        }
    }

}
+1
source

Source: https://habr.com/ru/post/1598147/


All Articles