First of all, these two are log systems.
Just slf4j is a simple façade log system simply to provide some column log interfaces without a specific implementation similar to JDBC but simpler than JDBC because
It does not need to load the driver to add only its specific package. And log4j himself alone can be a complete log system it provides its own implementation rather than simply
Provides a log interface. Let's talk about the integration of the two.
1. Create a new Java project
2, introduction of Log4j-1.2.15.jar,slf4j-api-1.5.6.jar,slf4j-log4j12-1.5.6.jar three package version Unlimited
3, in the project root directory under the new log4j.properties content as follows
Log4j.rootlogger=info, Serverdailyrollingfile, stdout
Log4j.appender.serverdailyrollingfile=org.apache.log4j.dailyrollingfileappender
Log4j.appender.serverdailyrollingfile.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.serverdailyrollingfile.file=logs/notify-subscription.log
Log4j.appender.serverdailyrollingfile.layout=org.apache.log4j.patternlayout
log4j.appender.serverdailyrollingfile.layout.conversionpattern=%d-%m%n
Log4j.appender.serverdailyrollingfile.append=true
Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD hh\:mm\:ss}%p [%c]%m%n
4. New Test class
public class Testslf4j {
public static void Main (string[] args) {
Propertyconfigurator.configure ("Log4j.properties");
Logger Logger = Logger.getlogger (Testslf4j.class);
Logger.info ("yes");
Org.slf4j.Logger logger2 = Loggerfactory.getlogger (Testslf4j.class);
Logger2.info ("yes");
Logger2.debug ("Hello");
Logger2.warn ("Hello");
}
}
The output is
2014-12-09 11:20:53 INFO [com.iss.slf4j.TestSlf4j] Yes
2014-12-09 11:20:53 INFO [com.iss.slf4j.TestSlf4j] Yes
2014-12-09 11:20:53 WARN [com.iss.slf4j.TestSlf4j] Hello
Believe may be in doubt since log4j can already be used as a log system alone why do we need SLF4J?
After reading this article, perhaps your doubts will be solved http://www.importnew.com/7450.html#comment-204549
SLF4J with log4j configuration log system