Log4j Introduction
Basically all large-scale applications, including our commonly used frameworks, such as Hibernate, spring, and struts, provide a certain amount of log information within them. Why is this log information hard-coded in the system? It is a way to debug the system and observe the system running status. Most programmers may remember that when they first wrote code, they always failed to write a method and liked to use system. out. println ("1111111") and other code to check whether the program is running in the way you want. In fact, these sysout is the log record information, but the system. out. println or system. err. println has many limitations in log processing:
First, all log information output is parallel. For example, I cannot control the output of only the log debugging information in one or more modules, or I cannot control the output of only some logs.
Second, the output of log information cannot be controlled in a unified manner. For example, I cannot control when to print information or when to print information. If I do not want to print any information, I can only comment the log information in all java files.
Third, the log information output method is fixed. For example, during the encoding period, it can only be output to the eclipse console. In the Tomcat environment, it can only be output to the Tomcat log file, I cannot choose complex control options, such as outputting logs to the database or outputting logs to emails.
Some may say that in order to debug the system, the use of system. Out. println, these small pot friends are simply weak. Do debugging systems not use DEBUG? It may be easier to quickly identify and locate errors using debug in the coding stage. But what if I want to track and observe the running status of the system at the product stage?
Therefore, in the encoding phase, a unified hard-coded LOG method is often required to record the running status of the program and provide a simple method, unified Control of log output granularity and output mode. Log4j was born under such circumstances. Currently, log4j has been widely used and has become one of the most popular log frameworks.
Log4j provides a very simple way to use it. If you do not need to discover log4j in depth, or you need to expand log4j by yourself, it is very convenient to use. Log4j is also very concerned about the performance, because a large number of log-based methods in applications will bring about some negative problems:
1. It will increase the length of a piece of code and increase the difficulty of reading;
2. Increasing the amount of code will reduce the system running performance to a certain extent;
Log4j constantly optimizes the performance so that the performance of common log output is even faster than system. Out. println.
Finally, log4j provides a very convenient extension method that allows us to easily customize our own log output levels and log output methods.
Before learning log4j, I want to say a word to all Java programmers in China: log is an attitude. I hope you can enjoy it.
For more technical questions, visit the Technical Forum on the official website of Chengdu Chuanzhi podcast!