How can I disable logging in Restlet 2.0?

我的未来我决定 提交于 2019-12-02 02:34:18

You first must configure SLF4J to intercept all of Restlet's calls to the java.util.logging APIs and map them into calls on the SLF4J facade's APIs instead. You simply do this by putting jul-to-slf4j.jar in your classpath, as @Bruno noted.

Second, you have to configure the SLF4J facade to forward its API calls on to a logging implementation to do the actual log message generation. To use the Logback logging implementation, you drop logback-classic.jar into your classpath

Finally, you have to configure your selected logging implementation. If you're using Logback, you can adjust the logging level of individual Loggers using XML.

We're using SLF4J with Restlet, and really like how SLF4J unifies all the different logging APIs into one. It's very nice.

Edit 29 April 2019: Be sure to check the comments from @NaftuliKay and @stempler.

This is what I did to disable the logging to STDERR and forward the logging to log4j. Basicly it's just a base class from which all restlets are based. It will install the log4j bridge, find the console handler and disable it. Works for me but might not be the best solution so if anyone has a better solution which does not require external config, please let me know.

public class CommonRestlet extends Application {

static {
    // Install logging bridge (JUL -> LOG4J)
    SLF4JBridgeHandler.install();

    // Disable annoying console logging of requests..
    Logger logger = Logger.getLogger("org.restlet");
    for (Handler handler : logger.getParent().getHandlers()) {
        // Find the console handler
        if (handler.getClass().equals(java.util.logging.ConsoleHandler.class)) {
            // set level to SEVERE. We could disable it completely with 
            // a custom filter but this is good enough.
            handler.setLevel(Level.SEVERE);
        }
    }
}
// Other common stuff here...
}

Dependency in pom.xml

    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>jul-to-slf4j</artifactId>
       <version>1.6.1</version>
    </dependency>
    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.6.1</version>
       <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
       </exclusions>
    </dependency>

I managed to disable the annoying Restlet logging by:

Component component = new Component();

// Disable damn log
component.setLogService(new org.restlet.service.LogService(false));

...

I tend to use the jul-to-slf4j bridge, but you can get other configurations, as described on the Restlet Wiki.

Something seems wrong here: If you have the org.restlet.ext.slf4j.jar, slf4j-api.jar, logback-core.jar, and logback-classic.jar linked in, you already have all you need to disable logging to sdout and stderr. Just modify the config file (logback.xml) so as not to append to them.

With this configuration, restlet is using slf4j "natively" and the usual slf4j configuration applies.

Also, the org.restlet.ext.slf4j.jar eliminates the need for the jul-to-slf4j.jar, which would incur a serious performance hit anyway.

Programmatic configuration

Engine.setRestletLogLevel(Level.OFF);

Details : http://restlet.com/learn/guide/2.2/editions/jse/logging

Since version 2.1, it is now possible to programmatically change the log level using the Engine#setLogLevel(…) and setRestletLogLevel(…) static methods. It is also possible to enable selective call logging by setting the Request#loggable property or by overriding the LogService#isLoggable(Request) method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!