Is there a way to customize the jersey client request log level?
I am using jersey client
2.25 and I am logging my requests. I would like to enable or disable this login, preferably via a logging configuration file. I tried putting the files logging.properties
on the classpath, but that doesn't seem to have any effect.
Logger logger = Logger.getLogger("my logger");
LoggingFilter filter = new LoggingFilter(logger, true);
Client client = ClientBuilder.newClient().register(filter);
Please note, that is LoggingFilter
deprecated for this version but appears to be reverting in 2.5.1. Recommendation 2.25 is to use LoggingFeature
, but I note that this is not in 2.5.1
source to share
I'm not sure why you can't find LoggingFeature
the version jersey 2.25.1
.
Following is one way to do it using LoggingFeature
-
Client class
Create your client object and set the logging level to Fine
-
// Define it as a constant
Logger LOGGER = Logger.getLogger(YourClient.class.getName());
// Set logging level to FINE level for request/response logging
Feature feature = new LoggingFeature(LOGGER, Level.FINE, Verbosity.PAYLOAD_ANY,
LoggingFeature.DEFAULT_MAX_ENTITY_SIZE);
Client client = ClientBuilder.newBuilder().register(feature).build();
log_config.properties file -
Suppose below is the log config file -
handlers= java.util.logging.FileHandler
# Using this level, request/response logging can be controlled
.level= FINE
java.util.logging.FileHandler.pattern = ./logs/application.log
java.util.logging.FileHandler.limit = 5000
java.util.logging.FileHandler.count = 50
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
The main class is
Use this log config file in your client application -
LogManager.getLogManager().readConfiguration(new FileInputStream("./config/log_config.properties"));
Now yours is LoggingFeature
set up for level logging Fine
and your log config file is set up for level level logging as well Fine
, this means the request / response will be logged in the logs.
If you change the level in the log config file, suppose from Fine
to INFO
, your request / response will no longer be logged in the logs.
Edit Following are the maven dependencies I am using -
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25.1</version>
</dependency>
<!-- Dependency for JSON request/response handling in Jersey -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25.1</version>
</dependency>
Edit . For console logging, you just need to configure the print request / response at the level Fine
-
handlers= java.util.logging.ConsoleHandler
.level= FINE
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
Importing a report of required classes -
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Feature;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.logging.LoggingFeature.Verbosity;
source to share