1. Q: What is SLF4J?
A: Simple Logging Facade for Java
Q: Why do we use SLF4J instead of Commons-Logging?
A: Because JCL is runtime discovery algorithm which relies on classloader hacks to find the logging framework at runtime
but this mechanism leads to numerous problems including unexpected behavior, hard to debug classloading problems resulting in increased complexity.
2. A simple example of using SLF4J --> Take SLF4J + Log4j as example
1) pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>edu.xmu.logging</groupId> <artifactId>Logging-Log4J</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> </dependencies> </project>
2) log4j.properties in class path is the same with that in last chapter.
# Define the root logger with appender X log = C:/YangKunLun/logging log4j.rootLogger = TRACE, FILE, SYSOUT #log4j.logger.edu.xmu = ERROR # Define the sysout appender log4j.appender.SYSOUT=org.apache.log4j.ConsoleAppender # Define the file appender log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender # Set the name of the file log4j.appender.FILE.File=${log}/log.out # Set the immediate flush to true(default) log4j.appender.FILE.ImmediateFlush=true # Set the threshold of file to debug mode log4j.appender.FILE.Threshold=debug # Set the threshold of console to trace mode log4j.appender.SYSOUT.Threshold=trace # Set the append to true, override log4j.appender.FILE.Append=true # Set the maxmium file size before rollover # log4j.appender.FILE.MaxFileSize=5KB # Set the date pattern log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-HH-mm # Set the backup index # log4j.appender.FILE.MaxBackupIndex=2; # Define the layout for file appender log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern=%m%n # Define the layout for console appender log4j.appender.SYSOUT.layout = org.apache.log4j.PatternLayout log4j.appender.SYSOUT.layout.conversionPattern=%m%n
3) Test case is the same with that in last chapter. We don't have to change the code, just to change the imported package.
package edu.xmu.log4j; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SLFLoggingExampleTest { private static Logger logger = LoggerFactory.getLogger("edu.xmu"); @Test public void test() { // TRACE < DEBUG < INFO < WARN < ERROR logger.trace("[TRACE] This is TRACE message"); logger.debug("[DEBUG] This is DEBUG message"); logger.info("[INFO] This is INFO message"); logger.warn("[WARN] This is WARN message"); logger.error("[ERROR] This is ERROR message"); } }
3. We've see in pom.xml that we imported slf4j-log4j12 in order to use Log4j in reality.
How can we know which package to import in order to use different kind of logging system?
Reference Links:
1) http://stackoverflow.com/questions/3222895/why-is-commons-logging-believed-to-be-unpopular Why we prefer SLF4J over JCL
2) http://articles.qos.ch/thinkAgain.html Think again before adpoting the commons-logging API
3) http://www.slf4j.org/manual.html SLF4J user manual