How can we make Log4j play its role? The answer is simple. You only need to meet the requirement that "jar packages with Log4j in classpath ". As mentioned above, commons-logging will automatically discover and apply Log4j. So as long as it exists, it plays a role. (Does it exist? Naturally, it does not work. commons-logging will select other log implementation classes .)
Note: the configuration file log4j. properties is required for Log4j. If the configuration file does not exist in the classpath, or the configuration is incorrect, a running exception is thrown.
In this way, to correctly apply Log4j to output log information, the role of log4j. properties is very important. Fortunately, this file has a general template and can be used by copying a copy (slightly modified. Almost every Java project directory contains a log4j. properties File. You can download the source code of several open-source Java projects. At the end of this article, we also attached a template-type log4j. properties file, which can be directly copied and used, or modified as needed. The log4j. properties file will be introduced later.
Here, by the way, we will be prompted: If you don't need to use commons-logging and use Log4j separately, the operation will be a little more troublesome, because Log4j requires a little more initialization code (compared to commons-logging ):
1 import org. apache. log4j. Logger;
2 import org.