A method for dynamically setting log levels in a distributed system
By generating log call chains and dynamically setting log levels in a distributed system, the problem of accurately capturing key business data in existing technologies is solved, enabling rapid fault location and analysis.
Patent Information
- Application Number
- CN202311442884.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-01
- Publication Date
- 2026-01-30
- Estimated Expiration
- 2043-11-01
AI Technical Summary
In distributed systems, existing technologies struggle to accurately capture critical business data in production environments, making fault location and analysis difficult.
By creating database tables and a log management module in the distributed system, unique log call identifiers are generated, method execution call chains are recorded, and log levels are dynamically set through a registration mechanism to precisely configure the log output level of a specific method or method call chain.
It enables the rapid capture of critical business data in the production environment, accurately locates and analyzes problems, and improves the efficiency of fault location.
Smart Images

Figure CN117555859B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software system design technology, specifically a method for dynamically setting log levels in a distributed system. Background Technology
[0002] Logs play a crucial role in applications. They help developers quickly locate and resolve problems, and also help them understand the application's operational status and whether it's functioning correctly. By reviewing logs, developers can understand where problems occur and how to fix them. Furthermore, logs can be used to track application execution, monitor data changes, perform data statistics, and conduct performance analysis.
[0003] Current mainstream open-source logging frameworks, such as log4j and logback, provide log level configuration functionality. Developers specify the log output level and output logs during the development process. The program then determines whether the logs can ultimately be output to a file based on the configured log display level and the log level specified by the developer at runtime.
[0004] Open-source logging frameworks effectively control log output through a hierarchical approach. During development, developers typically output debug business data at the "info" level. In testing environments, configuring the log display level to "info" allows for monitoring program execution in a black-box environment. However, in production environments during deployment, to avoid performance overhead from excessive log output and to conserve disk space, the log display level is usually set to "warn," monitoring only exceptions during program execution. This results in the inability to capture the business data specified by developers if a failure occurs in the production environment, hindering rapid problem localization and analysis.
[0005] To address these issues, open-source logging frameworks typically support dynamically configurable log display levels. During program execution, the log display level can be adjusted to allow specific levels of business data to be output. However, the display level is controlled globally or within a specific class; that is, all logging methods in all classes or a specified class will be output. In distributed, multi-node environments and under high concurrency, this still cannot accurately capture business data and quickly locate and analyze problems. Summary of the Invention
[0006] The technical objective of this invention is to address the above-mentioned shortcomings by providing a method for dynamically setting log levels in a distributed system. This method can establish a call chain for outputting business method execution flow logs, enabling the capture of critical business data in a production environment and rapid problem location and analysis.
[0007] The technical solution adopted by this invention to solve its technical problem is:
[0008] A method for dynamically setting log levels in a distributed system, the implementation of which includes:
[0009] The software system creates database tables; the software system includes a log management module and a log service executor module.
[0010] The log service executor module is integrated with the business code to provide a unified log output method. The business code calls the log output method in the log service executor to output logs. The unified log output method generates a unique log call identifier number that is not repeated and is saved in a thread variable. This identifier is used to record and chain all log outputs involved in the thread execution process to generate a method execution call chain.
[0011] The unified log output method records the information of the method caller. The log service executor module reports the data recorded by the unified log output method to the log management module. The log management module receives the data reported by the log service executor module, filters and removes duplicates, and saves it to the database.
[0012] The log management module provides an interface that allows users to view the log output call chain and log output methods, and to set the log output level for the corresponding call chain or method. After adjusting the log level, the log management module notifies the log service executor module. The unified log output method of the log service executor module outputs logs according to the level set by the call chain or method.
[0013] A program's log is a file that records various information during the program's execution. The purpose of the log is to record the system's operation process and exception information, providing detailed information for quickly locating problems that occur during system operation and for debugging issues during the development process.
[0014] This method uses a registration mechanism to add an identifier to all methods that call unified log output. By identifying the method call chain through the identifier, the log output level of a specific method or method call chain can be precisely configured, thereby capturing key business data and facilitating quick problem location and analysis.
[0015] Preferably, the unified log output method records the caller information of the method, including the full package name, class name, and method name.
[0016] Preferably, the process of generating the log output method call chain is as follows:
[0017] When the unified log output method is called, check whether the thread variable already contains the log identifier sequence number;
[0018] If no identifier exists, generate an identifier and store it in a thread variable;
[0019] Subsequent calls to the unified logging output method by the current thread will use the same log identifier sequence number, and all calls to the logging method that require the same information will be automatically chained together to form a method execution call chain.
[0020] Preferably, the process of filtering and deduplicating logs in the log management module is as follows:
[0021] Extract the names of all methods in the same log call chain;
[0022] Check if a single method already exists in the database; if not, add a new record.
[0023] Check if the entire method chain already exists in the database; if not, add a new record.
[0024] Preferably, the unified log output method of the log service executor module outputs logs as follows:
[0025] When the unified log output method is invoked, obtain the caller information of the method;
[0026] Determine whether the method caller has its logging level dynamically set or belongs to a method chain with a dynamically set logging level;
[0027] If the call chain is satisfied, logs are output according to the level set for the call chain; otherwise, logs are output according to the level set for the individual method.
[0028] Preferably, the software system creates a database table to record information about the caller method and the method call chain information in the log output;
[0029] The caller method information includes: the package name, class name, and method name of the calling method;
[0030] Method call chain information includes: the call chain identifier and the order within the call chain;
[0031] The unified log output method of the log service module is registered each time it is called. The log management module writes the received package name, class name, method name and call chain identifier into the database table.
[0032] Preferably, the software system includes a log management module and a log service executor module. After the log service executor and the log management module are started, they respectively register with the registry center and become service nodes of the microservice.
[0033] Preferably, the log service executor module is integrated with the business code to provide a unified log output method. The unified log output method is encapsulated using the decorator design pattern. The method class holds a reference to the open-source logging framework object, provides a log output method with the same name, and enhances its functionality by adding method registration and call chain judgment.
[0034] The present invention also claims a device for dynamically setting log levels in a distributed system, comprising at least one memory and at least one processor;
[0035] The at least one memory is used to store a machine-readable program;
[0036] The at least one processor is used to call the machine-readable program to implement the above-described method for dynamically setting log levels in a distributed system.
[0037] The present invention also claims a computer-readable medium storing computer instructions that, when executed by a processor, cause the processor to perform the above-described method for dynamically setting log levels in a distributed system.
[0038] The method for dynamically setting log levels in a distributed system according to the present invention has the following advantages compared with the prior art:
[0039] This design scheme, based on a database and registry center, enables dynamic setting of log levels in a distributed system. It generates log call methods and call chains through a registration mechanism, precisely specifies the log output level of a certain registered method or call chain, captures business data, and quickly locates and analyzes problems. Attached Figure Description
[0040] Figure 1 This is a system architecture diagram illustrating the method for dynamically setting log levels in a distributed system provided in this embodiment of the invention. Detailed Implementation
[0041] The present invention will be further described below with reference to specific embodiments.
[0042] This invention provides a method for dynamically setting log levels in a distributed system. The implementation of this method includes:
[0043] 1. The software system creates database tables;
[0044] This table is mainly used to record information about the caller method and the method call chain information in the log output;
[0045] The caller method information includes: the package name, class name, and method name of the calling method;
[0046] Method call chain information includes: the call chain identifier and the order within the call chain;
[0047] The unified log output method of the log service module registers itself each time it is called. The log management module then writes the received package name, class name, method name, and call chain identifier into a database table. The specific contents of this table are shown in Table 1 below:
[0048] Table 1
[0049] field name Field type Field length illustrate id Int primary key Package_name String 45 Package Name Class_name String 45 Class Name Function_name String 45 Method Name Execute_id String 45 Call chain identifier Execute_index Int Call chain order
[0050] 2. The software system includes a log management module and a log service executor module;
[0051] System architecture such as Figure 1 As shown, after the log service executor and log management module start, they register with the registry center to become service nodes of the microservice.
[0052] 3. The log service executor module is integrated with the business code to provide a unified log output method. The unified log output method can be encapsulated using the decorator design pattern. The method class holds a reference to the open-source logging framework object, provides a log output method with the same name, and enhances its functionality by adding method registration and call chain judgment.
[0053] 4. The business code calls the log output method in the log service executor to output logs.
[0054] 5. The unified logging output method generates a unique log call identifier number that is not repeated and is stored in a thread variable. This identifier is used to record and chain all log outputs involved in the thread execution process, generating a method execution call chain.
[0055] 5.1 When the unified log output method is called, check whether the thread variable already contains the log identifier sequence number;
[0056] 5.2 If the identifier does not exist, generate an identifier and store it in a thread variable;
[0057] 5.3 Subsequent calls to the unified log output method by the current thread will use the same log identifier sequence number. All calls to the same logging method will be automatically chained together to form a method execution call chain.
[0058] 6. The unified log output method records information about the method caller, including the fully qualified package name, class name, and method name.
[0059] 7. The log service executor module will report the data information recorded by the unified log output method to the log management module.
[0060] 8. The log management module receives data reported by the log service executor module, filters and deduplicates it before saving it to the database; the filtering and deduplication process of the log management module is as follows:
[0061] 8.1 Extract the names of all methods in the same log call chain;
[0062] 8.2. Check if a single method already exists in the database; if not, add a new record.
[0063] 8.3. Check if the entire method chain already exists in the database. If it does not exist, add a new record.
[0064] 9. The log management module provides an operation interface that supports viewing the log output call chain and log output methods, and supports setting the log output level for the corresponding call chain or call method;
[0065] After the log management module adjusts the log level, it notifies the log service executor module.
[0066] 10. The unified log output method of the log service executor module outputs logs based on the call chain or the level set by the calling method. The specific implementation is as follows:
[0067] When the unified log output method is invoked, obtain the caller information of the method;
[0068] Determine whether the method caller has its logging level dynamically set or belongs to a method chain with a dynamically set logging level;
[0069] If the call chain is satisfied, logs are output according to the level set for the call chain; otherwise, logs are output according to the level set for the individual method.
[0070] This invention also provides an apparatus for dynamically setting log levels in a distributed system, comprising at least one memory and at least one processor;
[0071] The at least one memory is used to store a machine-readable program;
[0072] The at least one processor is used to call the machine-readable program to implement the method for dynamically setting the log level in a distributed system as described in the above embodiments.
[0073] This invention also provides a computer-readable medium storing computer instructions. When executed by a processor, the computer instructions cause the processor to perform the method for dynamically setting log levels in a distributed system as described in the above embodiments. Specifically, a system or apparatus equipped with a storage medium storing software program code that implements the functions of any of the above embodiments can be provided, and the computer (or CPU or MPU) of the system or apparatus can read and execute the program code stored in the storage medium.
[0074] In this case, the program code read from the storage medium can itself implement the function of any of the above embodiments, and therefore the program code and the storage medium storing the program code constitute part of the present invention.
[0075] Examples of storage media used to provide program code include floppy disks, hard disks, magneto-optical disks, optical disks (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, DVD-RW, DVD+RW), magnetic tapes, non-volatile memory cards, and ROMs. Alternatively, program code can be downloaded from a server computer via a communication network.
[0076] Furthermore, it should be clear that not only can the program code read by the computer be executed, but also the operating system or other components operating on the computer can be instructed based on the program code to perform some or all of the actual operations, thereby realizing the function of any of the embodiments described above.
[0077] Furthermore, it is understood that the program code read from the storage medium is written to the memory set in the expansion board inserted into the computer or to the memory set in the expansion unit connected to the computer. Then, based on the instructions of the program code, the CPU or other components installed on the expansion board or expansion unit execute some and all of the actual operations, thereby realizing the function of any of the embodiments described above.
[0078] The present invention has been shown and described in detail above with reference to the accompanying drawings and preferred embodiments. However, the present invention is not limited to these disclosed embodiments. Based on the above embodiments, those skilled in the art will know that more embodiments of the present invention can be obtained by combining the code review methods in the different embodiments. These embodiments are also within the protection scope of the present invention.
Claims
1. A method for dynamically setting log levels in a distributed system, characterized in that, The method comprises the following steps: The software system creates a database table; the software system comprises a log management module and a log service executor module; The log service executor module is integrated with business code to provide a unified log output method, and the business code calls the log output method in the log service executor to output logs; the unified log output method generates a unique log call identification serial number that is saved in a thread variable to record and concatenate all log outputs involved in the thread execution process to generate a method execution call chain; The unified log output method records method caller information, and the log service executor module reports the data information recorded by the unified log output method to the log management module; the log management module receives the data reported by the log service executor module, filters and saves the data to the database after deduplication; The log management module provides an operation interface to support viewing log output call chains and log output methods, and supports setting log output levels for corresponding call chains or call methods; after the log management module adjusts the log level, it notifies the log service executor module; the unified log output method of the log service executor module outputs logs according to the level set by the call chain or the call method; The generation process of the log output method execution call chain is as follows: When the unified log output method is called, check whether the log identification serial number exists in the thread variable; If the identification serial number does not exist, generate the identification serial number and put it into the thread variable; Subsequent calls to the unified log output method by the current thread will use the same log identification serial number, and all required call log methods will be automatically concatenated into a method execution call chain.
2. The method for dynamically setting log levels of a distributed system according to claim 1, wherein, The unified log output method records method caller information, including full package name + class name + method name information.
3. The method for dynamically setting log levels of a distributed system according to claim 1, wherein, The log management module filters and deduplicates as follows: Extract the names of all methods in the same log call method chain; Query whether a single method exists in the database, and if not, add a new record; Query whether the entire method chain exists in the database, and if not, add a new record.
4. The method for dynamically setting log levels of a distributed system according to claim 3, wherein, The process of the unified log output method of the log service executor module outputting logs is as follows: The unified log output method is called, and the caller information of the method is obtained; Determine whether the method caller is dynamically set with a log level or belongs to a method chain with a dynamically set log level; If the call chain is met, the log is output according to the level set by the call chain; if the call chain is not met, the log is output according to the level set by the corresponding method.
5. The method for dynamically setting log levels of a distributed system according to claim 1, wherein, The software system creates a database table to record the information of the caller method of the log output and the information of the method call chain; The caller method information includes: call method package name, class name, and method name; The method call chain information includes: call chain identification and order in the call chain; The unified log output method of the log service module is registered every time it is called, and the log management module writes the received package name, class name, method name, and call chain identification information into the database table.
6. The method for dynamically setting log levels of a distributed system according to claim 1, wherein, The software system comprises a log management module and a log service executor module, and the log service executor and the log management module are registered with the registration center after being started to become service nodes of microservices.
7. The method for dynamically setting log level of a distributed system according to claim 1 or 5 or 6, characterized in that, The log service executor module is integrated with business code to provide a unified log output method, a decorator design pattern is used to encapsulate the unified log output method, a method class holds an open source log framework object reference, provides a log output method with the same name, and enhances the function, and method registration and call chain judgment are added.
8. A device for dynamically setting log levels in a distributed system, characterized in that, comprising at least one memory and at least one processor; The at least one memory is used to store machine readable programs. The at least one processor is used to call the machine readable programs to realize the method in any one of claims 1 to 7.
9. A computer readable medium characterized by The computer readable medium stores computer instructions, and the computer instructions make the processor execute the method in any one of claims 1 to 7 when executed by the processor.
Citation Information
Patent Citations
Distributed big data log link tracking method and system under micro-service architecture
CN112612675A
Modular log management method and device based on Spring log framework and medium
CN115729722A