How can I programmatically load custom named groovy config (logback-config.groovy
) config?
When I tries:
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(this.getClass().getResource("/logback-config.groovy"));
Got expt:
ch.qos.logback.core.joran.spi.JoranException: Problem parsing XML document. See previously reported errors.
What's wrong?
The issue here is that the JoranConfigurator
is specific only for XML configurations; for Groovy config files, logback uses a different class GafferConfigurator
. (Unfortunately, the logback documentation does not do a good job of advertising this fact.) But rather than reference the GafferConfigurator
directly, it would be better to use the ContextInitializer
class instead:
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
new ContextInitializer(loggerContext).configureByResource(this.getClass().getResource("/logback-config.groovy"));
This allows you to handle both XML and Groovy filetypes simultaneously, as well as take advantage of some of logback's built-in error checking.