slf4j+log4j implementing Logging in Java

Source: Internet
Author: User

Little Alan is here today to talk to you about the simple and common but essential thing in development, what is it? That's the log record, the log output, the log save.

The following is a unified log record four words to describe.

Logging is an essential part of the development of the project, especially for the Background service node program, with the output of the log can clearly see the whole process of running the program, then the successful operation of the project is OK, we should be clear to see the process of running the program well, if the project if the bug? If this time does not have the above mentioned log record, the log output, the log saves, that consequence simply is unimaginable, why? Because even if your program throws an exception does not mean that the program must stop waiting for you to solve the problem, and then it will be executed, most of the background node program will not because of some exceptions to stop its execution, but will continue to maintain execution in the event of some problems, Does not affect the execution of other functions because of some current problems, in order to continue to provide users with other correct services. Then there is a certain problem, the program continues to execute, and do not save the log in some way, for the information of the JVM output, it will continue to scroll down, and the length is not unlimited, then it is likely that you will lose the error message makes it difficult for you to solve the problem in the program, But with the log records that would be good to do, in the event of a problem you can always go to check the relevant logs, and then classify the log, the error log specifically stored in a certain location, then I think you are relatively happy at this time.

So how do you implement logging in Java? Little Alan personally prefer to use slf4j+log4j, also strongly recommend the use of this combination, rather than simply use log4j, why? Because the log library can not only log4j, such as there are java.util.logging, Logback and so on. What if the java.util.logging was used in our project and the leader said to change it to log4j? If your project is using log4j Java code instead of slf4j Java code, then congratulations, maybe this java.util.logging and log4j coding style is not the same, then how to do? You a class of a class to go to the log record code to find out, and then one by one to change Bai, refers to the indefinite leakage of two, it might be wonderful. But if it is the SLF4J Java code Implementation of the log records, then the result is completely different, and gently loosen up your superior's wonderful requirements. SLF4J and other log library is not the same, to see the Baidu encyclopedia this paragraph:

SLF4J is not a log library implementation, but just a façade for the log library implementation, which allows you to use any of the Log class libraries in the background through the appearance mode. Tube You wear is what underwear , anyway elder brother's appearance does not change, after all underwear outside wear of people or less, only change the underwear will not affect your appearance! Next we see how to achieve slf4j+log4j, and then a little chat about changing underwear topic.

Friendship tips: Here to Maven project for example, do not understand Maven's classmates, can be in little Alan's blog to see Maven's essay Understanding maven understanding.

Step one: Introduce the relevant jar package in MAVEN's POM structure: This contains slf4j, log4j, can not be missed slf4j-log4j

Slf4j:http://www.slf4j.org/download.html

Second step: Create the Log4j.properties file under the source code directory, the MAVEN project's source directory is Src/main/java

Let's take a look at the contents of the Log4j.properties file, which you can understand according to the comments:

%x What everyone on the Internet to find other corresponding information, explained it is a large space, here is saved.

Final step: Write Java implementation code for SLF4J

Here you can write their own main method to test, little Alan is directly in the development of the project cut a map, so also avoid everyone lazy, to try their own hands-on effect.

Another implementation of SLF4J is also possible, that is, the way the placeholder is used, the code is as follows:

Results Demonstration ①

Results Demonstration ②

Results Demonstration ③

This is the perfect implementation of slf4j+log4j logging, then how can we replace log4j with other log libraries? Very simply, we open the downloaded slf4j file to see:

It contains a lot of other log library support jar file, we just need to change the dependency of MAVEN to the required log library, and then a log library corresponding to the supporting jar file can be replaced by the corresponding log library configuration file, so you do not have a class to change the code of a class, because the underwear changed, But the appearance of SLF4J does not require any change.

Cute bo master: Alanlee

Blog Address: Http://www.cnblogs.com/AlanLee

This article is from the blog garden, welcome everyone to join the blog park.

slf4j+log4j implementing Logging in Java

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.