Logging enables us to debug and track the behavior and status of any time at any time. In any larger application, Logging is an integral part, so there are many third-party logging tools, which exempt from developers to write the Logging API. In fact, even if JDK has a structured Logging API. Since there is already so much choice (log4j, jdk, logkit, etc.), usually we can always find ready-to-developed APIs that are best suited for their application requirements.
However, there may be an exceptional situation, such as a familiar Logging API that cannot be compatible with the current application, or because of some kind of hard regulations, or due to the application of the architecture. The case of the Commons project Logging component is to package the functionality of the log log into a set of standard APIs, but its underlying implementation can be arbitrarily modified and transformed. Developers use this API to perform commands of log information, by the API to deliver these commands to the appropriate underlying handle. Therefore, for developers, the Logging component is neutralized for any specific underlying implementation.
If you are familiar with Log4j, you should not have any problems with the logging API using your COMMONS. Even if you are not familiar with log4j, just know that you must import two classes using Logging, create a log's static instance, below shows the code of this section:
Import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory;
Public class loggingdemo {private static log log = logfactory.getlog (loggingdeMo.class); // ...}
It is necessary to explain what happened when you call logfactory.getlog (). Calling this function will start a discovery process, that is, identify the implementation of the required underlying logging function, and the specific discovery process is listed below. Note that no matter how the underlying log tool is found, it must be a class that implements the log interface, and must be in the classpath. Commons Logging API provides support for the following underlying logging tools: JDK14Logger, Log4jlogger, LogkitLogger, NOOPLOGGER (directly discarding all log information), there is a SimpleLog.
(1) Commons Logging first looks for a commons-logging.properties file in ClassPath. This attribute file must define the org.apache.commons.logging.log property, which should be the full qualified name implemented by any of the above log interfaces.
(2) If the above step fails, Commons's logging then checks the system properties org.apache.commons.logging.log.
(3) If you can't find the org.apache.commons.logging.log system properties, Logging then looks for the Log4J class in ClassPath. If found, Logging assumes that the application is log4j. However, the property of the log4j itself is still configured by log4j.properties file.
⑷ If the above lookups cannot find the appropriate logging API, but the application is running on JRE 1.4 or later, the JRE 1.4 logging feature is used by default.
⑸ Finally, if the above operation fails, the application will use the built-in SimpleLog. Simplelog outputs all log information directly to System.err. After obtaining an appropriate underlying logging tool, you can start the log information. As a standard API, Commons
The main benefit of the Logging API is to establish an abstraction layer on the basis of the underlying log mechanism, convert the call to the log record command associated with the specific implementation by the abstraction layer.
The sample program provided herein will output a prompt message telling you which underlying log tool is currently using. Try running this program under different environmental configurations, for example, running this program without specifying any properties, then use it by default.
JDK14Logger; then specify system properties-jorg.apache.commons.logging.log = org.apache.commons.
Logging.Impl.SIMPLOG Run the program, then the logging tool will be simplelog; finally, put the log4j class into the classpath, just set the log4j log4j.properties configuration file, you can get the log4jlogger output. Annex: 1.commons-logging.properties File # commons-logging.properties # org.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog # simplelog.properties # # Logging detail level, # # Must Be One of Of ("TRACE", "Debug", "Info", "Warn", "Error", or "Fatal"). # Org.apache.commons.logging.simplelog.defaultlog = traceorg.apache.commons. Logging.log = org.apache.commons.logging.implatt.simplelog