0

I have an existing bundle that employs commons.logging. I would like to set the logLevel for the classes in the bundle during an eclipse plugin test. The under test does not itself have or require log4j as a bundle. Hence I need to know how to configure the jsr-47 logger visible within the bundle.

Jörn Guy Süß
  • 1,408
  • 11
  • 18

1 Answers1

0

JSR-47 shares a single log manager over the whole JVM. The client should initialize the logging properties from a resource:

InputStream resourceAsStream = getClass().getResourceAsStream("logging.properties");
LogManager.getLogManager().readConfiguration(resourceAsStream);

The resource has to handle the two common java logging gotchas:

  1. Set the root logger to allow finer output (otherwise things get squelched here)
  2. Set the package logger to allow finer output (otherwise things get squelched here)

It looks like this:

handlers = java.util.logging.ConsoleHandler
.level=FINEST
test=FINEST
java.util.logging.ConsoleHandler.level = FINEST

The target activates and uses commons.logging in the usual way:

log = LogFactory.getLog(getClass().getName();
log.debug("Badaboom");

The output then goes to the shared LogManager and then to the console.

Caveat: The configuration that is being read for the test could be changed dynamically at other locations in the code. If there is a smell of this happening, register a property change listener with the Manager and stop the code with a break-point to find the code changing the config.

Note: At this point it also becomes clear why java.util.logging is not well suited for OSGI: The same class and hence logger name may register by different bundles, overlaying settings.

Jörn Guy Süß
  • 1,408
  • 11
  • 18