Saturday, October 16, 2010

Logback: Change root logger level programmatically

A couple of years ago, I wrote about how it is possible to change log4j logging levels using JMX. I'm now using logback, which is intended to be the successor of log4j and provides several advantages over it.

The following code snippet can be used to change the root logger's logging level in logback:

import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.DEBUG); //change to debug

1 comment:

  1. If i do something like this

    val funch(packageName : String)
    Logger root = (Logger)LoggerFactory.getLogger(packageName);
    root.setLevel(Level.DEBUG); //change to debug

    Will this add a logger if the logger has not been defined inside logback.xml and make the changes ? Because i have no logger defined but still this works . Dont know y . Any clue ?


Note: Only a member of this blog may post a comment.