Using logback with Logfile Separation for your Wildfly project

Overview

Logback is intended as a successor to the popular log4j project.
In this sample we will configure Logback to write logging info to an application specific logfile 
and to the Wildlfy server.log file. For a first check we many read the application loggin file. 
If you are interested in timing considerations for generic Wildfly errors and your application 
errors you may review Wildflys server.log file

Logging Separation 
Assuming your container supports child-first class loading, separation of logging can be 
accomplished by embedding a copy of slf4j and logback jar files in each of your applications. 
For web-applications, placing slf4j and logback jar files under the WEB-INF/lib directory of 
the web-application is sufficient to endow each web-application with a separate logging 
environment. A copy of the logback.xml configuration file placed under WEB-INF/classes will 
be picked up when logback is loaded into memory.

By virtue of class loader separation provided by the container, each web-application will 
load its own copy of LoggerContext which will pickup its own copy of logback.xml.

XML configuration files

pom.xml 
     <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version> 
        </dependency>
        
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.3</version>
        </dependency>

Disable server logging features: ./src/main/webapp/WEB-INF/jboss-deployment-structure.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
  <deployment>
    <exclusions>
      <module name="org.apache.commons.logging" />
      <module name="org.apache.log4j" />
      <module name="org.jboss.logging" />
      <module name="org.jboss.logging.jul-to-slf4j-stub" />
      <module name="org.jboss.logmanager" />
      <module name="org.jboss.logmanager.log4j" />
      <module name="org.slf4j" />
      <module name="org.slf4j.impl" />
    </exclusions>
  </deployment>

Logback configuration file:  ./src/main/resources/logback.xml

-> Writes logging Info to /tmp/JPATestBean.log and Wildfly server.log 
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                   %d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">  
            <file>/tmp/JPATestBean.log</file>  
            <layout class="ch.qos.logback.classic.PatternLayout">    
                <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>  
            </layout>
        </appender>
  
    <root level="debug">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

Java Code

Initialize logger :
public class JPATestBean implements Serializable
  {
    // private static final org.slf4j.Logger logger = LoggerFactory.getLogger(JPATestBean.class);
    final static Logger logger = LoggerFactory.getLogger(JPATestBean.class);
......     
        logger.info("\nWeb Application shutdown ..." );
....
        logger.error("FATAL ERROR: Closing Entitiy Manager Factory ..." );

 

Reference

One thought on “Using logback with Logfile Separation for your Wildfly project”

  1. Thanks, this was very helpful, I didn’t realise I needed jboss-deployment-structure.xml.
    One note though you’re missing the closing tag for .

    Thanks though

Leave a Reply

Your email address will not be published. Required fields are marked *