Say goodbye to system.out.print () - J2SDK1.4 Add Java Log Framework (1)

zhaozj2021-02-16  43

introduction

As a Java programmer, most familiar, I am afraid that I am too system.out.print ("..."). When you don't have a debug tool to track a variable is worthwhile; when you need to display the captured Exception, Error's time; when you want to know what happens when you run, the usual approach is to call System.out .print prints them on the terminal and console. This approach has many inconveniences to the classification, formatting, and permanent preservation of output information. Although we can write it into a file and analyze it, this must be written in additional program code, and the cost cannot be ignored! The resulting complexity to the target system itself is inevitable to develop, debugging into a deep chamfer.

The launch of JDK1.4 makes all this is about to become history. Let us say goodbye to System.out.Print (), build a complete logging system for your own programs to use the Java Logging API!

First, the first example

First look at a simple example: simpleloggingtest.java

1 Import java.util.logging. *;

2 public class simpleloggingtest {

3 public static void main (String args []) {

4 // Other processing of programs 5 // Get an anonymous // Logger using Logger's static method

6 logger logger1 = logger.getanonymouslogger ();

7 // Record message 8 logger1.log (Level.info, "First Log Record"); 9 // Other Processing

10} 11}

Example 1 Note: Compile, execute the program requires support for JDK1.4 and above.

Run the program, you can see the program running results at the console:

2003-1-14 15:09:40 SimpleLoggingTest Main Information: First Log Record

First, the program references the java.util.logging package (line 1). Next, an instance of a Logger class is obtained when appropriate, and an anonymous Logger is obtained. Finally, the log method of the Logger class is called in the program that needs to record information (8th line, record an INFO level message). Second, Java Logging API

The Java Logging API is encapsulated in the java.util.logging package of JDK1.4.0. It gives a convenient means of providing convenience for the development of end users, system administrators, fault maintenance engineers, and software development team (engineers). It captures data information such as operating system platforms and executables, configuring errors, executing bottlenecks, and / or BUG, ​​formatted it into log records in some way of plain text, XML, or programmers, and then passes to memory , System output flow, console, file, sockets and other system resources for cache and output.

(1) The key classes in this package.

n logger: The application performs the main entities of logging calls. Logger objects are used to record messages for a particular system or application.

n logRecord: It is used to pass a record request between the log frame and a single record handler. N Handler: The final output processor for log data. It exports the LogRecord object to a variety of targets, including memory, output streams, console, files, and sockets. A variety of Handler subclasses are available for this purpose.

Figure 1 n level: Defines a set of standard recording levels and can be used to control the output of the record. Programs can be configured to only output some levels of records, and ignore other levels of outputs. n filter: Fine filtering, control the contents of records, more accurate than the control provided by the recording level. Recording the API supports the universal filter mechanism, which allows the application code to add any filter to control the output of the record. n formatter: Support for the formatting of LogRecord objects. Figure 2 N LogManager: The only, global object in the Java Logging framework, is used to maintain a series of shared data structures and status of the Logger Recorder and Log Services. It is responsible for the initialization of the entire log frame, maintains a global handle object, maintains a tree structure's logger's namespace, diagnosis log frame configuration file change to re-read and apply the relevant parameters and responsible for the program to stop running Cleaning up the entire log frame.

(2) Logger

1, logger's namespace

In the SimpleLoggingTest.java instance, we used an anonymous (no named) Logger object. In the Java Logging framework, logger is named. Logger's namespace is identical to the same structure as the namespace of the Java class: use "." Interval string. The namespace of Logger reflects the hierarchy of Logger. For example: Logger named "a.b" is "Father" (previous level) Logger recorder named "a.b.c". Logger's naming can be any string, in general, using the name of the package or class to naming Logger.

Logger's namespace is created and maintained by instances of global single-column LogManager.

Anonymous Logger is not stored in the namespace. 2. Create a logger instance

The logger object can be obtained by calling the factory method getLogger or GetAnonymousLogger.

/ / Get a logger object named "a" logger loggera = logger.getlogger ("a"); // Gets a Logger object called "A.B", the upper-level recorder is Loggera. Logger loggerab = logger.getlogger ("a.b"); // Get an anonymous logger object logger loggertmp = logger.getanonymousLogger ();

For non-anonymous logger, getLogger first looks for the same name's logger object, if there is, return to the logger object; if there is no existence, create a new Logger object in the namespace and associated with its upper Logger object. . Anonymous Logger object belongs to the private object of creating its object, can only be used by the object created, record some temporary log information. The named the Logger object enables global, in addition to the log frame, in addition to the object to create its object, can be used by other objects to record log information. Anonymous logger object is empty by a global root logger "object (empty root logger). This means that all anonymous Logger objects will inherit behavior from root logger. Anonymous Logger objects are usually used in the Java Applet application. It removes the safety inspection of the fault in the running process, allowing it to create a class object to modify the logger's control, status information, such as: SETLEVEL Settings Logger log message recording level; addHandle Add Logger's Handle (Processor) Object, etc. A Logger object can have zero to multiple Handler instances. When there is no Handler, if the logging is not prohibited from passing up in the namespace, the log message record of the Logger object will have its superior logger with the Handler instance. When a Logger object has multiple Handler instance objects, the log data thereof will be processed one by one by all Handler. (3), the log message received by the HandlerHandler object will output it. Handler can output log messages to a variety of target resources, such as: Output to the console for display, write log files, transfer to any physical resources such as remote log service on the network.

转载请注明原文地址:https://www.9cbs.com/read-26925.html

New Post(0)