Update slf4j Log Level Programmatically

Slf4j is an API that sits on top of a logging framework, e.g. Log4j 2, Logback or java.util.logging. So If you want to change the log level, the best way to do this is via the configuration of your logging implementation:

But sometimes, especially in tests it would be nice to see some logging output without changing global configuration files. Slf4j makes this very simple, just get the logger you want to change and set the log level:

Logger restClientLogger = (Logger) LoggerFactory.getLogger(SolrRestClient.class);
restClientLogger.setLevel(Level.DEBUG);

For the root logger you can use

Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.INFO);

 

 

Author: Thomas Lemmé

I am a Java Developer currently working at one of the top APM vendors as a Java agent developer.

Leave a Reply

Your email address will not be published. Required fields are marked *