Commons Reading Notes: Logging

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.