Logger comes from log4j's own package. If you use Logger.getLogger, you need a log4j jar package. In this way, you can only rely on log4j:
LogFactory comes from the common-logging package. If you use LogFactory.getLog, you can replace log4j with any logger that implements the common logging interface, and the program will not be affected. Apache's common-logging package is a universal logging interface. Through this middle layer, you can specify which logging system to use. Increase system flexibility. If log4j does not exist, commons-logging will choose other log implementation classes. This ensures that the log4j log file does not have to be used in the program.
Reasons for increased flexibility:
1) First, look for your own configuration file commons-logging.properties under the classpath. If found, use the Log implementation class defined in it;
2) If the commons-logging.properties file cannot be found, check whether the system environment variable org.apache.commons.logging.Log has been defined. If found, use the Log implementation class defined by it;
3) Otherwise, check whether there is a Log4j package in the classpath. If found, Log4j will be automatically used as the log implementation class;
4) Otherwise, use the JDK's own log implementation class (the log implementation class is only available after JDK1.4);
5) Otherwise, use a simple log implementation class SimpleLog provided by commons-logging;
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. Instead, the Jar package of Log4j is simply placed in classpash. In this way, the integration of commons-logging and Log4j is completed very simply.
According to different properties, log information is usually divided into different levels, from low to high: "Debug (DEBUG)" "Information (INFO)" "Warning (WARN)" "Error (ERROR)" "Fatal Error (FATAL) )".
Based on common-logging operation mode:
Copy the code code as follows:
package org;
import org.apache.commons.logging.Log;
import org.apache.log4j.Logger;
public class Test extends TagSupport{
public static Log log=LogFactory.getLog(Test.class);
public static void test()
{
log.debug("111");
log.info("125");
log.warn("485");
log.error("error");
}
public static void main(String[] a)
{
Test.test();
}
}
Operation method based on log4j
Copy the code code as follows:
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class TestLog4j {
static Logger logger = Logger.getLogger(TestLog4j.class);
public static void main(String args[]) {
PropertyConfigurator.configure("log4j.properties");
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}
commons-logging only provides a layer of packaging for Log4j (including other LOG implementations, of course). The specific log output is still transferred internally to Log4j for processing, and log4j will go to the classes directory by default to find the log4j.properties file.