Introduction
Command Line Parameter Parsing and Application Program Configuration and logging, as the skeleton of an application, can be seen everywhere. Therefore, the Apache Software organization has developed a set of common class libraries to help software developers build these "skeleton. Where:
& #8226; commons CLI for command line Parsing
& #8226; commons configuration is used to read configuration information in properties or XML format
& #8226; commons logging and log4j are used to provide log support.
These general class libraries are all downloaded from the region on http://jakarta.apache.org/commons/index.htmlnet.
Apache developed a set of log4j to support logging. Java 1.4 also introduced a set of built-in logging frameworks. If developers want to switch between the two sets of logging systems freely, what should I do? The answer is to use commons logging. Commons logging defines a set of abstract logging interfaces that you can configure to direct to any existing logging system.
& #8226; Use the abstract logging Interface
Problem:
You need to write log information when writing a library that can be used repeatedly, but you do not want to bind your logging function to Apache log4j or JDK 1.4 logging framework.
Solution:
Public static void main (string [] ARGs) {// Replace []
system. setproperty ("org. apache. commons. logging. log ",
" org. apache. commons. logging. impl. jdk14logger ");
log = logfactory. getlog ("com. discursive. jccook. someapp ");
If (log. istraceenabled () {
log. trace ("this is a trace message");
}< br>
If (log. isdebugenabled () {
log. debug ("this is a debug message");
}< br>
log.info ("this is an informational message");
log. warn ("this is a warning");
log. error ("this is an error");
log. fatal ("this is fatal");
}< br>
The logfactory. getlog method returns an appropriate log implementation based on the underlying environment. If you want to specify a specific logging system implementation, you can set the org. Apache. commons. Logging. Log System attribute. For example:
System. setproperty ("org. Apache. commons. Logging. log ",
"Org. Apache. commons. Logging. impl. log4jlogger ");
In this way, log4j will be used as the logging system.
Org. Apache. commons. Logging. log can be set:
& #8226; org. Apache. commons. Logging. impl. log4jlogger use log4j
& #8226; org. Apache. commons. Logging. impl. jdk14logger use the JDK 1.4 logging framework
& #8226; org. Apache. commons. Logging. impl. simplelog simple log implementation built in commons Logging
Others:
To sum up, commons logging will specify the specific log implementation in the following order.
& #8226; if the org. Apache. commons. Logging. log system parameter is defined, the specified logging implementation is used.
& #8226; If log4j is found in classpath, use log4j.
& #8226; If jdk1.4 is used, use the built-in logging framework of jdk1.4.
& #8226; if none of them are found, use the simple log implementation built in commons logging.
Translated:
Dancing with Java-porridge
From: http://www.matrix.org.cn/resource/article/1/1436.html