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?

centic
  • 15,565
  • 9
  • 68
  • 125
Niel Niergard
  • 101
  • 1
  • 3
  • 1
    This annoys me too. If something is working properly it should shut up. – Sridhar Sarnobat Sep 08 '16 at 02:50
  • In case you are using [slf4](https://www.slf4j.org) head to that question to see how to properly configure the jul-to-slf4j bridge: https://stackoverflow.com/q/9117030/873282 – koppor Jun 21 '18 at 08:37

4 Answers4

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();
    }
}
John Smith
  • 3,493
  • 3
  • 25
  • 52
  • 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. – lochi Feb 09 '16 at 04:11
  • This made no difference for me either UNTIL I realized my logging was to `org.glassfish.jersey`. – Sridhar Sarnobat Sep 08 '16 at 03:05
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.

Heri
  • 2,114
  • 14
  • 13
  • 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. – Niel Niergard Dec 09 '10 at 17:42
  • 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. – Heri Dec 09 '10 at 19:18
  • 3
    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. – Niel Niergard Dec 10 '10 at 11:16
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.

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
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.

jeckhart
  • 571
  • 3
  • 10
  • 4
    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. – Niel Niergard Dec 09 '10 at 17:33
  • 2
    I've seen a few references to this method elsewhere. It definitely does not exist in LogManager -- http://docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/LogManager.html – David Blevins Mar 20 '12 at 17:37