How to Use Jakarta commons Logging

Source: Internet
Author: User

Logging allows us to debug and track the behavior and status of an application at any time. Logging is an indispensable part of any large-scale application. Therefore, there are already many third-party logging tools that allow developers to write their own logging APIs. In fact, even JDK has a constructed logging API. Since there are already so many options (log4j, JDK, logkit, and so on), we can always find the ready-made API that best suits our application requirements.

However, exceptions may also occur. For example, a familiar logging API cannot be compatible with the current application, either due to a rigid rule or the application architecture. The logging component of the Commons project encapsulates the logging function into a set of standard APIs, but its underlying implementation can be modified and changed at will. Developers use this API to execute the commands that record log information. The API decides to pass these commands to appropriate underlying handles. Therefore, for developers, the logging component is neutral for any specific underlying implementation.

If you are familiar with log4j, there should be no problems using the commons logging API. Even if you are not familiar with log4j, as long as you know that logging must be used to import two classes and create a static log instance, the code for this operation is shown below:

Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;

Public class loggingdemo {
Private Static log = logfactory. getlog (loggingdemo. Class );
//...
}
 

It is necessary to describe in detail what happened when logfactory. getlog () is called. Calling this function starts a discovery process, that is, to find the implementation of the required underlying logging function. The specific discovery process is listed below. Note that no matter how the underlying log tool finds it, it must be a class that implements the log interface and must be in classpath. The commons logging API directly provides support for the following underlying logging tools: jdk14logger, log4jlogger, logkitlogger, and nooplogger (directly discarding all log information), and a simplelog.

(1) commons logging first looks for a commons-logging.properties file in classpath. This attribute file must at least define the org. Apache. commons. Logging. Log attribute. Its value should be the complete qualified name implemented by any of the preceding log interfaces.

(2) If the above step fails, the logging of commons will then check the system attribute org. Apache. commons. Logging. log.

(3) If the org. Apache. commons. Logging. Log System attribute cannot be found, logging searches for the log4j class in classpath. If logging is found, it is assumed that the application uses log4j. However, the log4j attributes must be correctly configured through the log4j. properties file.

(4) If no appropriate logging API can be found for the above search, but the application is running on JRE 1.4 or later, the logging function of JRE 1.4 is used by default.

Finally, if all the above operations fail, the application will use the built-in simplelog. Simplelog directly outputs all log information to system. Err.

After obtaining the appropriate underlying logging tool, you can start to record the log information. As a standard API, commons

The main advantage of the logging API is that an abstraction layer is established based on the underlying Log Mechanism and calls are converted into log record commands related to specific implementations through the abstraction layer.

The sample program provided in this article will output a prompt to tell you which underlying log tool is currently in use. Run this program in different environment configurations. For example, if you run this program without specifying any attributes

Jdk14logger; then specify the system property-Jorg. Apache. commons. Logging. log = org. Apache. commons.

Logging. impl. simplelog then runs the program, and the logging tool will be simplelog; finally, put the log4j class into classpath, as long as the log4j log4j is set correctly. properties configuration file to obtain the information output by log4jlogger.

Appendix:
1. commons-logging.properties files
# Commons-logging.properties
# Org. Apache. commons. Logging. log = org. Apache. commons. Logging. impl. simplelog
# Simplelog. Properties
# Logging detail level,
# Must be one of ("trace", "debug", "info", "Warn", "error", or "Fatal ").
# Org. Apache. commons. Logging. simplelog. defaultlog = trace
Org. Apache. commons. Logging. log = org. Apache. commons. Logging. impl. simplelog

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.