Tutorials‎ > ‎

Integrate SLF4j and LogBack with Oracle GlassFish v3 applications

In order to be able to use Logback on a Glassfish server, running multiple applications – on multiple Logback contexts, a few settings are required on the Glassfish server.

Install libraries for Logback

To allow Logback to work with multiple Logback contexts simultaneously, it’s best if Logback – and all required supporting libraries – are installed on the Glassfish server.

The default location for installing libraries on a Glassfish server is /opt/glassfish3/domains/domain1/lib/
To install the libraries, it’s sufficient to put the JAR-files in that directory.

Logback, when used as described above, requires the following libraries:
Package Version  Info  Provider 
logback-classic 1.0.11 This is the “main” logging package http://logback.qos.ch/download.html
logback-core 1.0.11 Dependency for “logback-classic” http://logback.qos.ch/download.html
janino 2.6.1 Required for conditional configuration processing http://dist.codehaus.org/janino/changelog.html
commons-compiler 2.6.1 Dependency for “janino” library http://dist.codehaus.org/janino/changelog.html
slf4j-api 1.7.5 This is what the applications use for logging http://www.slf4j.org/download.html

Configuring Glassfish for using Logback

To allow Glassfish to work with Logback, 2 JVM settings are needed.
These settings can be create via the asadmin commandline interface:
cd /opt/glassfish3/bin
./asadmin create-jvm-options '-Dlogback.ContextSelector=JNDI'
./asadmin create-jvm-options '-Dlogback.configurationFile=${com.sun.aas.configRoot}/apps'

Configuring applications

In order to use Logback in applications, we must add the necessary dependencies to the maven pom and configure the web.xml.
(For the maven test run we will use log4j, this because of the simple configuration for the console. Which is very handy if you're using a build server like Jenkins)

Maven pom.xml dependencies
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
    <scope>provided</scope>
</dependency>

<!-- BEGIN: TEST -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    <scope>test</scope>
</dependency>

The "src/test/resources/log4j.properties":
# Set root logger level to WARN
log4j.rootLogger=DEBUG, CONSOLE

# Console logger
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} | %p | %c: %m%n

The "src/main/webapp/WEB-INF/web.xml" with logback settings:
<!-- Logback configuration; NOTE: This should be the first ServletContextListener in your web.xml! -->
<listener>
    <listener-class>ch.qos.logback.classic.selector.servlet.ContextDetachingSCL</listener-class>
</listener>

<filter>
    <filter-name>LoggerContextFilter</filter-name>
    <filter-class>ch.qos.logback.classic.selector.servlet.LoggerContextFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>LoggerContextFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<env-entry>
    <env-entry-name>logback/context-name</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>${application_name}></env-entry-value>
</env-entry>

<!-- Viewing status messages -->
<servlet>
    <servlet-name>ViewStatusMessages</servlet-name>
    <servlet-class>ch.qos.logback.classic.ViewStatusMessagesServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ViewStatusMessages</servlet-name>
    <url-pattern>/logBack</url-pattern>
</servlet-mapping>

The ViewStatusMessages servlet will be viewable at the URL http://${host}/${yourWebapp}/logBack