July 26, 2015 Leave a comment
This article explains how to configure SLF4J logging framework for your Netbeans RCP modules.
This article was originally published on techtavern.wordpress.com
Netbeans RCP favors Java standards for a small memory footprint and to spare us from learning new APIs. For example, Netbeans uses java.util.logging instead of creating its own logging API. However, most developers prefer leaner logger APIs like SLF4J.
It happens that Netbeans kept only the API and replaced most of the java.util.logging original implementation. The startup module replaces the log manager. A custom handler writes messages into the platform messages file using a custom pattern. Another handler repeats messages on the platform messages TopComponent.
A third handler identifies error and warning messages with exceptions and presents them as a error dialogs. The Dialog API deprecated error dialogs in order to “encourage” java.util.logging.
Even worse, the platform classloader prevents the slf4j bridges from replacing the java.util.logging framework on platform and third party modules. Module importing SLF4J themselves will behave inconsistently to other modules and to platform defaults.
Instead of trying to bypass the Platform logging features, we opted for SLF4J redirecting messages to java.util.logging, which is then handled by the Platform on its custom way.
We created a library wrapper module containing slf4j-api-x.y.z.jar and slf4j-sdk14-x.y.z.jar. No coding is required. Only the org.slf4j packages needs to be set ‘public’.
This combination preserves the platform logging features and conventions, while enabling our modules with the SLF4J better API.
Netbeans RCP Toys
The solution described above is provided by the Netbeans RCP Toys project at Github. Look for the Logger-Toys suite, that contains the Logger-SLF4J-Libs module.