Niel Niergard
Niel Niergard

Reputation: 101

Switching off Jersey logging programmatically

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?

Upvotes: 10

Views: 6028

Answers (4)

Sridhar Sarnobat
Sridhar Sarnobat

Reputation: 25246

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.

Upvotes: 1

John Smith
John Smith

Reputation: 3615

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

Upvotes: 3

jeckhart
jeckhart

Reputation: 569

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.

Upvotes: 0

Heri
Heri

Reputation: 2124

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.

Upvotes: 2

Related Questions