A method and system for automatically generating a log configuration file according to module division
By using log configuration analysis and the Dom4j parser to automatically generate log framework configuration files, the problem of cumbersome log configuration in multi-module systems is solved, achieving the effect of simplifying development and standardizing log recording.
Patent Information
- Application Number
- CN202111348759.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-15
- Publication Date
- 2025-11-11
- Estimated Expiration
- 2041-11-15
AI Technical Summary
Existing logging frameworks are cumbersome to configure manually in multi-module systems, resulting in a large development workload and non-standard log recording and management.
The log configuration analysis method is adopted, which examines the configuration file through the plugin verification method, parses the tag information of the configuration file, automatically generates the log framework configuration file, and uses the Dom4j parser to operate the node, combined with the log aspect layer to handle abnormal log records.
It simplifies log development, reduces development workload, standardizes log recording and management, and facilitates log analysis and retrieval.
Smart Images

Figure CN115220777B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to IT and software technology, and more specifically, to a method and system for automatically generating log configuration files based on module division. Background Technology
[0002] For developers, various logging frameworks are essential for recording program runtime information during application development. During development and debugging, logs help us locate problems more quickly and resolve them more effectively. In application usage and maintenance, system logs help record most exception information. Some enterprises also collect log information for real-time monitoring and early warning of system operation. Currently, mainstream logging frameworks include log4j, logback, and log4j2.
[0003] Log4j is an open-source project from Apache. Using Log4j, you can control the destination of log messages—the console, files, GUI components, or even API servers, NT event loggers, and UNIX Syslog daemons. You can also control the output format of each log message. By defining the level of each log message, you can have more granular control over the log generation process.
[0004] Logback is another open-source logging component designed by the creator of log4j. Logback currently consists of three modules: logback-core, logback-classic, and logback-access. logback-core is the foundation module for the other two. logback-classic is an improved version of log4j. Furthermore, logback-classic fully implements the SLF4J API, making it easy to switch to other logging systems, such as log4j or JDK 14 Logging. The logback-access module integrates with Servlet containers to provide access to logs via HTTP.
[0005] Log4j2 is an upgrade to Log4j. It offers significant improvements over its predecessor, Log4j 1.x, and incorporates many of the improvements available in Logback, while also fixing some inherent issues in the Logback architecture.
[0006] These three logging frameworks are the most commonly used in application development and already meet typical logging needs. If we need to segment and record logs by module, these three frameworks can also help us achieve this. Furthermore, their configuration is relatively simple; generally, their configuration files can be manually configured. However, if there are dozens or even hundreds of modules, and we need to record logs from all modules, perform log analysis based on module distinctions, and record different module logs to different log files, then we need to modify the configuration files and add corresponding code in the module-related sections of the code. In this case, manual configuration becomes tedious, and the configuration work becomes mechanical and repetitive. Summary of the Invention
[0007] The technical problem to be solved by the present invention is to provide a method and system for automatically generating log configuration files based on module division, which addresses the shortcomings of the existing technology, simplifies development, and reduces the amount of log development required.
[0008] The present invention discloses a method for automatically generating log configuration files based on module division. The method performs module analysis on the configuration file to be divided using log configuration analysis to obtain log modules; performs node operations on the log modules to obtain the tag information of the configuration file; and generates a log framework configuration file that can be automatically divided into corresponding types of log files based on the tag information.
[0009] To further improve the log configuration analysis method, it specifically includes:
[0010] Step 1: Check the configuration file using the plug-in verification method to obtain a configuration file that meets the requirements;
[0011] Step 2: Read the configuration file and determine whether the configuration file has a controller layer configured. If a controller layer is configured, proceed to Step 3. Otherwise, traverse the project class packages of the project modules, find the controller layer, and then proceed to Step 3.
[0012] Step 3: Determine if there is a sub-package in the controller layer; if there is a sub-package, store the sub-package as a logging module; otherwise, obtain the default logging module and store it as a sub-package of the configuration file.
[0013] Step 4: Read the content configuration of the log module, operate the nodes of the configuration file through the Dom4j parser to obtain the tag information of the configuration file, and generate the log framework configuration file based on the tag information.
[0014] Furthermore, the plug-in verification method specifically includes,
[0015] Check if the configuration file exists. If it does not exist, provide a prompt to recreate the configuration file. If it exists, check the log type of the configuration file. If the log type is not configured, provide a prompt to reconfigure the log type. If the log type is configured, the configuration file meets the requirements.
[0016] Furthermore, if the tag information in the configuration file does not exist, then the default tag information is obtained as the tag information of the configuration file.
[0017] Furthermore, the tag information includes tag attributes and tag content.
[0018] Furthermore, the log configuration analysis method is also used to obtain the exception log records of the controller layer, divide the obtained exception log records into modules, and record them in the corresponding configuration files.
[0019] Furthermore, the method for obtaining exception log records specifically includes:
[0020] The controller layer calls the business processing layer in the project module based on the interface request initiated by the front end of the project module; if the call is successful, the business processing layer returns the business data to the front end.
[0021] Otherwise, intercept the instruction of the interface request and initiate an exception failure aspect interception based on the intercepted interface request;
[0022] Based on the project module to which the interface request belongs, a logger object is created, and the intercepted information is recorded in the configuration file; at the same time, error information is returned to the front end.
[0023] A system for automatically generating log configuration files based on module division includes a project module; the project module introduces a log configuration plugin package, which uses the aforementioned method for generating configuration files to configure the log framework configuration file.
[0024] Furthermore, the log configuration plugin package includes a log aspect module, which is used to obtain exception log records of the controller layer in the project module, divide the obtained log records into modules, and record them in the corresponding configuration files.
[0025] Furthermore, the project module can be a Maven-based project or a Spring Boot microservice framework-based project.
[0026] Beneficial effects
[0027] The advantages of this invention are:
[0028] 1. By using a log configuration analysis method, the system automatically generates log framework configuration files, eliminating the need for developers to manually configure each module one by one. This simplifies development and reduces the amount of log development required. It also standardizes log management, facilitating log searching and analysis.
[0029] 2. By setting up the logging aspect layer, developers no longer need to handle exceptions in every method of the controller class, which can effectively reduce their workload. Attached Figure Description
[0030] Figure 1 This is a flowchart illustrating the automatic log framework configuration file generation process of the present invention.
[0031] Figure 2 This is a schematic diagram of the log interception calling process of the present invention. Detailed Implementation
[0032] The present invention will be further described below with reference to embodiments, but this does not constitute any limitation on the present invention. Any limited modifications made by any person within the scope of the claims of the present invention are still within the scope of the claims of the present invention.
[0033] See Figure 1 This invention provides a method for automatically generating log configuration files based on module division. It uses log configuration analysis to analyze the configuration files to be divided into modules to obtain log modules. Node operations are performed on the log modules to obtain tag information from the configuration files, and a log framework configuration file is generated based on this tag information, automatically assigning each module to a corresponding log file type. For situations with numerous modules, this invention automatically generates log framework configuration files based on module division using log configuration analysis, eliminating the need for developers to manually configure each module individually, simplifying development and reducing the amount of log development work. It also standardizes log management, with logs from different modules recorded in corresponding files. If a problem occurs in a particular module, the corresponding log framework configuration file can be found and opened for analysis, facilitating log record analysis.
[0034] The log configuration analysis method in this embodiment specifically includes,
[0035] Step 1: Check the configuration file using the plugin verification method to obtain a configuration file that meets the requirements.
[0036] The configuration file is an XML file. The log configuration analysis method primarily runs within project modules. These project modules can be either Maven-based or Spring Boot microservice framework-based projects. This example uses a Maven-based project. Furthermore, dependencies such as the Spring framework are introduced into the Maven-based project to enable it to run.
[0037] In addition, the plug-in verification method specifically includes...
[0038] The system checks if the configuration file exists. If it doesn't, it displays a message indicating that the configuration file doesn't exist and prompts the user to recreate it. If the configuration file exists, it checks the log type. In this embodiment, the configuration file type is primarily one of log4j, logback, or log4j2. If the log type is not configured, it displays a message indicating that the log type is not configured and prompts the user to reconfigure the log type. If the log type is configured, the configuration file check confirms it meets the requirements. Furthermore, "configured" also includes not specifying a configuration, i.e., the default configuration. Setting a default configuration allows users to directly assign a particular log type as the default when there are too many of them, effectively improving configuration efficiency.
[0039] Step 2: Read the configuration file and determine if a controller layer is configured. If a controller layer is configured, proceed to Step 3. Otherwise, traverse the project class packages of the project modules, find the controller layer, and then proceed to Step 3.
[0040] Step 3: Determine if there is a sub-package in the controller layer. If there is a sub-package, store it as a logging module; otherwise, retrieve the default logging module and store it as a sub-package of the configuration file.
[0041] Step 4: Read the content configuration of the logging module, manipulate the nodes of the configuration file through the Dom4j parser to obtain the tag information of the configuration file, and generate the logging framework configuration file based on the tag information.
[0042] If the tag information in the configuration file does not exist, the default tag information will be used as the tag information in the configuration file. The tag information includes tag attributes and tag content.
[0043] Since the generated configuration file is an XML file, an XML parser is needed to parse and generate the XML file. Comparing four commonly used XML parsers: DOM (JAXP Crimson parser), SAX (Simple API for XML), JDOM, and Dom4j, Dom4j performs superiorly, offering the best performance. It boasts excellent performance, powerful features, and extreme ease of use, and it is also open-source software. Dom4j also incorporates many features beyond basic XML document representation, including integrated XPath support, XML Schema support, and event-based processing for large or streaming documents. Therefore, Dom4j was chosen to parse and manipulate XML nodes to generate the configuration file.
[0044] See Figure 2 The log configuration analysis method in this embodiment is also used to obtain exception log records from the controller layer, divide the obtained exception log records into modules, and record them in the corresponding configuration files. By setting up the log aspect layer, developers no longer need to handle exceptions in every method of the controller class.
[0045] The specific method for obtaining exception log records is as follows:
[0046] The controller layer invokes the business logic layer within a project module based on the API request initiated by the frontend. If the call succeeds, the business logic layer returns the business data to the frontend. Otherwise, it intercepts the API request and initiates an exception failure aspect based on the intercepted request. A logger object is created according to the project module to which the API request belongs, and the intercepted information is logged to the configuration file; simultaneously, error information is returned to the frontend.
[0047] A system that automatically generates log configuration files based on module division includes a project module; the project module imports a log configuration plugin package, which uses a method to generate configuration files to configure the log framework configuration file. This simplifies development, reduces the amount of log development required, and also standardizes log management, facilitating log retrieval and analysis.
[0048] The log configuration plugin package includes a log aspect module, which is used to obtain exception log records from the controller layer of the project module, divide the obtained log records into modules, and record them in the corresponding configuration files.
[0049] The project module can be a Maven-based project or a Spring Boot microservice framework-based project.
[0050] The above description is only a preferred embodiment of the present invention. It should be noted that those skilled in the art can make several modifications and improvements without departing from the structure of the present invention, and these will not affect the effectiveness of the implementation of the present invention or the practicality of the patent.
Claims
1. A method for automatically generating log configuration files based on module division, characterized in that, Log configuration analysis is used to analyze the configuration files to be divided into modules to obtain log modules; node operations are performed on the log modules to obtain the tag information of the configuration files, and a log framework configuration file that can be automatically divided into the corresponding type of log file is generated based on the tag information. The log configuration analysis method specifically includes, Step 1: Check the configuration file using the plug-in verification method to obtain a configuration file that meets the requirements; Step 2: Read the configuration file and determine whether the configuration file has a controller layer configured. If a controller layer is configured, proceed to Step 3. Otherwise, traverse the project class packages of the project modules, find the controller layer, and then proceed to Step 3. Step 3: Determine if there is a sub-package in the controller layer; if there is a sub-package, store the sub-package as a logging module; otherwise, obtain the default logging module and store it as a sub-package of the configuration file. Step 4: Read the content configuration of the log module, operate the nodes of the configuration file through the Dom4j parser to obtain the tag information of the configuration file, and generate the log framework configuration file based on the tag information; The plug-in verification method specifically includes: Check if the configuration file exists. If it does not exist, provide a prompt to recreate the configuration file. If it exists, check the log type of the configuration file. If the log type is not configured, provide a prompt to reconfigure the log type. If the log type is configured, the configuration file meets the requirements.
2. The method for automatically generating log configuration files based on module division according to claim 1, characterized in that, If the tag information in the configuration file does not exist, then the default tag information is obtained as the tag information of the configuration file.
3. The method for automatically generating log configuration files based on module division according to claim 2, characterized in that, The tag information includes tag attributes and tag content.
4. A method for automatically generating log configuration files based on module division according to any one of claims 1-3, characterized in that, The log configuration analysis method is also used to obtain the exception log records of the controller layer, divide the obtained exception log records into modules, and record them in the corresponding configuration files.
5. The method for automatically generating log configuration files based on module division according to claim 4, characterized in that, The method for obtaining the exception log records of the controller layer is specifically as follows: The controller layer calls the business processing layer in the project module based on the interface request initiated by the front end of the project module; if the call is successful, the business processing layer returns the business data to the front end. Otherwise, intercept the instruction of the interface request and initiate an exception failure aspect interception based on the intercepted interface request; Based on the project module to which the interface request belongs, a logger object is created, and the intercepted information is recorded in the configuration file; at the same time, error information is returned to the front end.
6. A system for automatically generating log configuration files based on module division, characterized in that, It includes a project module; the project module includes a log configuration plugin package, which uses the method for generating log configuration files as described in any one of claims 1-5 to configure the log framework configuration file.
7. The system for automatically generating log configuration files based on module division according to claim 6, characterized in that, The log configuration plugin package includes a log aspect module, which is used to obtain exception log records of the controller layer in the project module, divide the obtained log records into modules, and record them in the corresponding log configuration files.
8. The system for automatically generating log configuration files based on module division according to claim 6, characterized in that, The project modules described herein are based on Maven projects or Spring Boot microservice frameworks.
Citation Information
Patent Citations
Log capturing and processing method for micro-service development framework
CN109962814A
Unified log assembly
CN110780857A