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 customize the log Handler of the JDK logging framework

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how the JDK log framework to customize the log Handler, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Custom log Handler

All Handler classes inherit from the java.util.logging.Handler abstract class, which has the structure diagram shown in the figure.

Figure-Handler Class Diagram

As can be seen from the class diagram, the Handler abstract class provides abstract interfaces: publish, flush and close. These interfaces provide the basic functions of log output. At the same time, the Handler class holds Formatter,Filter and Level objects to control the log output. Therefore, writing a custom Handler class requires the following steps:

1. Inherit the Handler abstract class

2. Implement publish,flush and close methods. Where the publish method is used to publish a log record. The flush method is to clear the memory buffer. The close method releases the resources requested by the Handler class (such as files, socket, etc.) when the application is closed.

3. Set the default Formatter,Filter and Level objects. If necessary, you can read the configuration file to set these parameters when the class is initialized.

A typical custom Handler class implementation is shown in listing 1.

Listing 1 Custom Handler class

Public class MyHandler extends Handler {private bOOlean doneHeader = false; public MyHandler () {setLevel (Level.INFO); setFilter (null); setFormatter (new SimpleFormatter ());} _ cnnew1@Override public void close () throws SecurityException {if (! doneHeader) {output (getFormatter (). GetHead (this)); doneHeader = true;} output (getFormatter (). GetTail (this)); flush () } @ Override public void flush () {/ / clear buffer} @ Override public void publish (LogRecord record) {if (! isLoggable (record)) {return;} String msg = getFormatter () .format (record); try {if (! doneHeader) {output (getFormatter (). GetHead (this)); doneHeader = true;} output (msg) } catch (Exception ex) {reportError (null, ex, ErrorManager.WRITE_FAILURE);}} private void output (String message) {/ / implement log output}}

Here, the reportError method outputs the error information in the log class to the outside world, which is implemented by the ErrorManager class. The ErrorManager class is responsible for recording the error of Handler in the logging framework, which is usually printed to the console. Each specific log message is encapsulated into a LogRecord object by the JDK logging framework, which is partially defined as shown in listing 2.

Listing 2 LogRecord class definition

Public class LogRecord implements java.io.Serializable {public String getLoggerName (); public void setLoggerName (String name); public ResourceBundle getResourceBundle (); public void setResourceBundle (ResourceBundle bundle); public Level getLevel (); public void setLevel (Level level); public String getMessage (); public void setMessage (String message); public Object [] getParameters (); public void setParameters (Object parameters [); public int getThreadID (); public void setThreadID (int threadID); public long getMillis (); public void setMillis (long millis) Public Throwable getThrown (); public void setThrown (Throwable thrown);...}

As you can see in listing 2, the LogRecord class contains all the information about the level of a log message, message text, time, parameters, threads, and so on, which are handled by objects such as Handler,Formatter and Filter. At the same time, the class is serializable and can be serialized to the network and files. This class can also be bound to a ResourceBundle object to localize the message string.

The above is all the content of the article "how to customize the log handler of the JDK log framework". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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