6

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)?

ruichuan
  • 141
  • 1
  • 3
  • Why avoid XML/properties file? They are right way to manipulate log levels. Programming approach for this is bad because you would need to recompile to change log level!!!!! – appbootup Dec 18 '12 at 06:21
  • Unless you implement some way for the user to change the level themselves, through a GUI or TUI? You can't say one way or the other what the programmer SHOULD do unless you know HOW they're going to use it. – Fallso Nov 26 '14 at 16:41

1 Answers1

13

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);
Community
  • 1
  • 1
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136