Good use of Logging API: Part2
category
Pirates - Ricky @ 16:26
Java Classroom> Good use of Logging API: Part 2 - Jakarta Commons Logging Log4J (Jingwen Technical College Directory, Wu Mingzhe), preface:
Previously, we have learned how to use log4j, although log4j is a powerful logging library, but sometimes we want our program to have higher flexibility, without limiting above a particular library function. In order to solve this problem, we use Jakarta CommonSlogging to achieve this purpose. Second, what is JAKARTA COMMONS logging?
Detailed information can be obtained from the following URLs
http://jakarta.apache.org/commons/logging.html
Simple Jakarta Commons Logging is a set of small and simple universal log libraries. It is simple to say that it is a universal package (Package) for multiple Logging APIs, that is, we can pass it. The interface is dynamically changing the Logging library used without remembering the original program. The Logging API supported by Commons Logging is:
1.log4j (http://jakarta.apache.org/log4j) 2.Logkit (http://jakarta.apache/avalon/logkit) 3.jdk1.4 logging (http://java.sun.com/) 4.SIMPLOG: Using standard stdout and stderr5.nooplog: Ignore all Log messages
Third, obtain and install:
1. You can get the latest version from you can get from your website http://jakarta.apache/commons/logging.html
2. Unzip
3. Place commons-logging.jar in your ClassPath path or in the webapp / web-inf / lib path
※ Note: Commons-logging only contains the implementation of Simplelog and NOOPLOG, if you want to use other logging APIs, please download other libraries.
4. Set common-logging.properties: Before using Common-logging, you must first decide your logging, the following methods can let you define your logging work.
(1) Use System Properties to define org.apache.commons logging.logfactory: Example: Java
-Dorg.apache.commons.logging.log = org.apache.commons.logging.Impl.SIMPLOGMYAPPLICATION
(2) Use the JDK1.3 JAR Services Discovery mechanism: Place meta-inf / services / org.apache.commons.logging.logfactory in your JAR file and define the actual example you want to use in the first line: (Org. Apache.commons.logging.log = org.apache.commons.logging.impl.simplelog)
(3) Place the commons-logging.properties attribute file and your .class in the same path and plus a line in it org.apache.commons.logging.logging.IR.Paache.commons.logging.Impl.simpland ※ This Is the simplest method, it is also the method we want to introduce this article
5. Use the preset work to find that function, the order is:
(1) Looking for System Property (2) Use org.apache.commons.logging.log defined value (3) Looking for log4jcategoryLog (4) Do you use JDK1.4, if you use JDK1.4 Logger (5), use NOOPLOG is actually, that is, deactivated LOG functionality
Fourth, use and sample:
A1: Write the following code
Archive: Testlog4j.java
// At the beginning we need to enter import org.apache.commons.logging.logfactory // and Import Org.Apache.commons.logging.log
Import org.apache.commons.logging.logfactory; import org.apache.commons.logging.log;
Public Class Commlogtest {Public Static Void Main (String [] args) {
// From logfactory to initialize a log's actual (such as SimpleLog), all of our messages will be stored in this log
Log log = logfactory.getlog (Commlyst.class);
// The following uses Commons-logging 6 levels to set Log Information Log.Trace ("This is TRACE Histance Information"); log.debug ("This is debug hierarchical information"; log.info ("This is info Hierarchical information "); log.error (" This is Error Hollege Information "); log.warn (" This is WARN Hierarchical Information); log.fatal ("This is Fatal Hierarchical Information");
}
A2: Please put a file with a filemons-logging.properties in the same directory with .CLASS (COMMLOGTEST.CLASS) or the Web-INF / CLASSES directory under the Web-INF / CLASSES directory is as follows:
Archive: comMMons-logging.properties
# Use noPlog, deactivate log function # org.apache.commons.logging.log = org.apache.commons.logging.Impl.nooplog
# Use SimpleLog, we can use SimpleLog.properties to further control # This article uses this set of interfaces to display basic log messages org.apache.commons.logging.logging. Impl.simplelog
# Use log4jcategoryLog, we can use log4j to make further control # org.apache.commons.logging.log = org.apache.commons.logging.Impl.log4jcategoryLog
A3: Because we currently use SimpleLog to process our log, we need another property file to make further settings for the interface of SimpleLog, put another file with Simplelog.properties, put the same directory with .class Under the next or Web-INF / CLASSES directory, its contents are as follows: # Preset LogGing's execution level. The option must be "trace", "debug", "info", "warn", "error", "Fatal" one Item. # If you don't define the preset is "info".
Org.apache.commons.logging.simplelog.defaultlog = Error
# Specific SimpleLog Instance's Logging Execute Hierarchy, we must specify its name, in this case, simpletest # If its value is not defined, the preset is the value defined above the defaultlog definition.
Org.apache.commons.logging.simplelog.log.commlogtest = INFO
# Set whether you want to include the name of the Class in the output message, in this case, it is a commlogtest. Preset is False
Org.apache.commons.logging.SIMPLOG.SHOWLOGNAME = TRUE
# Set the name of the Class in a shorter format, in this case is CommLogtest. Preset is Trueorg.apache.commons.logging.SIMPLOG.SHOWSHORTLOGNAME = FALSE
# Set whether or not to include the current time and date in the output message. Preset is false
Org.apache.commons.logging.simplelog.showdatetime = true
A3: The output is as follows
C: Commlogtest> Java CommLogtest2003 / 03/12 01: 02: 49: 727 PST [Error] This is Error level information 2003/03/12 01: 02: 49: 727 PST [WARN] This is WARN level information 2003/03 / 12 01: 02: 49: 727 PST [Fatal] This is Fatal Hierarchy Information
V. Advanced Use - Match the log4j from above, the output of you may feel too simple, or you want to use other logging APIs to replace the preset Simplelog
Let's continue to introduce how to integrate log4j
A1: (1) Place a file name log4j.properties in the same directory with .class or the Web-INF / CLASSES directory under the Web-INF / CLASSES directory is as follows:
######################################################################################################################################################################################################################################################################################################## #############
# Set the Root Logger level to INFO and use SimpleConsole, RollingFile two Appende
Log4j.rootlogger = info, SimpleConsole
# SimpleConsole PatternLayout used as the output format log4j.appender.SimpleConsole = org.apache.log4j.ConsoleAppenderlog4j.appender.SimpleConsole.layout = org.apache.log4j.PatternLayoutlog4j.appender.SimpleConsole.layout.ConversionPattern = [% t]% -5p% C% D -% M% N
######################################################################################################################################################################################################################################################################################################## ##### (2) Add the Dist / lib / log4j-1.2.x.jar path to your ClassPath environment variable (EX: C: / log4j / lib / lob4j), or put it in webapp / web -Inf / lib
A2: Please modify the attribute values within Commons-logging.properties as follows:
Org.apache.commons.logging.log = org.apache.commons.logging.implatt.simplelog
Change to
Org.apache.commons.logging.log = org.apache.commons.logging.Impl.log4jcategoryLog
A3: The output is as follows
C: CommLogtest [main] info commandest 2003-03-12 01: 09: 14,791 - This is the info level information [main] error Commlyst 2003-03-12 01: 09: 14,791 - This is ERROR level information [main ] WARN CommLogte 2003-03-12 01: 09: 14,791 - This is WARN level information [main] Fatal CommLogte 2003-03-12 01: 09: 14,791 - This is Fatal Hierarchy Information
※ If you have any questions about how to use log4j, you can refer to the previous article.
Sixth, the end:
Using the Commons-Logging API, we can use more flexible practices to write our log. At present, all kinds of sub-projects under the Apache Jakarta project (such as Struts) have also used the Commons-Logging API to build from Log Information. This article discusses how to match the log4j with commons-logging, allowing us to have the logging of elasticity and readability to other practical (such as jdk1.4 logging), please have interest Refer to its own javadoc.