4

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

Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
natke
  • 743
  • 1
  • 9
  • 21

1 Answers1

5

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;
Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
  • Are you able to post your pom or gradle build file, or just a list of your dependencies. I don't have LoggingFeature in my dependencies. – natke May 02 '17 at 23:14
  • Ah, you were talking about 2.25.1 not 2.5.1 - and I was confused about release ordering! Got it, thanks. – natke May 03 '17 at 21:58
  • @natk FYI, There is no 2.5.1 version of jersey released yet. Also, if this answer solved your question, then, accept this answer. – Vikas Sachdeva May 04 '17 at 12:13
  • Actually this is not working for me. I get the logs, and am able to configure the level with the logging.properties file if I use a LoggingFilter. When I switch to the LoggingFeature, I do not get the logs. – natke May 04 '17 at 16:17
  • @natk It is strange. Are you using same log properties file that I pasted above ? Can u show your log properties file, code snippet ? – Vikas Sachdeva May 05 '17 at 00:00
  • I am loggin to the console. This is my properties file. `handlers= java.util.logging.ConsoleHandler` `.level=FINE` `java.util.logging.ConsoleHandler.level = INFO` `java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter` `org.glassfish.jersey.client=FINER` `org.glassfish.jersey.client.handler=java.util.logging.ConsoleHandler` – natke May 05 '17 at 20:24
  • @natk I edited my answer with required console log configuration. The configuration you pasted, in that configuration, mainly you need to change `java.util.logging.ConsoleHandler.level` to `FINE` so that `FINE` level logs will print. Value of both `.level` and `java.util.logging.ConsoleHandler.level` should be same – Vikas Sachdeva May 06 '17 at 02:07
  • guys can you please add the imports for `Feature feature = new LoggingFeature(LOGGER, Level.FINE, Verbosity.PAYLOAD_ANY, LoggingFeature.DEFAULT_MAX_ENTITY_SIZE);` ? – MHosafy Jan 21 '18 at 07:43
  • @MHosafy I updated answer with the `import statements` as per `jersey 2.25.1` version. Not sure about the latest version. – Vikas Sachdeva Jan 24 '18 at 02:46