以编程方式关闭 Jersey 日志记录

发布于 2024-10-06 01:03:58 字数 670 浏览 8 评论 0原文

在我的输出中,我有 JUL 记录来自 Jersey 的消息,就像这样

03.12.2010 14:14:55 com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:

以编程方式我想将它们关闭,所以我尝试了

Logger.getLogger("com.sun.jersey.api.core.PackagesResourceConfig").setLevel( Level.SEVERE );

Logger.getLogger("com.sun.jersey").setLevel( Level.SEVERE );

但这不起作用。

有趣的是,这个全局配置有效:

Logger.getLogger( "com" ).setLevel( Level.SEVERE );

或者

Logger.getLogger( "" ).setLevel( Level.SEVERE );

为什么?

In my output I have JUL logging messages from Jersey like this

03.12.2010 14:14:55 com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:

Programmatically I wanted to swich them off so I tried

Logger.getLogger("com.sun.jersey.api.core.PackagesResourceConfig").setLevel( Level.SEVERE );

or

Logger.getLogger("com.sun.jersey").setLevel( Level.SEVERE );

but this don't work.

Funny enough this global configuration works:

Logger.getLogger( "com" ).setLevel( Level.SEVERE );

or

Logger.getLogger( "" ).setLevel( Level.SEVERE );

WHY?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏辞 2024-10-13 01:03:58

我在这方面遇到了一些问题,所以我想我应该将代码与导入一起放入以避免混淆。我对此进行了测试,它可以在我的迷你网络服务器配置中运行。再次,为了完整性,我包含了整个服务器实现。

import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.net.httpserver.HttpServer;

public class WebServer {

    private HttpServer webServer;

    private final static Logger COM_SUN_JERSEY_LOGGER = Logger.getLogger( "com.sun.jersey" );

    static {
        COM_SUN_JERSEY_LOGGER.setLevel( Level.SEVERE );
    }

    public void start() throws IOException {
        System.out.println("Starting WebServer\n");
        webServer = createHttpServer();
        webServer.start();
        System.out.println(String.format("\nWeb Server started:" + "%sapplication.wadl\n", getURI()));
    }

    public void stop() {
        webServer.stop(0);
    }

    public static HttpServer createHttpServer() throws IOException {
        ResourceConfig rc = new PackagesResourceConfig("com.daford");
        return HttpServerFactory.create(getURI(), rc);
    }

    private static URI getURI() {
        return UriBuilder.fromUri("http://localhost/").port(4444).build();
    }
}

I had some problems with this so I thought I'd put code in with imports to avoid confusion. I tested this and it works in my mini webserver configuration. Again, I included the whole server implementation for completeness.

import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.net.httpserver.HttpServer;

public class WebServer {

    private HttpServer webServer;

    private final static Logger COM_SUN_JERSEY_LOGGER = Logger.getLogger( "com.sun.jersey" );

    static {
        COM_SUN_JERSEY_LOGGER.setLevel( Level.SEVERE );
    }

    public void start() throws IOException {
        System.out.println("Starting WebServer\n");
        webServer = createHttpServer();
        webServer.start();
        System.out.println(String.format("\nWeb Server started:" + "%sapplication.wadl\n", getURI()));
    }

    public void stop() {
        webServer.stop(0);
    }

    public static HttpServer createHttpServer() throws IOException {
        ResourceConfig rc = new PackagesResourceConfig("com.daford");
        return HttpServerFactory.create(getURI(), rc);
    }

    private static URI getURI() {
        return UriBuilder.fromUri("http://localhost/").port(4444).build();
    }
}
落花随流水 2024-10-13 01:03:58

Logger.getLogger() 返回的记录器是 WeakReference。因此,在您设置适当的级别后,它们可能会被垃圾收集,因为您不保留对其的任何引用,从而删除您的设置。

将记录器存储在具有适当范围的变量中以保留您的设置。

The loggers returned by Logger.getLogger() are WeakReferences. So directly after you set the appropriate level, they may get garbage collected, since you do not retain any reference to it, thus deleting your setting.

Store your logger in a variable with an appropiate scope to keep your settings.

怀里藏娇 2024-10-13 01:03:58

感谢上面的 John Smith,但这里是带有正确包名称的一行答案:

Logger.getLogger("org.glassfish.jersey").setLevel(Level.SEVERE);

不需要导入,这些来自 java.util。

让这成为你们所有人在 Unix 哲学 的沉默规则中的一课:当你没有什么可说的,闭嘴吧。

Credits to John Smith above, but here is the one line answer with the right package name:

Logger.getLogger("org.glassfish.jersey").setLevel(Level.SEVERE);

No imports necessary, these come from java.util.

And let this be a lesson to all of you in Unix Philosophy's rule of silence: when you have nothing erroneous to say, shut the hell up.

南风几经秋 2024-10-13 01:03:58

本质上每个记录器实例都有一个日志级别。但是,当您通过 Logger.getLogger() 检索记录器实例时,您很可能会创建新记录器的新实例。由于您没有保留对记录器的引用,它会立即超出范围并且您的更改会丢失。

Logger.getLogger("") 和 Logger.getLogger("com") 为您工作的原因是已经为这两个级别创建了持久记录器,这意味着您正在检索那些保持持久状态的记录器。

一个简单的修复方法是使用 JUL 中的 LogManager 类:

LogManager.getLogManager().setLevel("com.sun.jersey", Level.SEVERE);

有一个 有关 O'reilly 的精彩文章,应该会对您有所帮助。

Essentially each logger instance has a loglevel. However, when you retrieve a logger instance via Logger.getLogger() you are more than likely creating a new instance of a new logger. Since you aren't keeping a reference to the Logger it instantly goes out of scope and your change is lost.

The reason Logger.getLogger("") and Logger.getLogger("com") work for you is that persistent loggers are already being created for those two levels, which means you are retrieving those loggers which remain persistent.

One simple fix is to use the LogManager class in JUL:

LogManager.getLogManager().setLevel("com.sun.jersey", Level.SEVERE);

There is a great article on O'reilly that should help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文