is there a way of configuring the level of jersey client request logging?

java, jersey, jersey-2.0, jersey-client, logging

Solution

I am not sure why you are not able to find `LoggingFeature` in `jersey 2.25.1` version.

Below is one way of doing it using `LoggingFeature` -

Client Class -

Create your client object and set 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 configuration 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

Main Class -

Use this log configuration file in your client application -

LogManager.getLogManager().readConfiguration(new FileInputStream("./config/log_config.properties"));

Now, your `loggingfeature` is configured to log data at `FINE` level and your log configuration file is also configured for `FINE` level logging, it means request/response will be logged in logs.

If you change level in log configuration file, suppose from `FINE` to `INFO`, your request/response will no longer be logged in logs.

Edit Following are the maven dependencies which 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 below configuration for printing request/response at `FINE` level -

handlers= java.util.logging.ConsoleHandler

.level= FINE

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

Import Statement 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;

Problem

I am using `jersey client` 2.25 and I am logging my requests. I'd like an easy way to switch this logging on and off, preferably through a logging configuration file. I've tried putting a `logging.properties` files in the class path but this does not seem to have any effect. ``` Logger logger = Logger.getLogger("my logger"); LoggingFilter filter = new LoggingFilter(logger, true); Client client = ClientBuilder.newClient().register(filter); ``` Note: that the `LoggingFilter` is deprecated for this version but appears to come back in 2.5.1. The advice for 2.25 is to use `LoggingFeature` but I note that this is not present at all in 2.5.1

Original source