Java knowledge accumulation-log components: log4j, logback, and common-logging

Source: Internet
Author: User

The first time I entered the workplace, I had a comprehensive understanding of "log. Unlike print statements that are everywhere in the student age, schools cannot teach anything useful.

The recent work happens to be to convert the original log4j log component of the project to logback. The reason for conversion is that logback has better performance than log4j. However, the arrival of the latest version of log4j seems to make up for this gap. In short, the assigned tasks should be done with great care, and many useful things can be learned from them. Next, I will introduce how log4j converts logback, possible problems, and solutions. The jar package involved will be given the download link at the end.

 

To switch log4j to logback, follow these steps:

1. Introduce the jar packages of logback-classic and logback-core to the project and remove the jar packages related to log4j from the classpath of the project.

2. Check that the project introduces the slf4j jar package as the log adaptation.

3. create a logback in the project. XML file, using the conversion tool (Address: http://logback.qos.ch/translator/), the original log4j configuration file (log4j. properties. XML.

4. After the preceding conversion, You need to modify the following details because the parameter meanings are different:

Configuration content

Logback

Log4j

% C {}

{0} print the class name, for example, mylog

{N} print the first letter of the Class Name and package name, for example, C. B. U. mylog.

{0} print the full path class name, for example, Com. bs3.utils. mylog.

{1} print the class name, for example, mylog

{2} print the class name and the previous package name, for example, utils. mylog

{N }.

Log File Name Style

<File> log/Rpt. Log </File>

<Rollingpolicy class = "ch. QoS. logback. Core. Rolling. timebasedrollingpolicy">

<Filenamepattern> log/Rpt. log. % d {yyyymmddhh} </filenamepattern>

</Rollingpolicy>

<! -- Removed'.'-->

Log4j. appender. Rpt. File = log/Rpt. Log

Log4j. appender. Rpt. datepattern = '.' yyyymmdd

5. Modify the errors caused by missing log4j. jar in the project to use logback.

 

Possible problems and solutions:

1. After log4j is converted to logback, all spring logs are output to the console in red letters, not controlled by logback.

Because spring logs use commons-logging by default, the solution is to introduce jcl-over-slf4j-1.6.1.jar in the project, so that commons-logging can be connected to slf4j, and then unified log output through logback.

2. After the switchover is complete, the error "Java. Lang. illegalaccesserror: tried to access field org. slf4j. impl. staticloggerbinder. Singleton from class org. slf4j. loggerfactory" will occur when the project is started.

The reason is that the jar package version of The slf4j-api is too low, change to slf4j-api-1.6.4.jar to solve.

 

Download the jar package:

Http://pan.baidu.com/share/link? Consumer id = 475873 & UK = 152821134

 

 

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.