How can I disable logging in Restlet 2.0?

前端 未结 6 746
梦如初夏
梦如初夏 2021-01-21 16:20

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 a

相关标签:
6条回答
  • 2021-01-21 16:52

    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>
    
    0 讨论(0)
  • 2021-01-21 16:58

    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.

    0 讨论(0)
  • 2021-01-21 17:08

    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.

    0 讨论(0)
  • 2021-01-21 17:09

    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!

    0 讨论(0)
  • 2021-01-21 17:10

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

    0 讨论(0)
  • 2021-01-21 17:12

    I managed to disable the annoying Restlet logging by:

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

    ...

    0 讨论(0)
提交回复
热议问题