SLF4J with log4j configuration log system

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.