Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize JDK Log Framework and its main functions

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to achieve the JDK log framework and its main functions analysis, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

The following describes the logging framework of JDK, and how to customize logging processing, message formatting, message level and other components according to different requirements. Finally, it describes how to use the extensibility of the logging framework to monitor Java programs through STAF (Software Testing Automation Framework, an automated testing framework) logging service.

Since Java 1.4, JDK has included a new logging framework package, java.util.logging, which is well designed, tightly integrated with JDK, flexible to control, and easy to use. Logging is of great significance for software maintenance, especially for software debugging after it has been deployed to the running environment. In actual projects, more complex logging capabilities are often required than those provided by the framework. For this requirement, the JDK logging framework is scalable enough to customize log processing, message formatting, log message level and other components of different requirements. In the following content, this article describes how to extend the JDK logging framework to customize how logging is handled. And take a practical example to introduce how to combine the JDK logging framework and STAF (Software Testing Automation Framework, an automated testing framework) logging service to monitor java programs.

Introduction to JDK Log Framework

JDK's logging framework is the java.util.logging package. For a software log system, there must first be a log object, which is responsible for recording log information. At the same time, this information can be output to different locations, such as the console, files and even the network. The format of the information can be output to plain text, XML or Html format according to different requirements. At the same time, log information needs to be classified at different levels, which has the advantage of filtering redundant information and keeping only critical logs. For a log framework, the log object must be configurable, it can output to the specified target according to the configuration, and determine the format of the output and the level above which log can be output. Configurations can also take a variety of forms, either in the form of code or in the form of configuration files. Especially in the form of configuration files, for a software that has been deployed to the running environment, it is very convenient to change the log configuration without changing its source code.

The JDK logging framework provides all of the above functionality. It mainly includes the following parts:

◆ Logger: logging object. Used to record log information.

◆ Handler: used to process the output of log information. In the Handler class, you can decide whether the log is output to a file or to the console.

◆ Filter: used to filter logs. In the Filter class, you can decide whether to output the log based on the log level or some condition. In this way, the redundant information can be removed.

◆ Formatter: used to format log information. This class can format log text into XML or Html format, which depends entirely on the specific implementation.

◆ Level: used to represent the level of the log. The JDK logging framework has the following levels by default: SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST.

As far as the program is concerned, its Logger object will first determine whether the level of the log meets the requirements of the output level, and then give the log messages that meet the level requirements to the configured Handler object for processing. If the log object is configured with a Filter object, then the Filter object will filter the log information. After receiving the log message, the Handler object changes the format of the log according to the configured format class Formatter, filters the log information again according to the configured Filter object and Level object, and * * outputs it to the output location specified by the Handler object, which can be console, file, network socket or even memory buffer.

JDK provides several Handler classes that are supported by default:

◆ ConsoleHandler: output logs to the console

◆ FileHandler: output logs to a specified file

◆ MemoryHandler: output the log to the memory buffer, and then output the log in the buffer when certain conditions are met (such as the log information of a keyword)

◆ SocketHandler: output logs to network socket

◆ StreamHandler: output logs to input and output stream objects

After reading the above, have you mastered how to implement the JDK logging framework and its main functions? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report