LogFactory comes from the public log package. If you use LogFactory.getLog, you can replace log4j with any logger that implements the universal logging interface, and the program will not be affected. Apache's common-logging package is a universal logging interface, through which you can specify which logging system to use at will. Increase the flexibility of the system. If log4j does not exist, commons-logging will choose another logging implementation class. This ensures that log4j is not necessarily used in programs.
Reasons for increased flexibility:
1) First, find your own configuration file commons-Logging.properties under classpath, and if it is found, use the log defined in it to implement the class;
2) If the files of commons-Logging.properties cannot be found, check whether the system environment variable org.apache.commons.logging.Log has been defined, and if it is found, use the defined log to implement the class;
3) Otherwise, check whether there is a package of Log4j in the class path, and if there is, automatically use Log4j as the log implementation class;
4) Otherwise, use the log implementation class of JDK itself (the log implementation class will be available after JDK 1.4);
5) Otherwise, use the simple log provided by commons-logging to implement the SimpleLog class;
In order to simplify the configuration of commons-logging, the configuration file of commons-logging is generally not used, and the system environment variables related to commons-logging are not set, only the Jar package of Log4j is put into classpash. In this way, the integration of commons-logging and Log4j is simply completed.
According to different attributes, log information is usually divided into different levels, from low to high: debugging, information, warning, error and fatal error.
Operation mode based on public log:
Packaging organization;
Import org.apache.commons.logging.log;
Import org.apache.log4j.logger;
Common class test extension tag support {
Public static log log = logfactory.getlog (test.class);
Public Static Invalid Test ()
{
log . debug(" 1 1 1 ");
log . info(" 125 ");
log . warn(" 485 ");
log . error(" error ");
}
Public static void main(String[] a)
{
test . test();
}
}
Running mode based on log4j
Import org.apache.log4j.logger;
Import org.apache.log4j.propertyconfigurer;
Public class TestLog4j {
static Logger Logger = Logger . get Logger(testlog 4j . class);
Public static void main (strinargs []) {
property configurator . configure(" log4j . properties ");
Logger.debug ("Here are some debugging");
Logger.info ("Here is some information");
Logger.warn ("Here are some warnings");
Logger.error ("There are some errors here");
Logger.fatal ("Here are some deadly");
}
}
-
Commons-LOGging only wraps Log4j (including other logging implementations, of course), and the specific log output is handed over to the log4j behind it. Log4j will go to the classes directory to find the log4j.properties file by default.