Jetty: how to disable logging?

2019-02-06 02:11发布

I am trying to embed 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?

标签: logging Jetty
7条回答
一夜七次
2楼-- · 2019-02-06 02:21

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.

查看更多
疯言疯语
3楼-- · 2019-02-06 02:24
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.

查看更多
趁早两清
4楼-- · 2019-02-06 02:26

In Jetty Embedded you can try: org.mortbay.log.Log.setLog(null); Before call Server.

查看更多
虎瘦雄心在
5楼-- · 2019-02-06 02:27

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);
查看更多
太酷不给撩
6楼-- · 2019-02-06 02:29

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) { }
}
查看更多
老娘就宠你
7楼-- · 2019-02-06 02:30

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);
查看更多
登录 后发表回答