Spring combined with log4j (SLF4J)

Source: Internet
Author: User

maven Dependency <!--slf4j (cascade: LOG4J/SLF4J-API)--<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.5</version></dependency> Configure demo log4j.properties# Output Pattern:date [thread] priority Category-messageLog4j.rootlogger=info,console,logfile#ConsoleLog4j.appender.console=org.apache.log4j.consoleappenderLog4j.appender.console.layout=org.apache.log4j.patternlayoutlog4j.appender.console.layout.conversionpattern=%d-xxl-net-%p [%c]-<%m>%n#RollingFileLog4j.appender.logfile=org.apache.log4j.dailyrollingfileappenderLog4j.appender.logfile.file=${catalina.base}/logs/xxl-net.logLog4j.appender.logfile.layout=org.apache.log4j.patternlayoutlog4j.appender.logfile.layout.conversionpattern=%d-xxl-net-%p [%c]-<%m>%n Logservice.javaprivate static transient Logger Logger = Loggerfactory.getlogger (triggerserviceimpl.class);logger.info ("Full station static ... start:{}", start);logger.info ("Full station static ... end:{}, cost:{}", end, End-start); comparison of log4j and slf4j

SLF4J:java simple log façade (easy Logging facade for Java, abbreviated SLF4J)

He is a package of logging framework interface program, in the appearance of the implementation of the model. The Logging framework that can be used when software is deployed is supported by the Java Logging API, log4j, and Logback. Published in the MIT licensing manner.

SLF4J author is Ceki g&uuml;lc&uuml, author of Log4j, who claims SLF4J is more efficient than log4j, simpler and more stable than Apache Commons Logging (JCL).

Comparison of SLF4J and log4j:

1, LOG4J provides TRACE, DEBUG, INFO, WARN, ERROR and FATAL six record levels, but slf4j that there is no substantial difference between the error and FATAL, so took off the FATAL level, only the remaining five.

2, most people in the program will go to write Logger.error (exception), in fact, this time log4j go back to this exception tostring. The true wording should be logger (message.exception), and SLF4J would not make the programmer make the mistake.

3, log4j indirect in encouraging programmers to use string addition to the wording, and SLF4J will not have this problem, you can use Logger.error ("{} Is+serviceid", Serviceid);

4, the use of slf4j can be conveniently used to provide a variety of collective implementation of the jar. (similar to Commons-logger)

5, from Commons--logger and log4j merge is very convenient, SLF4J also provides a swing tools to help you complete the merge.

6, the provision of string content replacement function, will be more efficient, explained as follows:

?
1 2 3 4 5 6 7 8 9 Ten //传统的字符串产生方式,如果没有要记录Debug等级的信息,就会浪费时间在产生不必要的信息上logger.debug("There are now "+ count + " user accounts: "+ userAccountList); //为了避免上述问题,我们可以先检查是不是开启了Debug信息记录功能,只是程序的编码会比较复杂if(logger.isDebugEnabled()) {    logger.debug("There are now "+ count + " user accounts: "+ userAccountList);} //如果Debug等级没有开启,则不会产生不必要的字符串,同时也能保持程序编码的简洁logger.debug("There are now {} user accounts: {}", count, userAccountList);

7. SLF4J only supports the MDC and does not support NDC.

Spring combined with log4j (SLF4J)

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.