Slf4j + log4j implements logging in Java, slf4jlog4j

Source: Internet
Author: User

Slf4j + log4j implements logging in Java, slf4jlog4j

Alan is here to talk about something that is simple, common, but essential in development today. What is that? That is, log record, log output, and log storage.

Later, it will be used in a unified manner.Log recordsFour words to describe.

Log record is an essential part of project development. Especially for backend service node programs, with log output, you can clearly see the entire program running process, if the project runs successfully, we should be clear about the program running process. What if a Bug occurs in the project? If there is no log record, log output, or log storage at this time, the consequences would be unimaginable. Why? Because even if your program throws an exception, it does not mean that the program must stop and wait for you to solve the problem before it can be executed, most of the background node programs will not stop execution due to exceptions, but will continue to execute so that in the case of some problems, it does not affect the execution of other functions due to some existing problems, so as to continue providing other correct services to users. At this time, there will be some problems. If the program continues to be executed and logs are not saved in some ways, for the jvm output information, it will continuously scroll down, and the length is not infinite, so it is very likely that you will lose the error information and it is difficult for you to solve the problems in the program, however, with log records, you can view the relevant logs at any time when a problem occurs, and then classify the logs and store the error logs in a specific location, in this case, I think you will be more pleasant.

So how to implement logging in Java? Small Alan personally prefers slf4j + log4j and strongly recommends this combination, instead of simply using log4j. Why? Because log4j is not the only log4j library, such as java. util. logging and logback. If java. util. logging has been used in our project, what should the leader say to change it to log4j? If your project uses the Java code of log4j instead of the Java code of slf4j, congratulations, maybe this java. util. logging and log4j have different encoding styles. What should I do? You can find the log record code in one class and modify it one by one. Maybe it's amazing to miss two. However, if it is a log record of slf4j Java code implementation, the results will be completely different. It is easy to handle the wonderful requirements of your superiors. Slf4j is different from other logstores. Let's take a look at the description in Baidu Encyclopedia:

Slf4j is not a logstore implementation, but a logstore implementation facade. It allows you to use any log class library in the background through the appearance mode. What kind of underpants do you wear? the appearance of your brother remains unchanged. After all, there are still few people wearing underpants. Changing your underpants will not affect your appearance! Next, let's take a look at how to implement slf4j + log4j. Next, let's talk about changing the topic of underwear.


Friendly reminder: here we take the maven project as an example. If you do not understand maven, you can go to Alan's blog to check maven's understanding and understanding.


Step 1: introduce the relevant jar package in the maven pom structure: This contains slf4j, log4j, do not miss the slf4j-log4j

Server Load balancer:


Step 2: Create the log4j. properties file under the source code directory. The maven project's source code directory is src/main/java

Let's take a look at the content of the log4j. properties file. You can understand it according to the Notes:

% X everyone went online to look for other corresponding materials. It would be a huge space to explain, saving the cost.


Last step: Compile the Java implementation code of slf4j

Here, you can write a main method to test it. Alan will cut a graph directly in the project under development, so that you can avoid laziness and try the effect on your own.

Another implementation of slf4j is also feasible, that is, using placeholders. The Code is as follows:


Result 1

Result example ②

Result example ③



In this way, the log records of slf4j + log4j are implemented perfectly. How can we replace log4j with other logstores? It's easy. Let's open the downloaded slf4j file and see:

It contains many jar files supported by other logstores. We only need to change the maven dependency to the required logstore, and then change the corresponding jar files supported by the logstore, switch to the configuration file of the corresponding logstore, so that you will not change the code for a class or a class, because the panties are changed, but the slf4j appearance does not need to be changed.


Alimail: AlanLee


This article is from the blog site. You are welcome to join the blog site.


Related Article

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: 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.