Original: Https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast
Logback is intended as a successor to the popular Log4j project. It is designed, in addition-many individual contributors, by Ceki Gülcü, the founder of Log4j. It builds upon experience gained in building industrial-strength logging systems going back as far as 1999. Logback-classic natively implements the slf4j API so, can readily switch back and forth between Logback and other Logging frameworks such as log4j or java.util.logging (JUL).
The simple Logging facade for Java or (SLF4J) serves as a simple facade or abstraction for various Logging frameworks, e.g . Java.util.logging, log4j and Logback, allowing the end user to plug in the desired logging framework at deployment time.
If your working with a Maven Web-app project, this procedure would get you setup to log with Logback through SLF4J super FA St.
Step 0-add Logback Dependency libraries. If you is using Maven skip this step.
Import the following libraries to your Web-inf/lib folder:
- Web-inf
- Lib
- Logback-classic.x.x.x.jar
- Logback-core.x.x.x.jar
- Slf4j-api-x.x.x.jar
Step 1-add logback Dependency to your Maven POM
Declare the following dependency in your Maven 2 Pom.xml and Maven would grab the appropriate libraries for you during the Build.
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version></dependency> |
Step 2-import Existing (starter) XML configuration files
You'll likely want to the start with a base configuration file, you can build upon. In Maven can has a logging configuration for your main source and another for your testing. You can download starter configuration files for your project by clicking the links in the hierarchy below. Put them in your project according to the position indicated by the hierarchy shown.
Step 3-customize The XML configuration just enough to test
Open up the Logback.xml file. If you used the starter provided and the link above, you'll find the following:
<?xmlversion="1.0"encoding="UTF-8"?><configuration> <appender name="STDOUT"class="ch.qos.logback.core.ConsoleAppender"> <layoutclass="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern> </layout> </appender> <loggername="com.base22"level="TRACE"/> <rootlevel="debug"> <appender-refref="STDOUT"/> </root></configuration> |
You'll notice that one logger are defined at the A package level ("Com.base22"). You can simply the change this to the match your application ' s package base. You can also declare additional loggers (packages and/or classes) if desired.
Step 4-put Logging code in your classes
The last thing your need to do are drop some logging code in a class and test this whole setup.
Add the following to the Imports section of your Java code:
importorg.slf4j.Logger;importorg.slf4j.LoggerFactory; |
Add the following at the top of your class in the Global section (just under, the line, declares your class public clas s Whatever extends Whatever). Change the name of the class (myClassName) in the GetLogger method call, of course. Name it the same as the class you ' re dropping this code into.
staticfinalLogger LOG = LoggerFactory.getLogger(MyClassName.class); |
Throw Some logging statements in the your code somewhere where you know they ' ll is fired right away when you run the your app. for Example
LOG.trace("Hello World!");LOG.debug("How are you today?");LOG.info("I am fine.");LOG.warn("I love programming.");LOG.error("I am programming."); |
Alternatively, can just download this simple console test app and run it as a Java app from the command line or from W Ithin your IDE:
Slf4jconsoletest.java
This class have a main method so it runs as a Java app and it'll log one statement at each level.
Step 5-run your app and make sure it works
Finally, run your app and make sure it works. You should see log lines in your console. If it doesn ' t work, just review these steps a little more carefully and fiddle with it.
How to setup slf4j and Logback in a Web app-fast--reprint