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 开发者_Python百科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?
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();
}
}
The loggers returned by Logger.getLogger()
are WeakReference
s. 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.
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.
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.
精彩评论