Apache commons-logging Usage Examples
This article describes how to use Apache commons-logging in your programAUTHOR:ZJ 07-3-17blog: [Url]http://zhangjunhd.blog.51cto.com/[/url]
1
.
Commons-loggin
IntroductionJakarta Commons Logging (JCL) provides a log interface (interface), taking into account both lightweight and non-dependent log implementation tools. It provides a simple log manipulation abstraction for middleware/log tool developers, allowing program developers to use different specific log implementation tools. Users are assumed to be familiar with the higher-level details of some log implementation tool. The JCL provides a simple wrapper for some other logging tools, including log4j, Avalon Logkit, and JDK 1.4, which is closer to the implementation of log4j and Logkit.
2
. Quick StartJCL has two basic abstract classes: Log (basic logger) and logfactory (responsible for creating log instances). When Commons-logging.jar is added to Classpath, it will reasonably guess the log tool you want to use and then set itself up so that the user does not need to do any settings at all. The default logfactory is to follow the steps below to discover and decide which log tool will be used (in order, the search process will be aborted when the first tool is found): 1. Look for the value 2 in the current factory named Org.apache.commons.logging.Log configuration property. Find the value 3 in the property named Org.apache.commons.logging.Log in the system. If there is log4j in the application's classpath, the related wrapper (wrapper) class (Log4jlogger) 4 is used. If the application is running on a jdk1.4 system, use the related wrapper class (Jdk14logger) 5. The specific implementation of the simple log wrapper class (Simplelog) Org.apache.commons.logging.Log is as follows:-org.apache.commons.logging.impl.jdk14logger Use JDK1.4. -org.apache.commons.logging.impl.log4jlogger use log4j. -org.apache.commons.logging.impl.logkitlogger use Avalon-logkit. -org.apache.commons.logging.impl.simplelog common-logging comes with a log implementation class. It implements the log interface and outputs the log messages to the system error stream system.err. -org.apache.commons.logging.impl.nooplog common-logging comes with a log implementation class. It implements the log interface. No action is made in the method of its output log.
3
. Using JCL developmentBecause log4j is strong, and developers do not want to be too dependent on log4j. So at present the more popular is commons-logging and log4j combination use. 1. The deployment logger downloads the Commons-logging.jar and log4j.jar packages, enabling them to be placed in the project's Lib directory and introduced into the project. 2. Specify the logger to set the class that implements the interface in the properties file common-logging.properties. The following (set log4j as the log package used here):
#commons-logging.properties file configuration information # org.apache.commons.logging.log=org.apache.commons.logging.impl.simplelog# Must be one of ("Trace", "Debug", "Info", "Warn", "error", or "fatal"). #利用log4j为输出介质org. Apache.commons.logging.log=org.apache.commons.logging.impl.log4jcategorylog #JDK5 Logger# Org.apache.commons.logging.log=org.apache.commons.logging.impl.jdk14logger |
3. The methods defined in the Org.apache.commons.logging.Log interface are in the order of severity from highest to lowest:
Log.fatal (Object message), Log.fatal (object message, Throwable t), Log.error (Object message), Log.error (Object message, Throwable t); Log.warn (object message); Log.warn (Object message, Throwable t); Log.info (object message); Log.info (object message, Throwable t); Log.debug (object message); Log.debug (Object message, Throwable t); Log.trace (object message); Log.trace (Object message, Throwable t); |
In addition to this, the following methods are provided for code protection.
Log.isfatalenabled (); log.iserrorenabled (); log.iswarnenabled (); log.isinfoenabled (); log.isdebugenabled (); Log.istraceenabled (); |
4. The information level is important to ensure that the log information is appropriate for the content and the severity of the response problem. 1) Fatal a very serious error that caused the system to abort. It is expected that such information will be displayed immediately on the status console. 2) Error Other run-time errors or not expected conditions. It is expected that such information will be displayed immediately on the status console. 3) Warn uses deprecated APIs, very poorly used APIs, ' almost ' errors, other run-time undesirable and unexpected states but not necessarily called "errors." It is expected that such information will be displayed immediately on the status console. 4) A meaningful event generated by info runtime. It is expected that such information will be displayed immediately on the status console. 5) Detailed information in the debug system process. It is expected that this type of information is only written to the log file. 6) Trace for more detailed information. It is expected that this type of information is only written to the log file. Typically, the level of the logger should not be less than info. That is, the debug information should not normally be written to the log file.
4
.
Apache commons-logging
Use Process1) Add the Common-logging.jar package to the environment variable or classpath. 2) Import Org.apache.commons.logging.Log; Org.apache.commons.logging.LogFactory; and related package 3) Get the log instance where you want to use logging.
Private Static Log Log = Logfactory. GetLog (Test.class); |
4) Use the Logger object's debug,info,fatal ... Method.
log. Debug ("Debug Info."); |
5.Apache commons-logging
using the exampleTest.java
PackageSampleImportOrg.apache.commons.logging.Log;ImportOrg.apache.commons.logging.LogFactory; Public classTest {PrivateStaticLogLog= Logfactory.GetLog(Test.class); PublicvoidLog () {Log. Debug ("Debug Info.");Log. info ("info info");Log. Warn ("Warn info");Log. Error ("error info");Log. Fatal ("fatal info"); } PublicStaticvoidMain (string[] args) {test test =NewTest (); Test.log (); }} |
Results:DEBUG Sample. Test.log (test.java:13) Debug Info.info Sample. Test.log (test.java:14) Info Infowarn Sample. Test.log (test.java:15) Warn Infoerror Sample. Test.log (test.java:16) Error Infofatal Sample. Test.log (test.java:17Fatal Info When there is no configuration file (. properties), it is just like the result. At this point, it uses the simple log wrapper class (Simplelog). Add the package and configuration file below to make it use log4j. 1) Add config file commons-logging.properties and log4j.properties. 2) Add Log4j.jar and Common-logging.jar two packages to the environment variable or classpath. 3)Test.javaContent does not change. 3)commons-logging.properties
Org.apache.commons.logging.log=org.apache.commons.logging.impl.log4jcategorylog |
4)log4j.properties
Log4j.rootlogger=info, Stdoutlog4j.appender.stdout=org.apache.log4j.consoleappenderlog4j.appender.stdout.layout =org.apache.log4j.patternlayout# Pattern to output the caller ' s file name and line number.log4j.appender.stdout.layout.conversionpattern=%5p [%t]-%m%n |
Results:
info [main]-Info infowarn [main]-Warn infoerror [main]-Error infofatal [main]-Fatal info |
6.Apache Log
Introduction Set[1]
Apache log4j Configuration Instructions[2]
Apache log4j Usage examples[3]
Apache commons-logging Usage examples[4]
How to build a self-appender extended log4j Framework
7
. References[1] Official documentation [2] Jakarta Commons Logging (JCL) Development notes [url]http://blog.csdn.net/fasttalk/archive/[/url]2005/06/29/406681. aspx
This article is from the "sub-larvae" blog, make sure to keep this source http://zhangjunhd.blog.51cto.com/113473/25135
Apache commons-logging Usage Examples