Switching off Jersey logging programmatically
Asked Answered
T

4

10

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?

Tricia answered 3/12, 2010 at 13:20 Comment(2)
This annoys me too. If something is working properly it should shut up.Motif
In case you are using slf4 head to that question to see how to properly configure the jul-to-slf4j bridge: https://mcmap.net/q/236058/-jul-to-slf4j-bridge/873282Guyette
A
3

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();
    }
}
Australorp answered 25/11, 2015 at 4:7 Comment(2)
This doesn't seem to work with Jersey 2.7. I have sl4j with logback in my jersey based application. Need to set jersey log level to WARN.Thor
This made no difference for me either UNTIL I realized my logging was to org.glassfish.jersey.Motif
T
2

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.

Tatouay answered 6/12, 2010 at 19:20 Comment(3)
So you suggest something like private final static Logger COM_SUN_JERSEY_LOGGER = Logger.getLogger( "com.sun.jersey" ); { System.out.println(COM_SUN_JERSEY_LOGGER); COM_SUN_JERSEY_LOGGER.setLevel( Level.SEVERE ); } I get null, so a NPE.Tricia
Yes, something like this. I cannot reproduce your example, and null does not make any sense, there must be something else wrong with it. What you're doing here is defining the usual static logger, so you cannot log at all at this namespace if Logger.getLogger("...") would ever return null.Tatouay
Ahhh. It works with an static initializer not an object initializer block. So: private final static Logger COM_SUN_JERSEY_LOGGER = Logger.getLogger( "com.sun.jersey" ); static { COM_SUN_JERSEY_LOGGER.setLevel( Level.SEVERE ); } does it.Tricia
M
1

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.

Motif answered 8/9, 2016 at 3:9 Comment(1)
Hmmmm, after re-reading the question more carefully maybe this won't work for the OP. But it worked for me.Motif
H
0

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.

Homicide answered 7/12, 2010 at 22:14 Comment(2)
LogManager.getLogManager().setLevel("com.sun.jersey", Level.SEVERE); gives an compiler error. You have meant LogManager.getLogManager().getLogger("com.sun.jersey").setLevel(Level.SEVERE);? If, then LogManager.getLogManager().getLogger("com.sun.jersey") will return null, so you get a NPE.Tricia
I've seen a few references to this method elsewhere. It definitely does not exist in LogManager -- docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/…Muumuu

© 2022 - 2024 — McMap. All rights reserved.