Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable jetty 9 logging programmably?

I'm using embedded Jetty 9, with slf4j.jar in the path. By default, jetty logs tons of information.

I'd like to disable the logging, or at least change the log level to INFO. How can I do this in the program (i.e., without putting some xml configuration file)?

like image 530
ruichuan Avatar asked Dec 16 '12 00:12

ruichuan


1 Answers

There is no way using straight slf4j to set the Logger level, as slf4j is just a log facade/routing API.

You would need to rely on the underlying logging implementation to set the logging level on the namespace "org.eclipse.jetty" Such as:

  • If using slf4j-simple.jar, and the SimpleLogger, then you cannot set the level programmatically, only via System properties once the SimpleLogger is initialized, which is very early in the JVM.

  • If using slf4j-log4j.jar, use the Log4j specific techniques.

org.apache.log4j.LogManager.getLogger("org.eclipse.jetty").setLevel(Level.WARN);
  • If using slf4j-jdk14.jar, use the java.util.logging techniques.
java.util.logging.LoggerFactory.getLogger("org.eclipse.jetty").setLevel(Level.WARNING);
  • If using logback.jar, cast the slf4j Logger to the logback Logger and then set the Level.
final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger("org.eclipse.jetty");
if (!(logger instanceof ch.qos.logback.classic.Logger)) {
    return;
}
ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) logger;
logbackLogger.setLevel(ch.qos.logback.classic.Level.WARN);
like image 107
Joakim Erdfelt Avatar answered Sep 19 '22 22:09

Joakim Erdfelt