Summary of unified Management log with SLF4J

Source: Internet
Author: User
Tags log4j

Summary of unified Management log with SLF4J

Reference page: http://www.slf4j.org/

A Use SLF4J unified management and configuration for unified use of log4j logs

Jar used: (Slf4j-api-1.7.5.jar,jcl-over-slf4j-1.7.5.jar,jul-to-slf4j-1.7.5.jar,slf4j-log4j12-1.7.5.jar,log4j-1.2.12.jar)

It is convenient to use SLF4J Unified Management log because multiple frameworks in the project use different logs or change the log framework in the previous project instead to another log.

1.SLF4J is an interface standard.

2.jcl-over-slf4j-1.7.5.jar,jul-to-slf4j-1.7.5.jar is to bridge the previous JCL Jul log to slf4j and be managed by SLF4J.

Note: Use of Jul-to-slf4j-1.7.5.jar should include code when application is initialized (remove the handle that existed before, install Slf4jbridgehandler):
Slf4jbridgehandler.removehandlersforrootlogger ();
Slf4jbridgehandler.install ();

3.slf4j-log4j12-1.7.5.jar is the implementation of the SLF4J interface, which is a slf4j to log4j conversion layer, which turns SLF4J's managed log into log4j

4.log4j-1.2.12.jar is the jar of the log4j log

5. Then configure a log4j configuration file: log4j.properties

#将等级为INFO的日志信息输出到File (same name as defined) destination

#可以填写多个目的地

#等级可分为OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, all (in descending order of priority)

Log4j.rootlogger =info,file

#此为hibernate的包中类的等级, and select its information output (optional output to define)

#log4j. Logger.org.hibernate.tool.hbm2ddl=info,file

#log4j. Logger.org.hibernate.jdbc=debug,file

Log4j.logger.org.hibernate.type.descriptor.sql.basicbinder=trace,file

#log4j. Logger.org.hibernate.type.descriptor.sql.basicextractor=debug,file

Log4j.logger.org.hibernate.sql=trace,file

# Log4j.additivity.bao.leiming=false is log only printed to the output of this log

#不再打印到其父logger的输出端 to prevent log information from printing multiple times

Log4j.additivity.org.hibernate.sql=false

#log4j. Logger.org.hibernate.engine.queryparameters=trace,file

#log4j. Logger.org.hibernate.engine.query.hqlqueryplan=debug,file

Log4j.additivity.org.hibernate.type.descriptor.sql.basicbinder=false

The type of log output that #定义名为File (name can be arbitrary) this generates a log for each day

Log4j.appender.file=org.apache.log4j.dailyrollingfileappender

#定义名为File的输出端文件名

Log4j.appender.File.file =c:/logs/tomcat.

Log4j.appender.File.encoding =utf-8

Log4j.appender.File.datePattern = '. ' Yyyy-mm-dd '. Log '

#定义名为File的输出端的layout的类型, here patternlayout for flexibility in assigning layout patterns

Log4j.appender.File.layout =org.apache.log4j.patternlayout

#使用PatternLayout, specify a specific format for printing information here

log4j.appender.file.layout.conversionpattern=%d[%t]%-5p%c-%m%n

Log4j.appender.File.append =true

#与上述雷同, here is the type of log output defined named stdout

Log4j.appender.stdout=org.apache.log4j.consoleappender

Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

log4j.appender.stdout.layout.conversionpattern=%d{yyyy-mm-ddhh:mm:ss,sss}[%c]-[%p]%m%n

This allows the log to be written in the project using the SLF4J unified interface (assuming that the class is named AA)

Logger the name of the configuration file, if not, inherit the parent, if not the parent inherits Rootlogger

Final Logger Logger = Loggerfactory.getlogger ("Logger's name");

Logger.debug ("thisis debug Message");

Logger.info ("This is InfoMessage");

Logger.warn ("This is Warnmessage");

Logger.error ("This is ErrorMessage");

It is convenient to change the log frame later, such as using Logback without log4j.

Example: The original project using SLF4J Unified Management using LOG4J and now use Logback

Steps:

1, delete Log4j-1.2.12.jar, Slf4j-log4j12-1.7.5.jar

2. Delete the configuration file log4j.properties

3. Import jar Package: Log4j-over-slf4j-1.7.5.jar, Logback-access-1.0.13.jar, Logback-classic-1.0.13.jar, Logback-core-1.0.13.jar

4. Add logback-1.0.13 configuration file Logback.xml

Can

You do not need to modify the code before the project, just replace the jar package and the corresponding log configuration file

Review it again. December 15, 2016

Additional Reference URLs:

http://phl.iteye.com/blog/2021461

Http://www.tuicool.com/articles/IfeUfq

http://blog.csdn.NET/xianymo/article/details/51026663

Summary of unified Management log with 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.