1.0 Java Logging Overview 1.1 Overview of Control Flow 1.2 Log Levels 1.3 Loggers 1.4 Logging Methods 1.5 Handlers 1.6 Formatters 1.7 The LogManager 1.8 Configuration File 1.9 Default Configuration 1.10 Dynamic Configuration Updates 1.11 Native Methods 1.12 XML DTD 1.13 Unique Message IDs 1.14 Security 1.15 Configuration Management 1.16 Packaging 1.17 Localization 1.18 Remote Access and Serialization2.0 Examples 2.1 Simple Use 2.2 Changing the Configuration 2.3 Simple Use, Ignoring Global Configuration 2.4 Sample XML Output3.0 Appendix A: DTD for XMLFormatter Output
1.0 javatm logging overview
Logging APIs in J2se API Specification. It has been described in detail. The goal of this document is to provide a summary of key elements.
1.1 OverView of Control Flow Control Somarity
The application calls the Logger object to log the log. (Example 1.1) Loggers is organized in a hierarchy, and the child logger may inherit some properties from their parent Logger. (Example 1.2)
The application calls the Logger object to log the log. These logger objects locate the LogRecord object, which is some objects that are transmitted to the Handler object (Example 1.3). Both loggers and handlers use the log level, and (optional) filters to determine if they are interested in a particular record (Example 1.4). When a record is required, a handler can use a formatter to locate and format this information, before publishing to the I / O stream (Example 1.5).
Each Logger keeps tracking some outputs of Handlers. By default, all Loggers also send their output to their parent Logger (Example 1.5). However, Logger may also be configured to ignore the upper Handlers. (Example 1.6)
Some Handlers may output them directly to other handlers. For example, MemoryHandler maintains a logRecord's internal ring buffer, and is triggering the event it publishes its logRecords through a target handler. In this case, any information is the last Handler processing of the chain.
These APIs are structural, so even if Logging is disabled, the Logger API is also lightweight. If Logging is disabled at a given log level, the logger performs a lightweight comparison test and returns. If Logging is enabled at the specified level, Logger still carefully decreases as much as possible, when passing LogRecord to Handlers. In particular, localization and formatting are delayed when the Handler requests them. For example, a MEMORYHANDLER maintains a log buffer of a LogRecords, where there is no time to format.
2 log levels
Each log information has a related log level. This level is the importance and urgency of the rough guidance of log information. The log level object is encapsulated into an integer, and the higher value represents a higher priority. The Level class defines seven standard log levels. Range from Finest (minimum priority, minimum) to Server (highest priority group, maximum)
3loggers
As specified above, the customer code sends a log request to the Logger object. Each Logger keeps tracking it to be interested in log levels and discards logs that are smaller than this level.
Loggers is usually naming in an entity, using a point separator name, such as "java.awt". This namespace is hierarchical and is managed by LogManager. This namespace should usually be named space organization in Java. But don't have to follow .. For example, a Logger name is "java.awt" is possible to handle log requests for a Java.awt package, but may also process a log request for a class in Sun.AWT.
Outside, you can also create an anonymous logger, a Logger that does not appear in sharing namespace. See 1.14.
Logger tracks their parent Logger in log namespace. The father of a logger is its nearest ancestor, in the log namespace. Root logger (name ") No father. Anonymous logger area All Given the root logger as their parent. Logger It may inherit multiple properties from their parent (in logger namespace). Special, one logger may inherit:
* Log Level. If a logger level is set to null, then this log will inherit the first non-NULL level from the parent log.
* Handlers. Default A logger will record any output information to its parent Handlers., And recursive in the tree.
* Resource package name. If a log has a NULL resource package name, then it will inherit any resource package defined to its parent log and recursively in the tree.
1.4 logging methods
The Logger class provides a large amount of convenient way to generate log information. For convenience, there is a method for each level, named "Loger.log (constants.waring, ..." developer can call the convenience ": Logger.warning (... "
There are two log styles, adapt to users of different styles.
First, there is a method of directly acquiring the source and source method. These methods are used to use the source for developers, some sources that want to quickly locate log information. This style is:
Void Warning (String Source Class, String Source Method, String MSG);
Second, there are many ways to get the source and source method names. These methods are used to use developers, and some developers who want to easily use Logging and do not need detail source information.
The second legal party, the Logging framework will try to detect that class and method call logging, and add this information to LogRecord .. However, it is important that automatic detection may be just about approximately approximate.
1.5 Handlers
StreamHandler
ConsoleHandler
FILEHANDLER
SocketHandler
MemoryHandler
1.6 Formatters
SimpleFormatter
XMLFormatter
1. 7 logmanager
There is a global logmanager object tracking global log information. This includes:
Logger's hierarchy namespace.
Logging control properties read by profile
There is only one LogManager object to get it by using a static logmanager.getlogManager method.
1.8 Configuration File
Logging configuration can be initially initialized using a logging profile (which will be read). This profile is implemented in the standard format of Java.util.Properties.
The location and usage of the default configuration file is as follows:
C: / program files / java / jdk1.5.0 / jre / lib / logging.propertiesd: / java> java -djava.util.logging.config.file = d: /java/logging.properties testLogG
ER
Another option, the logging configuration can be initialized by a class (which can be used to read the initialization information). This mechanism is allowed to read from a variety of source, such as LDAP, JDBC, and more. See the LogManager API Specification .
There are some global configuration information. This information has been described in detail in the LogManager description and contains a series of root-level handlers that is installed in the startup.
The initialization configuration can specify a level for specific loggers. These levels 衩 apply the commanded logger and any Logger below it below its command level. Various levels are applied in their order defined in the configuration file.
The initial configuration can contain any properties for use for Handlers or run the log subsystem. For convenience, these properties should begin with the sentence of the Handler class or the primary logger.
For example, MemoryHandler uses an attribute "java.util.Logging.MemoryHandler.Size" to determine the default Ring buffer size.
1.9 Default Configuration
The default configuration on JRE is just a default, which can be changed by ISVS, system administrator, and end users.
The default configuration only limits disk space. This is not provided to user flood information, but guarantees the capture of key error messages.
The default configuration encapsulates a separate handler in root logger, used to send information to the console.
1.10 Dynamic Configuration Updates
The program can update the logging configuration in the runtime, using any of the following methods:
FileHandlers, MemoryHandlers and PrintHandlers can be created in a variety of properties.
New Handlers can be added, and you can delete it.
The new Logger can be created and supported by specific Handlers.
Level can be set on Handlers.
1. 11 private method
Logging no private method
1.12XML DTD
XML DTD is used by XMLFormatter.
DTD is designed to be a top-level document with "
Note that in the event of the JVM crash, it may not be able to terminate an XMLFormatTer stream with log> attribute. Therefore, the tool to analyze log records should be prepared to have no end.
1.13 Unique Message IDS
The Java API does not provide this feature, so the program must be implemented by itself and included in the information string.
1. 14security
The main installation requirements are untrustworthy code that cannot be changed,. Special, if the log configuration has been set to record a system log information to a handler. These, the information is not possible to block or interrupt the log.
A new installation permission LoggingPermission is defined to control more log configuration.
The trusted program can call any log configure the API. Untrusted Applets is another thing. Do not trust Applets can create and use the named loggers normally. However, they do not allow changing log control settings. For example, add and remove Handlers. Or change the log level. However, you don't trust Applets to create their own "anonymous" logs. And use it. Anonymous logs are not registered in global namespaces, and they have no access check, allowing them to change their log control even if they don't trust Set.
The log frame does not attempt to prevent flood attacks. The reliability of the log generating source cannot be detected. Since a logging from a particular source class and source method is required to be released, it may be forged! Similarly, if XMLFormatter is like formatter Do not try to prevent the nested log information in the information string. This way, a fraudulent log record may contain a spoofing XML record inside, its string makes it two to look like another XML record.
In addition, the log framework does not attempt to protect yourself to prevent the hormon attack. Any customer can hide important information on the log frame with unmanifestinity information, implement the flood attack ..1. 15 Configuration Management
The API is structured, so the initialization information is read as an attribute from a configuration file .. Configuration information may change by calling a variety of log classs and object changes.
In addition, there is a method in the LogManager to allow the configuration file to be reread. When this occurs, the configuration file value overrides any value that is changed.
1. 16Packaging
All log classs are in Java. * Namespace, in the java.util.loging package.
1.17 Localization Localization
Day information may need to be localized.
Each Logger may have a resource package related to its name. The corresponding resource package can be used to image the raw string and the local string.
Typically localization will be performed by Formatters. For convenience, the Formatter class provides a Formamessage method to provide some basic localization and formatting support ..
1. 18 Remote method and serialization
As most Java platform APIS. Log APIS settings are used to use internal spaces. All calls are intended to local. However, sometimes some Handlers may want to forward their output to other systems. There are several ways to do these things.
Some Handlers (such as SocketHandler) may write data to additional systems using XMLFormatter. This provides a simple, standard, format to resolve and execute in a variety of systems.
Some Handlers may wish to transfer LogRecord objects via RMI. LogRecord class is therefore possible. However, one problem is how to process logRecord parameters. Some parameters may not serialize, and other systems may not be designed to serialize Such a large state. To solve this problem, the LogRecord class has a custom WriteObject method translation parameter is a string (using Object.toString ()) before writing them. See the LogRecord API specification.
Multi-class Logging classes do not try serialization. Loggers and Handlers are all-state-rich classes, which are tied to specify virtual machines. In this relationship, they are similar to Java.IO classes, and are not serialized.
*********************************************************** ****************************
Example 1. 1
Import java.util.logging.logger;
Import java.util.logging.Logging.Logging.LesL;
Public class simplelogger {
Public static void main (String [] args) {
Logger s = logger.getlogger ("s");
// Because the Logger class is used, you need to import java.util.logging.logger
S.Log (Level.info, "This Is A Info");
Because the Level class is used, you need to import java.util.logging.Logging.Logging.Logging.Level
}
}
//: ~ ~
D: / java / logging> Java Simplelogger
2005-3-13 11:09:10 Simplelogger main
Information: this is a info
*********************************************************** ****************************
Example 1. 2
Import java.util.logging.logger;
Import java.util.logging.Logging.Logging.LesL;
Public class simplelogger {
Public static void main (String [] args) {
Logger s = logger.getlogger ("s"); // Because the logger class is used, you need to import java.util.logging.logger
S.Log (Level.info, "This Is A Info");
// Because of the use of the Level class, you need to import java.util.logging.LOFEVEL
S.Setlevel (Level.warning);
Logger sb = logger.getlogger ("s.b");
Sb.log (Level.info, "this is a sb.info");
// Because s sets Level to Warning, SB inherits this property from its parent Logger S.
// So SB's Level is Warning, so INFO record will not be released
Sb.log (Level.Warning, "this is a sb.warning");
}
}
//: ~ ~
D: / java / logging> Java Simplelogger
2005-3-13 11:16:00 Simplelogger main
Information: this is a info
2005-3-13 11:16:00 Simplelogger main
Warning: this is a sb.warning
D: / java / logging>
*********************************************************** ****************************
Example: 1.3
Import java.util.logging.logger;
Import java.util.logging.Logging.Logging.LesL;
Import java.util.logging.filehandler;
Import java.io.ioException;
Public class simplelogger {
Public static void main (String [] args) {
Logger s = logger.getlogger ("s");
// Because the Logger class is used, you need to import java.util.logging.logger
S.Log (Level.info, "This Is A Info");
// Because of the use of the Level class, you need to import java.util.logging.LOFEVEL
S.Setlevel (Level.Warning);
Try {
FileHandler MyfileHandler = New FileHandler ("SimpleLogger.log");
// Because I use the FileHandler class, I need to import java.util.logging.fileHandler
//, because the above sentence creates IOException, the Exception is imported, and it processes it.
S.AddHandler (MyFileHandler);
/ *
Because in C: / Program Files / Java / JDK1.5.0 / JRE / LIB / Logging.properties
The default sets consolehandler, so don't add Handler to see the log at the console.
Now add myFileHandler, you will see the log in the file (default is XML format)
* /
} catch (ioexception e) {};
Logger sb = logger.getlogger ("s.b");
Sb.log (Level.info, "this is a sb.info");
// Because s sets Level to Warning, SB inherits this property from its parent Logger S.
// So SB's Level is Warning, so INFO record will not be released
Sb.log (Level.warning, "this is a sb.warning");
}
//: ~ ~
D: / java / logging> Java Simplelogger
2005-3-13 11:31:20 Simplelogger main
Information: this is a info
2005-3-13 11:31:20 Simplelogger main
Warning: this is a sb.warning
D: / java / logging>
XML Version = "1.0" encoding = "gbk" standalone = "no"?>
record>
log>
*********************************************************** ****************************
Example: 1.4
Import java.util.logging.logger;
Import java.util.logging.Logging.Logging.LesL;
Import java.util.logging.filter;
Import java.util.logging.logrecord;
Import java.util.logging.filehandler;
Import java.io.ioException;
Public class simplelogger {
Public static void main (String [] args) {
Logger s = logger.getlogger ("s");
// Because the Logger class is used, you need to import java.util.logging.logger
S.Log (Level.info, "This Is A Info");
// Because of the use of the Level class, you need to import java.util.logging.LOFEVEL
S.Setlevel (Level.Warning);
S.setfilter (new myfilter ());
Try {
FileHandler MyfileHandler = New FileHandler ("SimpleLogger.log");
// Because I use the FileHandler class, I need to import java.util.logging.fileHandler
//, because the above sentence creates IOException, the Exception is imported, and it processes it.
S.AddHandler (MyFileHandler);
/ *
Because in C: / Program Files / Java / JDK1.5.0 / JRE / LIB / Logging.properties
The default sets consolehandler, so don't add Handler to see the log at the console.
Now add myFileHandler, you will see the log in the file (default is XML format) * /
} catch (ioexception e) {};
S.Log (Level.warning, "this is a s.warning");
// Because MyFilter will filter out the log above, it will not be recorded (that is, not displayed)
Logger sb = logger.getlogger ("s.b");
Sb.log (Level.info, "this is a sb.info");
// Because s sets Level to Warning, SB inherits this property from its parent Logger S.
// So SB's Level is Warning, so INFO record will not be released
Sb.log (Level.Warning, "this is a sb.warning");
}
}
Class myfilter imports filter {
// Due to use Filter, you need to import java.util.logging.filter;
Public Boolean Isloggable (LogRecord Record) {
// Because use LogRecord, you need to import java.util.logging.logrecord
IF (Record.getLevel (). INTVALUE ()> java.util.logging.LAVEL.WARNING.INTVALUE ()) {
Return True;
}
Else
Return False;
}
}
//: ~ ~
D: / java / logging> Java Simplelogger
2005-3-13 11:50:41 Simplelogger Main
Information: this is a info
2005-3-13 11:50:41 Simplelogger Main
Warning: this is a sb.warning
D: / java / logging>
XML Version = "1.0" encoding = "gbk" standalone = "no"?>
record>
log>
*********************************************************** ********************
Example 1. 5
Import java.util.logging.logger;
Import java.util.logging.Logging.Logging.LesL;
Import java.util.logging.filter;
Import java.util.logging.logrecord;
Import java.util.logging.filehandler;
Import java.util.logging.simpleformatter; import java.io.ioException;
Public class simplelogger {
Public static void main (String [] args) {
Logger s = logger.getlogger ("s");
// Because the Logger class is used, you need to import java.util.logging.logger
S.Log (Level.info, "This Is A Info");
// Because of the use of the Level class, you need to import java.util.logging.LOFEVEL
S.Setlevel (Level.Warning);
S.setfilter (new myfilter ());
Try {
FileHandler MyfileHandler = New FileHandler ("SimpleLogger.log");
// Because I use the FileHandler class, I need to import java.util.logging.fileHandler
//, because the above sentence creates IOException, the Exception is imported, and it processes it.
MyfileHandler.SetFormatter (New SimpleFormatter ());
// Because SimpleFormatter is used, it is necessary to import java.util.logging.simpleformatter
// Because the formatter is SimpleFormatter, the content of the file will become simple format instead of XML format.
S.AddHandler (MyFileHandler);
/ *
Because in C: / Program Files / Java / JDK1.5.0 / JRE / LIB / Logging.properties
The default sets consolehandler, so don't add Handler to see the log at the console.
Now add myFileHandler, you will see the log in the file (default is XML format)
* /
} catch (ioexception e) {};
S.Log (Level.warning, "this is a s.warning");
// Because MyFilter will filter out the log above, it will not be recorded (that is, not displayed)
Logger sb = logger.getlogger ("s.b");
Sb.log (Level.info, "this is a sb.info");
// Because s sets Level to Warning, SB inherits this property from its parent Logger S.
// So SB's Level is Warning, so INFO record will not be released
Sb.log (Level.Warning, "this is a sb.warning");
}
}
Class myfilter imports filter {
// Due to use Filter, you need to import java.util.logging.filter;
Public Boolean Isloggable (LogRecord Record) {
// Because use LogRecord, you need to import java.util.logging.logrecord
IF (Record.getLevel (). INTVALUE ()> java.util.logging.LAVEL.WARNING.INTVALUE ()) {
Return True;
}
Else
Return False;
}
}
//: ~ ~
D: / java / logging> Java Simplelogger
2005-3-13 12:11:48 Simplelogger main
Information: this is a info
2005-3-13 12:11:49 Simplelogger main
Warning: this is a sb.warningd: / java / logging>
2005-3-13 12:11:49 Simplelogger main
Warning: this is a sb.warning
Example 1. 6
Use the following method
Java.util.logging.logger
VoidsetUseparentlers (Boolean UseParentlers) Specify WHETHER or Not this Logger Should de It's Parent Logger.
Finally, coupled with a good website found in 9cbs, English is good to see this may be better.
http://www.phptr.com/articles/article.asp?p=361635&seqnum=4&rl=1
There is also one you find, it's good.
http://javaalmanac.com/index.html