Processing logging in Java

zhaozj2021-02-16  82

A log record package is provided in JDK1.4: java.util.logging, which can perform fairly complex logging in the program. For example, by specifying the level and log of the log (console, file, socket, memory buffer), you can also create subcunauts, which can specify the content you want to record by using the program control. You can also use the configuration file to specify without the need to go to the program. Compared with log4j, it is more simple and lighter, and it is undoubtedly a better choice when the requirements for log output are not very complicated.

First we get an example of a LogManager class:

Logmanager lmgr = logmanager.getlogManager ();

Then we create a logger and add it to the current manager:

String thisname = "logpkg";

Logger log = logger.getlogger (thisname);

LMGR.Addlogger (log);

If we don't specify the location where the log file is stored, follow the contents specified in the logging.properties file in the jre / lib directory, default is consolehandler means that the log information is displayed in the console.

In the program we can publish log information by level, there are 7 levels: Server, Warning, Info, Config, Fine, Finer, Finest, and OFF (not record).

Log.Server ("error");

We can set the recording level of the recorder to ignore messages below Warning levels, only those recorded by Server and Warning can be output.

Log.Setlevel (Level.warning);

Log.info ("this message is info"); // This information will be ignored and will not be output

Log.Warning ("this message is warning"); // This information will output

The name of the logger can be a simple text, or a comma-separated composite name, a comma-left name, but a recorder with an additional name on the right side of the comma is a subcorder of the prototype recorder, such as a logger called LogPKG and one Call logpkg.test1 recorder. The recorder and its sub-recorder can be different levels, only Server information can be logged throughout the program, and other more events can be recorded for a suspicious part.

If you want to output the log to the file and console, you can create a FileHandler and add it to the logger.

FH = New FileHandler ("runtime.log");

Log.addhandler (fH);

If there is no special designation, the file format defaults to XML.

Control logging by profile:

FileInputStream Fi = New FileInputStream (New File (Logging.properties));

LMGR.Readconfiguration (Fi);

The advantage of this is that there is no need to change or recompile the program, you can change the status of the log record.

The format of the configuration file:

Handlers = java.util.logging.fileHandler, java.util.logging.consolehandler

.Level = info

Java.util.logging.consolehandler.pattern = runtime.log

Java.util.logging.consolehandler.limit = 50000

Java.util.logging.consolehandler.count = 2

Java.util.logging.consolehandler.formatter = java.util.logging.xmlFormatter

Java.util.logging.consolehandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Logging.Consolehandler.Logging.ConsoleHandler.LesLevel = Warning

Java.util.logging.consolehandler.formatter = java.util.logging.simpleformatter

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

New Post(0)