1

I have a logback.xml in the resources folder of my java web application which is running on Tomcat 7. Is there a way to change the debug level of some class of the application dynamically as the application is running?

The standard thing to do it is a normal java jar running from the CLI is to just edit logback.xml with say vim and after a while the log is updated. Is this possible with tomcat as I tried it and id does not seem to work.

Is there any better way?

idipous
  • 2,868
  • 3
  • 30
  • 45

1 Answers1

5

You can try like this is your application to change the level:

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

For specifying the package name as logger-name.

LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
Logger myPackageLogger = loggerContext.getLogger("com.mypackage");
myPackageLogger.setLevel(Level.INFO);
Garry
  • 4,493
  • 3
  • 28
  • 48
  • Is there a way to chose the specific package since my logback gives me this ability? To change for specific classes or even packages. – idipous Aug 05 '15 at 07:08
  • That did it. Excellent solution. Thanks! – idipous Aug 05 '15 at 22:45
  • Unfortunately it seems not to working at all. I have a spring rest controller and this import: `import org.slf4j.Logger; import org.slf4j.LoggerFactory;` and the I buitl a method with `ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); root.setLevel(ch.qos.logback.classic.Level.toLevel(arg));` , but with no effect, starting from logback.xnl with DEBUG, no changes to INFO have effect, system stays at DEBUG. – Stefano Scarpanti Jul 27 '17 at 08:47