Why use log4j instead of system out. println?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[why use log4j instead of system. Out. Println?]

Hello, I'm Jiang Xinyi, a student of Shanghai Branch of it Academy. I'm an honest, pure and kind java programmer

Today, I'd like to share with you the knowledge points in deep thinking about Java task 1 on the official website of the Academy - why use log4j instead of system out. println?

1. Background introduction

Adding log records to applications is generally based on three purposes: monitoring the changes of variables in the code and periodically recording them in files for statistical analysis by other applications; Track the running track of the code as the basis for future audit; Act as a debugger in the integrated development environment and print the debugging information of the code to the file or console.

The most common way is to embed many print statements in the code, which can be output to the console or file. A better way is to construct a log operation class to encapsulate such operations, rather than filling the main body of the code with a series of print statements.

Today, with the emphasis on reusable component development, in addition to developing a reusable log operation class from beginning to end, Apache provides us with a powerful log operation package - log4j

Log4j is an open source project of Apache. By using log4j, we can control that the destination of log information transmission is console, file, GUI component, even socket server, NT event recorder, UNIX syslog daemon, etc; We can also control the output format of each log; By defining the level of each log information, we can control the log generation process in more detail. The most interesting thing is that these can be flexibly configured through a configuration file without modifying the application code.

2. Knowledge analysis

About log4j, you need to know the following points

The configuration file of log4j has the following three aspects

1. Root directory (level and destination);

2. Destination (console, files, etc.);

3. Output style

Root directory (level and destination)

log4j. rootLogger = [ level ],appenderName,…

There are four levels officially recommended, from low to high: debug, info, warn and error

Appendername is the parameter name of the destination

Destination (console, files, etc.)

log4j. appender. appenderName = fully. qualified. name. of. appender. class

Log4j provides five output destinations

Configure the format of log information

log4j. appender. appenderName. layout = fully. qualified. name. of. layout. class

Log4j provides the following four formats

3. Frequently asked questions

1. How is log4j loaded?

2. Why don't you stop after the error

3. Why use log4j instead of system out. println

4. What is the difference between rootcategory and rootlogger

4. Solutions

How is log4j loaded?

When the application starts, it will look for log4j. In the SRC directory by default If the XML configuration file does not exist, it will continue to look for log4j Properties file, as long as one of them is found, the contents of the configuration file will be loaded

Of course, it can also be loaded manually, because automatic loading will only load log4j XML or log4j Properties. If there are configuration files with different names or not placed in the root directory, they need to be loaded manually

Why doesn't error stop

Because there are not only four levels in log4j, but seven

Why use log4j instead of system OUT. PRINTLN

Programmers often encounter the situation of debugging programs outside the Java ide environment. At this time, most people will choose to use system out. Println statement is used to debug the method of outputting the value of a variable. This leads to a very troublesome problem: once one day the programmer decides not to display these systems out. Println can only comment out these garbage statements line by line. If you need to debug the variable value one day, you can only remove these comments and restore the system out. Println statement, the debugging code is full of logic code, which is neither beautiful nor conducive to search

Log4j handles this problem well, and system Out can only be output to the console, while logging tools such as log4j can configure the output target and output level to reduce the coupling between the output form and the program

What is the difference between rootcategory and rootlogger

In fact, rootlogger is a subclass of rootcategory, which is an old way of writing

5. Coding practice

Why use log4j instead of sys out_ Tencent video

6. Expand thinking

Why use slf4j instead of log4j?

Slf4j: simple logging facade for Java. It is not a specific logging solution. It only serves a variety of logging systems. According to the official statement, slf4j is a simple facade for the logging system, allowing end users to use the desired logging system when deploying their applications.

7. References

Reference 3:

Reference 2:

Reference 3:

8. More discussion

1. Difference between log4j and log4j2

Log4j is through a The properties file is used as the main configuration file, but log4j 2 has abandoned this method and adopts xml,. JSON or JSN only needs to introduce a jar package to do log4j in this way, while log4j 2 requires two cores

For log4j to take effect, we need to be on the web XML

log4j2. Just put XML in the project resource directory. Remember that a fine node is log4j2 XML instead of log4j XML, XML name can't be less than 2

The most important difference is the difference between configuration files. You can configure them according to your situation when you use them

2. How to close log4j without comments

Via console options

Immediateflush = true: indicates that all messages will be output immediately. If it is set to false, it will not be output. The default value is true.

3. XML configuration file and properties configuration file

Properties is a simple key value correspondence.

If properties can't meet your requirements, you naturally need to use XML.

XML can store complex object structures with multiple attributes.

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Skill tree It Academy

"We believe that everyone can become an engineer. From now on, find a senior brother to introduce you, control your learning rhythm, and stop being confused on the way to learning.".

Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance. Come and study with me~

Author: Jiang Xinyi

Link: http://www.jnshu.com/login/1/15188308

Source: Jianshu

The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Author: Shenxia link: https://www.jianshu.com/p/87276710eb79 Source: the copyright of Jianshu Jianshu belongs to the author. Please contact the author for authorization and indicate the source for any form of reprint.

For more information, you can join the IT communication group 565734203 to discuss and communicate with you

Here is the skill tree · it Academy: nofollow "> https://www.jnshu.com , beginners switch to the gathering place of the Internet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>