简体   繁体   English

如何在 Restlet 2.0 中禁用登录?

[英]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.我只是想在我的项目中禁用 Restlet 的 stdout/stderr 日志记录,并通过 org.restlet.ext.slf4j 提供的 SLF4J 外观转发所有 Restlet 日志记录。 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.您首先必须配置 SLF4J 以拦截 Restlet 对 java.util.logging API 的所有调用,并将它们映射到 SLF4J 外观的 API 上的调用。 You simply do this by putting jul-to-slf4j.jar in your classpath, as @Bruno noted.正如@Bruno指出的那样,您只需将jul-to-slf4j.jar放在您的类路径中即可完成此操作。

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.其次,您必须配置 SLF4J 外观以将其 API 调用转发到日志记录实现以执行实际的日志消息生成。 To use the Logback logging implementation, you drop logback-classic.jar into your classpath要使用 Logback 日志记录实现,请将logback-classic.jar放入类路径

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 .如果您使用的是 Logback,则可以使用 XML调整各个 Logger 的日志记录级别。

We're using SLF4J with Restlet, and really like how SLF4J unifies all the different logging APIs into one.我们将 SLF4J 与 Restlet 一起使用,并且非常喜欢 SLF4J 如何将所有不同的日志 API 统一为一个。 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! 2019 年 4 月 29 日编辑:请务必查看@NaftuliKay 和@stempler 的评论,Restlet 在过去十年中无疑发生了变化!

This is what I did to disable the logging to STDERR and forward the logging to log4j.这就是我禁用 STDERR 日志记录并将日志记录转发到 log4j 所做的工作。 Basicly it's just a base class from which all restlets are based.基本上它只是一个基类,所有restlet都基于它。 It will install the log4j bridge, find the console handler and disable it.它将安装 log4j 网桥,找到控制台处理程序并禁用它。 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 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:我设法通过以下方式禁用了烦人的 Restlet 日志记录:

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详情: 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.从 2.1 版开始,现在可以使用 Engine#setLogLevel(…) 和 setRestletLogLevel(…) 静态方法以编程方式更改日志级别。 It is also possible to enable selective call logging by setting the Request#loggable property or by overriding the LogService#isLoggable(Request) method.还可以通过设置 Request#loggable 属性或覆盖 LogService#isLoggable(Request) 方法来启用选择性呼叫日志记录。

我倾向于使用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.这里似乎有问题:如果您链接了 org.restlet.ext.slf4j.jar、slf4j-api.jar、logback-core.jar 和 logback-classic.jar,那么您已经拥有了禁用日志记录所需的一切sdout 和 stderr。 Just modify the config file (logback.xml) so as not to append to them.只需修改配置文件 (logback.xml) 以免附加到它们。

With this configuration, restlet is using slf4j "natively" and the usual slf4j configuration applies.使用此配置,restlet“本地”使用 slf4j 并且通常的 slf4j 配置适用。

Also, the org.restlet.ext.slf4j.jar eliminates the need for the jul-to-slf4j.jar, which would incur a serious performance hit anyway.此外,org.restlet.ext.slf4j.jar 消除了对 jul-to-slf4j.jar 的需要,这会导致严重的性能下降。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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