Org.apache.commons.logging Introduction
Simple Packaging Package for Multi-Log APIs API
Overview
This package provides an API to log processing of server-side programs to use a variety of different log systems. Includes as follows:
· Log4j Apache Jakarta project. Each log of each log corresponds to a Log4j Category class.
· JDK Logging API JDK1.4 and subsequent versions. Each log of each log is a java.util.logging.logger instance.
· Logkit Apache Jakarta project. Each log of each log corresponds to a Logkit Logger class.
· NOOPLOG simply accepts the log output of all Log instances.
· Simplelog outputs the log of all Log instances into System.out.
Quick start wizard
Don't worry, continue, the following example describes the typical declarations and use of Logger (named with the class name of the call):
Import org.apache.commons.logging.log;
Import org.apache.commons.logging.logfacory;
Public class foo {
Log log = logfactory.getlog (this.calss);
Public void foo () {
...
Try {
IF (log.Indebugeload ()) {
Log.debug ("About to Do Something To Object" Name);
}
Name.bar ();
} catch (IllegalStateException E) {
Log.Error ("Something Bad Happened to" Name, E);
}
...
}
}
All log outputs will be abandon unless you are configured. So you really want to see the rest of this page to understand how to configure your log system.
Configuring a Commons Logging Package
Select a logfactory implementation
From an application view, first, it is to load an object that references the logFactory instance to create a log instance for this app. This is usually done by calling the static getFactory () method. This method implements the following discovery algorithm to select the name of the logfactory implementation class and use it in the app:
· Check the system properties of org.apache.commons.logging.logfactory.
· Use JDK 1.3 JAR service discovery mechanism (see
Http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html gets more information) to find resources named meta-inf / services / org.apache.commons.logging.logfactory, The first line contains both the needs of the needs.
• Find a property file called Common-logging.Properties in the application's classpath, where the org.apache.commons.logging.logfactory property defines the name of the desired implementation class.
· Back to the default implementation, this next will be introduced.
If you find a commons-logging.properties file, all properties defined will be used to configure the properties of the LogFactory instance.
Once a class name that implements a class is selected, the corresponding class will be loaded from the current thread's class-loader (if any), or load LogFactory from the class loader. This will allow copies of common loads (such as servlet containers), but still allow each web application to provide your own logfactory implementation, if needed. An instance of this class will be created and default logfactory implementation
A default implementation class in the log package APIS
(Org.apache.commons.logging.impl.logfactoryImpl), when no other implementation classes are found, he will choose him. His main purpose is to create (if needed) by calling the getInstance () method and returns a log instance. The following rules are used by default:
· Only one log instance of the same name is created. The GetInstance () method using the same name or class parameter will call the same logfactory instance and will return the same Log instance.
· When a log instance is indeed created, the default logfactory implements the following discovery mechanism:
· Find org.apache.commons.logging.log system properties (in order to compatibility with the previous version of the API, org.apache.commons.logging.log system properties should not be considered).
· Find factory configuration properties called org.apache.commons.logging.log.
• If the log4j log system is valid in the ClassPath of the application, use the corresponding class package (log4jcategorylog).
· If the application uses JDK1.4 systems, use the JDK14Logger package.
• If all, return to the default non-output log package (NoPlog).
• Load this designated class from the thread class loader (any), or load Logfacory from the class loader.
· Example of an instance of a selected log implementation class, transmitting this specified name as the unique parameter to its constructor.
If you want the current log to output it on System.Out, but no one of the three provided log packs is installed, a simple Log implementation called SimpleLog will take effect. Based on the above rules you can choose it, in the command line containing a system properties definition to launch your app:
Java /
-D org.apache.commons.logging.log = org.apache.commons.logging.impla.SIMPLOG /
MyApplication
See SimpleLog's Javadoc to get detailed configuration information for this implementation.
Configuring the priority of the log system
The basic principle is to be responsible for the user to the priority log system. Common-logging does not change the existing configuration.
Each individual log implementation can provide his own configuration properties. This will be explained in the class publishing of the corresponding implementation class.
Finally, some LOG implementations (such as log4j) require an external configuration file for the entire log system. This file needs to be prepared with a special method in the actual log usage.
Use log package APIS
Use the log API in the application component as follows.
1. Get a reference to the factory method logfactory.getInstance (String Name), your application can contain a variety of log systems to cope with various purposes. A typical solution is to use their own log instances for each of the servers.
2. The information is recorded by calling the appropriate method (DEBUG (), INFO (), Warn (), Error (), and Fatal ()) (if the corresponding level is available). For example: You can initialize and use a Log instance in your program part:
Import org.apache.commons.logging.log;
Import org.apache.commons.logging.logfactory;
Public class mycomponent {
protected log log = logfactory.getlog ("my.component");
// Called ONCE AT Startup Time
Public void start () {
...
Log.info ("MyComponent Started");
...
}
// Called Once At Shudown Time
Public void stop () {
...
Log.info ("MyComponent Stopped");
...
}
// Called Repeatedly to Process A Particular Argument Value
// Which you want logged if debugging is enabled
Public void process (string value) {
...
// Do Thie THRING ConcateNation Only Jif Logging is enabled
IF (log.IndebugeNable ())
Log.debug ("MyComponent Processing" Value;
...
}
}