Glassfish uses per default java.util.logging (JUL) for logging. Third-party frameworks are often using different logging frameworks. For my company and private projects I’m using slf4j for all kind of logging. I found some rare resources on the web how to consolidate the different logging frameworks to just have one single logging file.

slf4j is delivery with a bridge to redirect JUL logging to slf4j. The steps to enable slf4j with log back are:

You need these 4 artifacts:

  • slf4j-api-1.5.10.jar
  • jul-to-slf4j-1.5.10.jar
  • logback-core-0.9.20.jar
  • logback-classic-0.9.20.jar

You can download either from the slf4j and log back websites, or grab them from the maven repository

Step 1:

Copy these jar’s to the $GF_INSTALL/glassfish/lib/endorsed

Step 2:

Create a logback.xml at $GF_INSTALL/glassfish/domains/domain1/config containing:

<configuration debug="true" scan="true">
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file</tmp/gf_server.log>/file>
    <append<true>/append>
    <encoder>
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{52} - %msg%n</Pattern>
    </encoder>
  </appender>
  <root>
    <level value="INFO" />
    <appender-ref ref="FILE" />
  </root>
</configuration>

Step 3:

Modify $GF_INSTALL/glassfish/domains/domain1/config/logging.properties

handlers=org.slf4j.bridge.SLF4JBridgeHandler
com.sun.enterprise.server.logging.GFFileHandler.flushFrequency=1
com.sun.enterprise.server.logging.GFFileHandler.file=/tmp/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
com.sun.enterprise.server.logging.GFFileHandler.level=ALL
.level=INFO

Step 3:

Modify $GF_INSTALL/glassfish/domains/domain1/config/domain.xml and add two JVM options at the server-config JVM section:

-Djava.util.logging.config.file=${com.sun.aas.instanceRoot}/config/logging.properties
-Dlogback.configurationFile=file:///${com.sun.aas.instanceRoot}/config/logback.xml

Now you can restart Glassfish and you should only see two entries in the server.log:

16.06.2012 17:19:40 com.sun.enterprise.admin.launcher.GFLauncherLogger info
INFO: JVM invocation command line:
-XX:+UnlockDiagnosticVMOptions
-XX:PermSize=64m
-XX:MaxPermSize=192m
-XX:NewRatio=2
-Xmx512m
-d32
-client
-javaagent:/srv/glassfish-3.1.2-logback/glassfish/lib/monitor/flashlight-agent.jar
-Dfelix.fileinstall.disableConfigSave=false
-Djavax.net.ssl.keyStore=/srv/glassfish-3.1.2-logback/glassfish/domains/domain1/config/keystore.jks
-Djava.awt.headless=true
-Dfelix.fileinstall.poll=5000
-Djava.endorsed.dirs=/srv/glassfish-3.1.2-logback/glassfish/modules/endorsed:/srv/glassfish-3.1.2-logback/glassfish/lib/endorsed
-Dfelix.fileinstall.bundles.startTransient=true
-Djavax.net.ssl.trustStore=/srv/glassfish-3.1.2-logback/glassfish/domains/domain1/config/cacerts.jks
-Djava.util.logging.config.file=/srv/glassfish-3.1.2-logback/glassfish/domains/domain1/config/logging.properties
-Dcom.sun.enterprise.security.httpsOutboundKeyAlias=s1as
-DANTLR_USE_DIRECT_CLASS_LOADING=true
-Djava.security.auth.login.config=/srv/glassfish-3.1.2-logback/glassfish/domains/domain1/config/login.conf
-Dgosh.args=--nointeractive
-Dosgi.shell.telnet.maxconn=1
-Djdbc.drivers=org.apache.derby.jdbc.ClientDriver
-Dfelix.fileinstall.dir=/srv/glassfish-3.1.2-logback/glassfish/modules/autostart/
-Dosgi.shell.telnet.port=6666
-Djava.security.policy=/srv/glassfish-3.1.2-logback/glassfish/domains/domain1/config/server.policy
-Dfelix.fileinstall.log.level=2
-Dcom.sun.enterprise.config.config_environment_factory_class=com.sun.enterprise.config.serverbeans.AppserverConfigEnvironmentFactory
-Dosgi.shell.telnet.ip=127.0.0.1
-Dcom.sun.aas.instanceRoot=/srv/glassfish-3.1.2-logback/glassfish/domains/domain1
-Dcom.sun.aas.installRoot=/srv/glassfish-3.1.2-logback/glassfish
-Djava.ext.dirs=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/jre/lib/ext:/srv/glassfish-3.1.2-logback/glassfish/domains/domain1/lib/ext
-Dfelix.fileinstall.bundles.new.start=true
-Dlogback.configurationFile=file:////srv/glassfish-3.1.2-logback/glassfish/domains/domain1/config/logback.xml
16.06.2012 17:19:41 com.sun.enterprise.admin.launcher.GFLauncherLogger info
INFO: Successfully launched in 16 msec.

all the rest is logged to the gf_server.log file located at /tmp/gf_server.log. The logging bridge jul-to-slf4j is redirecting all JUL logging to slf4j ant then to the logging backend of your choice. I used log back instead of log4j. Logback has some advantages over log4j which are described here.