Use the interface into programming interfaces and configuration interfaces, and how to use log facilities in the software system to log registration, configure the operation of the interface planning log facility.
Overall introduction
The reason for Common-logging for Common is that it is a universal log package that is packaged can be log4j, logkit, and specific log systems such as LOG in JSDK 1.4. In the end, the binding is mainly dependent on the binding search strategy of the configuration interface and Common-logging. The log format, log target diversity features mainly rely on the capabilities and configuration of specific log systems.
Log level
At the time of the record log, you should record the log information we need according to the level, such as information that requires a record debug, or the system prompt information, or program operation, and record exception information and record error stack information.
The following is the level of various logs:
JDK1.4 logging is recorded in the java.util.logging.Logging.Logging.Logging.Logging.Level class defines the following log level constant
Server: indicates a serious failure. There will always be a companion of Throwable.
WARNING: Warning information.
? INFO: Medium priority. Indicates what a component is doing (for example, monitoring a task's running progress), but is not used to help debug the component.
? Config: Designed for messages generated during application configuration.
? FINE: Tracking information. The level and lower priority level should be used to help debug some classes, but should not be used to explain the way the application is operated.
Finer: Detailed tracking information.
? Finest: Very detailed tracking information.
Log4j defines the following log level constants in the org.apache.log4j.level class
Fatal: Represents a serious failure. Leading the discontinuation of the application.
? Error: Represents an error event. The Application can continue to run.
Warn: indicates a potential danger.
INFO: Indicates that a component is doing important operation, and the information is relatively rough.
DEBUG: Represents a detailed operation of a component to debug Application.
? All: Represents log information for a record.
Common-logging log level is divided into 6 kinds, from low to heights for TRACE, Debug, Info, Warn, Error, Fatal.
Fatal: Represents a serious failure. Leading the discontinuation of the application.
? Error: Represents an error event. The Application can continue to run.
Warn: indicates a potential danger.
INFO: Indicates that a component is doing important operation, and the information is relatively rough.
DEBUG: Represents a detailed operation of a component to debug Application.
• TRACE: Very detailed tracking information, only used to record the log.
Part of the log level is embodied in the programming interface, and a part is reflected in the configuration interface. Each level has a corresponding convenience method, such as Fatal () and info (), allowing one to be assigned to a message through a method, and recording an exception. Each message must allocate a level in these log levels to ensure that log records are easily controlled at runtime.
Application programming interface
Common-logging application programming interface is mainly defined in org.apache.commons.logging.log interface, this interface primarily defines two types of operations: 1. One type is level judgment, used to reduce unnecessary log operations Calculate to improve performance, function names, and parameters as follows:
Log.isfatalenabled ();
Log.iserrorenabled ();
Log.isswarnenabled ();
Log.isinfoenabled ();
Log.IndebugeNabled ();
Log.IstraceNabled ();
The following code can explain this:
IF (log.Indebugeload ()) {
... some high cost operation ...
Log.debug (theresult);
}
If the level of log facilities are defined above Debug, these high cost operations can avoid running.
2. One type is a log registration, follow the level registration log information, the function names, and parameters as follows:
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);
Log registration operations are also two small classes: a parameter log information registration operation and two parameters log information registration operations. The former applies to the three types of users, the latter used to print the error stack information in the Log Registration, so it is more suitable for developers to adjust and maintain. Note: Trace and Debug encapsulate the debug method of log4j
Configure interface
Commons-logging configuration can be set in the system properties, but this is a bad habit, system properties affect all classes under the same JVM, and is not good to control, so configuring the best position of Commons-logging in Commons-logging .properties file. When configuring Commons-Logging using the properties file, you must put it under the ClassPath of the software system. The following two attribute items can be set in the two attribute items in Commons-logging.properties: org.apache.commons.logging.log Commons-logging default logfactory. Follow the values of this item to instantiate instances of the application programming interface log. If this item is not set, logfactory searches for classes to implement the log interface in the orderpath of the software system:
Log4j
JSDK 1.4
JCL Simplelog
This rule we call it a search strategy. Org.apache.commons.logging.logfactory This item overrides the default LogFactory implementation to meet the specific needs of the application, such as redefining the search strategy.
Sample code
Note: On the two Properies files in the class of the class, the log package will automatically find them, then edit the running program, you can see the log log to get the result.
Commons-logging.properties
# Set the log log
Log4jorg.apache.commons.logging.log = org.apache.commons.logging.Impl.log4jloggerorg.apache.commons.logging.logfactory = org.apache.commons.logging.impl.log4jfactory
Set the log log Using JDK1.4 logging package
# org.apache.commons.logging.log = org.apache.commons.logging.Impl.jdk14logger
# Set the log log to use logkit
# org.apache.commons.logging.log = org.apache.commons.logging.impl.logkitlogger
# Set the log log Using Simplelog
# org.apache.commons.logging.log = org.apache.commons.logging.Impl. Simplelog
2. Log4j.properties
# Log4j will resolve this file
Log4j.debug = false
# 不 清 清 这 这 作 不 不 作 还 还 作 还
Log4j.disableOverride = TRUE
Log4j.disable = info
# Set the priority level of the log of all classes
Log4j.rootlogger = debug, dest1, dest2
# Set this package record log as a fake
# dist1, dist2 will not log the log of com.xxxx.commons.logging.test.loggingSample, only DIST3 will record.
# 反, will record, this will be repeated
Log4j.additivity.com.xxxx.commons.logging.test.loggingSample = false
# Special specified for a particular package log level and target device
Log4j.category.com.xxxx.commons.logging.test.loggingsample = WARN, DEST3
# 本 Target device used to debug
Log4j.Appender.Dest1 = org.apache.log4j.consoleappender
# log4j.Appender.dest1.Layout = org.apache.log4j.simplelayout
Log4j.Appender.Dest1.Layout = org.apache.log4j.patternlayout
#% - 4R% -5P [% T]% 37C% 3X -% M% N
Log4j.Appender.Dest1.Layout.conversionPattern =% D% p% C - <% m> (% F.% m:% L)% T% N
# 本 Target device used to trace
Log4j.Appender.Dest2 = org.apache.log4j.rollingfileappender
# 该 文件 记 记 级 (INFO level is not recorded)
Log4j.Appender.Dest2.threshold = info
# File Save Path
Log4j.Appender.Dest2.file = c: /log4jlog.htm
# 是否 文件 文件 文件 信息
Log4j.Appender.Dest2.Append = TRUE
# Set the maximum value of the file
Log4j.Appender.Dest2.maxfilesize = 5kb
# Set the maximum number of backup files
Log4j.Appender.Dest2.maxbackupindex = 10
# Use an HTML format to log the log
Log4j.Appender.Dest2.Layout = org.apache.log4j.htmllayout
# Whether to print the code line of the message
Log4j.Appender.Dest2.Layout.LocationInfo = true # Set the HTML title of the log
Log4j.Appender.Dest2.Layout.title = my app title
# This target device is used to specify the class or package
Log4j.Appender.Dest3 = org.apache.log4j.dailyrollingFileAppender
# log4j.Appender.Dest3.threshold = debug
Log4j.Appender.Dest3.file = c: /specpackagelog.htm
Log4j.Appender.Dest3.Append = false
Log4j.Appender.Dest3.Layout = org.apache.log4j.htmllayout
Log4j.Appender.Dest3.Layout.LocationInfo = true
Log4j.Appender.Dest3.Layout.title = My App Title 2
Loggingsample.java
Package com.xxxx.commons.logging.test;
Import org.apache.commons.logging. *;
/ **
* Common of Commons-Logging and Log4j
*
title: commons-logging-1_0_4 Source code P>
*
description: commons-logging-1_0_4 Source code p>
*
Copyright: Copyright (C) 2004 P>
*
Company: xxxx p>
* @Author Wang Jian
* @version 1.0
* /
Public class loggingsample {
Private log log = logfactory.getlog (getclass (). getname ());
Public static void main (String [] args) {
Org.apache.log4j.propertyConfigurator.configure ("e: /commons-logging-1_0_4/classes/com/xxxx/commons/logging/test/log4j.properties);
Loggingsample loggingsample = new loggingsample ();
// Trace, Debug, Info, Warn, Error, Fatal
Loggingsample.trace ();
Loggingsample.debug ();
Loggingsample.info ();
Loggingsample.warn ();
Loggingsample. error ();
Loggingsample.fatal ();
// com.xxxx.commons.logging.test.loggingSample
// loggingsample1 = new com.xxx.commons.logging.test.loggingSample ();
// loggingsample1.trace ();
// loggingsample1.debug ();
// loggingsample1.info ();
// loggingsample1.warn ();
// loggingsample1.error ();
// loggingsample1.fatal ();
}
Public loggingsample () {}
Public void trace () {
IF (log.isticraceenabled ())
Log.Trace ("TRACE --- System Start Trace Record Log);
}
Public void debug () {
IF (log.Indebugenabled ())
Log.debug ("Debug --- System Start Debug Record Log");
}
Public void info () {
IF (log.isinfoenabled ())
Log.info ("INFO --- System Start INFO Record Log);
}
Public void warn () {
IF (log.iswarnenabled ())
Log.warn ("Warn --- System Start WARN Record Log);
}
Public void error () {
IF (log.iserrorenabled ())
Log.Error ("Error --- System Start Error Record Log", New Throwable ("Test Log.Error");
}
Public void fat () {
IF (log.isfatalenabled ())
Log.fatal ("Fatal --- System Start Fatal Record Log", New Throwable ("Test Log.FATAL");
}
}
to sum up
It is recommended to try several cases when using the configuration file, so you will soon understand the usage of those configurations.