My
previous post on the same subject is one of the most popular articles in this blog, and since Glassfish still does not officially support slf4j, this update may be helpful to anyone hitting my blog via Google trying to improve their logging experience with Glassfish.
To redirect (almost) all log messages from Glassfish and your applications to the same log file and/or console, follow this recipe:
- Copy these JARs to [install-root]/lib/endorsed:
- jul-to-slf4j.jar
- slf4j-api.jar
- logback-classic.jar
- logback-core.jar
- Build a JAR containing your logback.xml configuration at root level and put it in the same place. [Update 26 Jul 2011: Actually, this is no longer required: Simply create a logback configuration file somewhere in your local file system and add the system property
-Dlogback.configurationFile=file:/path/to/my/logback.xml to your Glassfish configuration.]
- Define a new system property in the jvm-options section of your domain.xml:
-Djava.util.logging.config.file=${com.sun.aas.instanceRoot}/config/my_logging.properties
- Create the corresponding file my_logging.properties with the following contents:
handlers = org.slf4j.bridge.SLF4JBridgeHandler
com.sun.enterprise.server.logging.GFFileHandler.flushFrequency=1
com.sun.enterprise.server.logging.GFFileHandler.file=${com.sun.aas.instanceRoot}/logs/server.log
com.sun.enterprise.server.logging.GFFileHandler.rotationTimelimitInMinutes=0
com.sun.enterprise.server.logging.GFFileHandler.logtoConsole=false
com.sun.enterprise.server.logging.GFFileHandler.rotationLimitInBytes=2000000
com.sun.enterprise.server.logging.GFFileHandler.alarms=false
com.sun.enterprise.server.logging.GFFileHandler.formatter=com.sun.enterprise.server.logging.UniformLogFormatter
com.sun.enterprise.server.logging.GFFileHandler.retainErrorsStasticsForHours=0
When you restart Glassfish, you should only see two log messages in the old
java.util.logging format, all the rest goes via slf4j and logback to the appenders configured in your
logback.xml. The two remaining log messages are issued before Glassfish reads the
domain.xml configuration with the adapted configuration, so there is no easy way of avoiding them.
I have been using this approach on various Glassfish versions from 3.0.1 to 3.1-b33. It is rather ugly, but nonetheless effective.
All this may sound rather mysterious, so I'd better add some explanations:
jul-to-slf4j.jar is a bridge for redirecting
java.util.logging via the slf4j API to any logging backend of your choice (logback in this case). To make the redirection work, the bridge has to be visible to the classloader evaluating the logging configuration. Since Glassfish starts logging very early in its bootstrapping phase, the
lib/endorsed folder is the only place where the bridge has the desired effect.
Unfortunately, class file folders seem to be unsupported for endorsed libraries. Of course, it would be easier to simply put the
logback.xml file in
lib/endorsed/classes where you can directly edit it. But for a quick change to your logging configuration, you can always use an editor which supports editing ZIP/JAR file contents in place.
I created the contents of
my_logging.properties by trial and error, starting with a one-liner containing only the handler. This caused a couple of exceptions as Glassfish seemed to be missing the
GFFileHandler settings, so I copied them over from the original
logging.properties.
The same procedure should also work for other slf4j backends, so you could replace the logback JARs by a
log4j.jar.