Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log level in Jerseys new LoggingFeature

Tags:

java

jersey

I am trying to log with Jersey 2.23. Since this version, the class LoggingFilter is deprecated, as one can read for example here: https://jersey.java.net/documentation/latest/logging_chapter.html. So I have to use LoggingFeature instead. What did not work was the register method of ResourceConfig as it is explained in this documentation. But in the end the property method worked:

client.property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_SERVER, "WARN");

This prints every message as a warning. Unfortunately it is documented nowhere (at least I couldn't find anything) which values are allowed. Obviously it has to be a String, because I get a log message that there is no way to transform the value into a String, when I try anything else than a String. Now I want to log this messages with level TRACE and I can't find a suiting String to achieve this. "TRACE" and "FINE" did not work for example, in these cases nothing is logged. I have to mention that I use Log4j2 together with the Slf4jBridgeHandler because Jersey uses JUL.

like image 557
Jogi Avatar asked Jun 22 '16 09:06

Jogi


1 Answers

I struggled with this myself for several hours before finally uncovering the "mystery" today.

It is somewhat counter-intuitive but the level you are setting with the LOGGING_FEATURE_LOGGER_LEVEL_SERVER is actually the minimum level the server's logger must be set to in order to print your logs. I had assumed, based on the name, that this was setting the actual logger level -- implying that setting it to FINER or FINEST would produce more output. Instead it is simply "turning off" logging unless a specific level is met.

As an example, if you set it to WARNING then you will see the logs as long as the server/client is set to print at least WARNING level. The levels as defined by java.util.logging are:

SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value)

So by setting it to WARNING (the literal WARN does not work for me in 2.23.1) you will see the logs because, by default, logging is typically at INFO level.

An alternate solution is to change the default logging level in your logging.properties file which is typically in $JAVA_HOME/jre/lib/logging.properties. You could, for example, make the following changes to the file and you would no longer need to set any special properties in your code:

java.util.logging.ConsoleHandler.level = FINEST  
org.glassfish.jersey.test.JerseyTest.level = FINEST

The obvious disadvantage to this is that it will effect anything you run from this JDK/JRE. There are ways you can override this standard location and use an alternate logging.properties file but it depends on how you're executing your code so I will let you research that based on your circumstances.

One example would be this thread which explains how to do it when using Maven: Logging level under maven surefire

like image 96
Greg R Avatar answered Nov 07 '22 05:11

Greg R