November 18, 2013

How to configure JDK logging for Jersey 2 tests

Jersey uses JDK Logging API to log messages.

One way how to configure JDK Logging is a properties file. In such case you have to set JVM system property java.util.logging.config.file to reference the properties file.

Initially the logging configuration file logging.properties can look:

#All attributes details
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.SimpleFormatter.format=%4$-7s [%3$s] %5$s%6$s%n

#All log level details
.level=INFO
org.glassfish.jersey.level=CONFIG
org.glassfish.jersey.tracing.level=FINEST

The configuration explained:

  • handlers=java.util.logging.ConsoleHandler - log messages are written to System.err
  • java.util.logging.ConsoleHandler.level=FINEST - allow to log FINEST message;
    default is INFO so more detailed messages are suppressed by default
  • java.util.logging.SimpleFormatter.format - example of simple and single line format - just level, logger name and message;
    Note: It just works with Java 7.
  • org.glassfish.jersey.level=CONFIG - example of per-package level configuration;
    log all SEVERE, WARNING, INFO and CONFIG level Jersey messages
  • org.glassfish.jersey.tracing.level=FINEST - example of detailed level configuration of specified package
And as mentioned it is necessary to set system property java.util.logging.config.file. Do you want to see log messages during unit testing (e.g. using maven)? There are two options:

  • run tests in new JVM instance (fork option) and configure the system property in such maven plugin
  • enhance maven command line parameter and add the
    -Djava.util.logging.config.file=... property

For example, run test application using Jetty server for test module tracing-support:

~/jersey2/tests/integration/tracing-support$ mvn compile jetty:run -Djava.util.logging.config.file=src/test/resources/logging.properties

-that's all folks