Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include xml configuration in logback.groovy

I'm writing a Spring Boot app and need the flexibility of controlling my logback configuration using Groovy. In Spring Boot 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 though is start with Spring Boot's default logback configuration, and just override or modify settings as needed.

If I were using 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 it's other included files to see how to replicate this manually, but it would add a bit of boilerplate code I'd like to avoid.

Thanks for any help you can provide.

like image 329
balduncle Avatar asked Jul 16 '15 03:07

balduncle


People also ask

How do I setup a Logback XML file?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.

How do I use Groovy Logback?

When Logback starts, it searches for a logback. groovy file in the classpath to configure itself. If you store the file in a different location outside the classpath, you will need to use the logback. configurationFile system property to point to the location, like this.

How do I use application properties in Logback XML?

xml , you can use <springProperty> to access properties from Spring's environment including those configured in application. properties . This is described in the documentation: The tag allows you to surface properties from the Spring Environment for use within Logback.

How to configure Logback with Logback XML file?

Logback with logback.xml Configuration Example 1 Project structure. In the following tutorial we will show you how to configure logback with logback.xml file. ... 2 Maven Dependency 3 Example log4j2.xml file. Create a logback.xml file and put it into the resources folder. ... 4 Testing log4j2 configuration 5 Output. ... 6 References. ...

Did You Know you can configure Logback with Groovy?

Logback is one of the most popular logging frameworks for Java, but did you know you could configure it using Groovy? This guide walks you through the whole process. Join the DZone community and get the full member experience. Logback is designed to be faster and have a smaller memory footprint than the other logging frameworks around.

How do I configure Logback in Spring Boot?

The Logback Configuration File. For Logback configuration through XML, Logback expects a Logback.xml or Logback-test.xml file in the classpath. In a Spring Boot application, you can put the Logback.xml file in the resources folder.

How do I check if Logback is working or not?

In the root element, you can set the debug=true attribute to inspect Logback’s internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute. When you do so, Logback scans for changes in its configuration file.


2 Answers

There's an online tool that translates given logback.xml file to equivalent logback.groovy. In your case it resulted in:

//
// 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's not supported for groovy configurations.

like image 147
Opal Avatar answered Oct 22 '22 20:10

Opal


How do you feel about instead of adding/overriding your configuration, you reload it again?

You can create a Spring Bean that will see if a logback file is in a location you specify, and if it is, reload using that file

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);
        }
    }

}
like image 38
rince Avatar answered Oct 22 '22 18:10

rince