The use method of Jakarta Commons logging

Source: Internet
Author: User
Tags log log log4j

Log (Logging) enables us to debug and track the behavior and state of an application at any time. Logging is an integral part of any larger application, so there are already a number of Third-party logging tools that eliminate the developer's own authoring of logging APIs. In fact, even the JDK has a constructed logging API. Now that you have so many choices (Log4j,jdk,logkit, etc.), usually we can always find a ready-made API that best suits our application requirements.

However, there may be exceptions, such as a familiar logging API that is not compatible with the current application, either due to some kind of hard rule or due to the application's architectural reasons. The Commons Project logging component is to encapsulate the logging functionality as a standard set of APIs, but its underlying implementation can be arbitrarily modified and transformed. Developers use this API to execute commands to log information, and the API decides to pass these commands to the appropriate underlying handle. Therefore, for developers, the logging component is neutral for any specific underlying implementation.

If you are familiar with log4j, there should be no problem using the Commons logging API. Even if you are unfamiliar with log4j, as long as you know that using logging must import two classes, create a static instance of log, the following shows the code of this part of the operation:

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 elaborate on what happens when you invoke Logfactory.getlog (). Calling this function launches a discovery process that identifies the implementation of the required underlying logging functionality, which is listed below. Note that regardless of how the underlying log tool is found, 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 low-level logging tools: Jdk14logger,log4jlogger,logkitlogger,nooplogger (discarding all log information directly) and a simplelog.

⑴commons's logging first looked for a commons-logging.properties file in Classpath. This property file must at least define the Org.apache.commons.logging.Log attribute, and its value should be the fully qualified name of the implementation of any of these log interfaces.

⑵ If the previous step fails, Commons's logging then checks the System attribute Org.apache.commons.logging.Log.

⑶ if the Org.apache.commons.logging.Log system attribute is not found, logging then looks for the log4j class in Classpath. If found, logging assumes that the application is using LOG4J. However, the properties of log4j itself are still configured correctly through the Log4j.properties file.

⑷ If none of the above lookups can find an 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 all log information directly to System.err.

After you get the appropriate underlying logging tool, you can then start logging information. As a standard of api,commons

The main benefit of the Logging API is the creation of an abstraction layer based on the underlying log mechanism, which transforms the call into a logging command related to a specific implementation through an abstraction layer.

The sample program provided in this article outputs a hint that tells you which underlying log tool is currently being used. Try running the program under different environment configurations, such as running the program without specifying any attributes, by default

Jdk14logger, and then specifies the system Properties-jorg.apache.commons.logging.log=org.apache.commons.

Logging.impl.SimpleLog then run the program, then the logging tool will be simplelog, and finally, put the log4j class into the classpath, as long as the log4j log4j.properties configuration file is set up correctly, you can get Log4jlogger The output information.

Report:
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 ("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.