Jetty: how to disable logging?
I am trying to em开发者_Go百科bed Jetty 6.1 in another program. Jetty is dumping INFO-log information and I need to turn it off. Is there a simple way to disable logging programmaticaly?
A more concise version of Jeff Chern's answer:
org.eclipse.jetty.util.log.Log.setLog(new NoLogging())
.
import org.eclipse.jetty.util.log.Logger;
public class NoLogging implements Logger {
@Override public String getName() { return "no"; }
@Override public void warn(String msg, Object... args) { }
@Override public void warn(Throwable thrown) { }
@Override public void warn(String msg, Throwable thrown) { }
@Override public void info(String msg, Object... args) { }
@Override public void info(Throwable thrown) { }
@Override public void info(String msg, Throwable thrown) { }
@Override public boolean isDebugEnabled() { return false; }
@Override public void setDebugEnabled(boolean enabled) { }
@Override public void debug(String msg, Object... args) { }
@Override public void debug(Throwable thrown) { }
@Override public void debug(String msg, Throwable thrown) { }
@Override public Logger getLogger(String name) { return this; }
@Override public void ignore(Throwable ignored) { }
}
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
This worked for me, but just make sure to call it before starting the server.
In Jetty Embedded you can try: org.mortbay.log.Log.setLog(null); Before call Server.
I've had success in creating a dummy logger that discards everything it's ever asked to log, and then passing it into Log.setLog(...)
.
E.g.
private static class DummyLogger implements Logger {
@Override
public String getName() {
return "DummyLogger";
}
@Override
public void warn(String msg, Object... args) {}
@Override
public void warn(Throwable thrown) {}
@Override
public void warn(String msg, Throwable thrown) {}
@Override
public void info(String msg, Object... args) {}
@Override
public void info(Throwable thrown) {}
@Override
public void info(String msg, Throwable thrown) {}
@Override
public boolean isDebugEnabled() {return false; }
@Override
public void setDebugEnabled(boolean enabled) {}
@Override
public void debug(String msg, Object... args) {}
@Override
public void debug(Throwable thrown) {}
@Override
public void debug(String msg, Throwable thrown) {}
@Override
public Logger getLogger(String name) {return this; }
@Override
public void ignore(Throwable ignored) {}
}
For reference, I used these packages:
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
instead of the mortbay ones, but they should probably behave the same (or at least similarly).
Set the StdErrLog
level to WARN
, before starting your server:
Properties p = new Properties();
p.setProperty("org.eclipse.jetty.LEVEL", "WARN");
org.eclipse.jetty.util.log.StdErrLog.setProperties(p);
Use this
org.eclipse.jetty.util.log.Log.setLog(new jettyNoLog());
Logger.getLogger(jettyNoLog.class.getName()).setLevel(Level.OFF);
org.eclipse.jetty.util.log.Log.getProperties().setProperty("org.eclipse.jetty.LEVEL", "OFF");
org.eclipse.jetty.util.log.Log.getProperties().setProperty("org.eclipse.jetty.util.log.announce", "false");
org.eclipse.jetty.util.log.Log.getRootLogger().setDebugEnabled(false);
Looking at the debugging page mentioned by Vinay:
Jetty uses the SLF4J logging infrastructure to bridge to commons-logging for JSP2.0. This means that commons-log messages are sent to the SLF4J interface.
We ship the Simple log implementation, which will only output INFO level and above messages to stderr.
However, you can replace the Simple log with any other SLF4J log implementation by removing the lib/jsp-2.0/slf4j-simple-1.0-rc5.jar and copying in the SLF4J impl of your choice. The core Jetty code has a soft dependency on SLF4J, meaning that if an SLF4J impl is found on the classpath at startup Jetty will direct all logging messages to it.
Alternatively, you can remove the SLF4J jars altogether and use commons-logging instead by copying in the commons-logging jar and a commons-logging compliant log impl, such as log4j, to the lib/ directory. However, if you do that, be aware that as the core Jetty code does not use commons-logging, it will log messages to stderr instead. Read on to learn how to get the stderr log mechanism to print DEBUG level messages.
So it's a matter of putting the right jar file. For instance, you could put a log4j compatible interface and configuring your log4j configuration file properly to mute completely jetty's logging statements or to be more verbose.
Before starting the server, call:
org.eclipse.jetty.util.log.StdErrLog logger = new org.eclipse.jetty.util.log.StdErrLog();
logger.setLevel(org.eclipse.jetty.util.log.StdErrLog.LEVEL_OFF);
org.eclipse.jetty.util.log.Log.setLog(logger);
This is basically the same as in the answer by arcuri82, but without the remaining single log line, which logger is used.
Add configuration
log4j.category.org.eclipse.jetty=error
in the file src/main/resources/log4j.properties
精彩评论