Monday, July 18, 2011

Configure Hibernate logging on Glassfish with SLF4J

Glassfish 3.1 ships with EclipseLink as the default JPA provider. One can, however, install Hibernate 3.5 using the Glassfish Update Tool, which is the easiest means of installing Hibernate on Glassfish. The installation also installs the SLF4J-JDK14 wrapper, that will enable all SLF4J logging calls to be redirected to the java.util.logging logging implementation used by Glassfish. One would, therefore, find that SLF4J calls would be transparently redirected to the Glassfish domain log, without a lot of configuration.

In order to configure logging for Hibernate on Glassfish, the JDK logger configuration in the logging.properties file of the Glassfish domain must be modified, to include any desired configuration changes. Simply put, one need not install log4j and the slf4j-log4j12 module in Glassfish and also provide log4j.properties to get Hibernate logging to work.

The following lines were added in the logging.properties file of the Glassfish domain (usually located in $GLASSFISH_DOMAIN/config) to enable various loggers created by to provide a more verbose output:



To understand how those entries work, it is sufficient to understand that Hibernate uses SLF4J as a logging facade, delegating the actual writing of the log records to an underlying logger like log4j, logback or the JDK logger. Glassfish uses the JDK logger as the logging implementation, and to enable the calls on the SLF4J logger, to create entries in the Glassfish domain logs, one must have the SLF4J-JDK14 module (slf4j-jdk14-x.y.z.jar) installed in the classpath. The SLF4J-JDK14 module serves to adapt all SLF4J method calls to the JDK Logger interface, allowing any application (in this case, Hibernate) using SLF4J to have the log records written by the JDK logger. The adapter transforms the levels used by SLF4J to the ones known to the JDK logger as follows:

SLF4J Level JDK Logger Level
TRACEFINEST
DEBUGFINE
INFOINFO
WARNWARNING
ERRORSEVERE

By inference, an invocation within Hibernate using Logger.trace() would be logged only if the JDK logger was configured to log messages at the FINEST level.

2 comments:

Turbo said...

Hmmm. Am I doing something wrong? Adding the logging to logging.properties didn't do anything. I installed Hibernate using update tool in glassfish, I could find slf4j-jdk14-1.5.8.jar
and slf4j-api-1.5.8.jar
in the lib dir of glassfish.

Is that all?

Vineet Reynolds said...

Well, I think you've hit some bug of some kind if the entries in the GlassFish domain's logging.properties are being ignored. I just hit one a couple of hours back, and I ended up creating a new GlassFish instance root.