简体   繁体   中英

How can I disable logging in Restlet 2.0?

I simply want to disable Restlet's logging to stdout/stderr in my project and forward all Restlet logging through the SLF4J facade provided by org.restlet.ext.slf4j. Is there an easy way to do this?

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, Restlet has doubtless changed over the last decade!

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

...

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.

我倾向于使用jul-to-slf4j桥接器,但您可以获得其他配置,如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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM