Table of Contents
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.
- For details read : Logback Overview Separation
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
- SLF4J + Logback does not log in WildFly – : http://stackoverflow.com/questions/23079492/slf4j-logback-does-not-log-in-wildfly
- How to setup SLF4J and LOGBack in a web app – fast https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast
- Logback Overview Separation http://logback.qos.ch/manual/loggingSeparation.html
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