Implementation of Java log caching mechanism — reprint

< H2 id = "major1" > overview

Log technology provides important support for product quality and service. JDK adds logging mechanism after version 1.4, which provides convenience for Java developers. However, this logging mechanism is based on the static log level, that is, the log level to be printed needs to be set before the program runs, which will bring some inconvenience.

In the log function provided by JDK, the log level is subdivided into 9 levels to distinguish the purposes of different logs. It is used to record an error, record normal operation information, or record detailed debugging information. Because the log level is static, if the log level is set too high, it is difficult to print the low-level log, which makes it difficult to track the cause of the error when the error occurs. At present, the common method is to adjust the log level to a relatively low level when the error occurs, and then trigger the error, Make the root cause of the problem appear. However, this way of changing the product configuration and then re triggering the problem for debugging makes the product user experience worse, and some problems are difficult to re trigger due to accidental and complex environment.

On the contrary, if the log level is adjusted to a low level at the beginning, there will be a lot of useless information in the log. Moreover, when the product is more complex, the generated log files will be large and refresh quickly, unable to record effective information in time, and even become a performance bottleneck, thus reducing the help of the log function to the product.

This article uses the memoryhandler class in Java logging to cache all levels of logs and output them at an appropriate time to solve this problem. It mainly focuses on the definition and logging of memoryhandler Properties file.

The scenario attached to the example is as follows. It is envisaged that when a serious error occurs in the product, the user needs to view the previous error information containing exception as the basis for diagnosing the cause of the problem. A solution made by using the Java buffer mechanism is to save the log entries containing exceptions generated during the operation of all products in a circular buffer queue of settable size. When a severe error occurs, the logs in the buffer queue will be output to the specified platform for users to consult.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>