A method and device for realizing log output oriented to business logic
By defining error codes and log priority, centrally outputting the error logs of the software system, the redundancy and maintenance difficulties caused by dispersed logs are solved, and error checking efficiency and system performance are improved.
Patent Information
- Application Number
- CN202010554138.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-17
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2040-06-17
AI Technical Summary
In the development of software systems, error logs are scattered and difficult to maintain, resulting in duplicate redundancy problems, affecting performance and error checking efficiency.
By defining the error code of the processing business logic module and adding log content according to the functions of the submodule, the main module outputs the log content of the submodule according to the predefined log priority, and centrally outputs the error log to avoid dispersed output.
It realizes logical centralized output of error logs, reduces redundant information, improves the log's easy maintenance and error checking efficiency, and avoids performance impact.
Smart Images

Figure CN111897787B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of log processing, and in particular to a method for implementing log output oriented to business logic, and also to a device for implementing log output oriented to business logic. Background Art
[0002] Error management is inseparable from error reporting so that we can find solutions to the errors. Error information is mostly output in the form of logs. When the information provided by the logs is not enough to locate the problem, it is necessary to reproduce the error. If the error is easy to retry, it can basically be solved quickly. However, there are also cases where errors are difficult to find because they are not easy to pay attention to.
[0003] In the development of software systems, developers need to solve problems as soon as possible. During the development process, they need to pay attention to what necessary log information should be output. Since the output of a large number of logs will inevitably affect the performance of the program, it is necessary to think about how to effectively organize error logs to facilitate error checking as much as possible without affecting the performance of the program. Summary of the invention
[0004] The present application provides a method for implementing log output oriented to business logic, which solves the problem that error logs are scattered and difficult to maintain, and the output log information is prone to duplication and causes a large amount of redundancy.
[0005] The present application provides a method for implementing log output oriented to business logic, including:
[0006] Define error codes for processing business logic modules;
[0007] According to the functions of the submodules for processing the business logic, the log content containing the error code is added to the submodules, and a log is added to the submodules;
[0008] When the main module for processing business logic is called, the main module outputs the log content corresponding to each submodule according to the predefined log priority level.
[0009] Preferably, the error code includes:
[0010] The name and error type of each sub-module of the processing business logic module.
[0011] Preferably, according to the functions of each submodule of the processing business logic, the log content containing the error code is added to each submodule, and logs are added to each submodule, including:
[0012] According to the functions of each submodule of the processing business logic, obtain the error code corresponding to each submodule;
[0013] The log content including the error code is added to the corresponding sub-modules, thereby adding logs to the sub-modules.
[0014] Preferably, predefined log priorities include:
[0015] fatal, error, and debug.
[0016] Preferably, when the main module for processing business logic is called, the main module outputs the log content corresponding to each submodule according to the predefined log priority level, including:
[0017] When the main module for processing business logic is called, the main module calls the submodules contained in the main module according to the business logic;
[0018] When the submodule is executed, the main module that processes the business logic outputs the log content corresponding to the called submodule according to the predefined log priority.
[0019] Preferably, the log content corresponding to the called submodule is output, including:
[0020] Output the error code corresponding to the called submodule, the priority of the log, and the time and location of the log generation.
[0021] The present application also provides a device for implementing log output for business logic, including:
[0022] An error code definition unit is used to define error codes for processing business logic modules;
[0023] A log adding unit, used to add the log content containing the error code to each sub-module according to the function of each sub-module for processing the business logic, and add a log for each sub-module;
[0024] The log content output unit is used to output the log content corresponding to each submodule according to the predefined log priority level when the main module for processing the business logic is called.
[0025] Preferably, the log adding unit includes:
[0026] An error code acquisition subunit, used to acquire the error code corresponding to each submodule according to the function of each submodule of the processing business logic;
[0027] The log adding subunit is used to add the error code to each corresponding submodule, thereby adding a log to each submodule.
[0028] Preferably, the log content output unit includes:
[0029] The module calling sub-unit is used for, when calling the main module for processing the business logic, the main module calls the sub-module included in the main module according to the business logic;
[0030] The log content output subunit is used for outputting the log content corresponding to the called submodule according to the predefined log priority when the submodule is executed.
[0031] The present application provides a method for implementing log output oriented to business logic, which can enable error log information to be output in a logic-oriented centralized manner, thereby solving the problems of error logs being scattered and difficult to maintain, and the output log information being easily repeated and causing a large amount of redundancy. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 This is a flow chart of a method for implementing log output oriented to business logic provided by this application;
[0033] Figure 2 This is a schematic diagram of a device for implementing log output for business logic provided by the present application. DETAILED DESCRIPTION
[0034] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0035] Figure 1 This is a flow chart of a method for implementing log output for business logic provided by this application. Figure 1 The method provided in this application is described in detail.
[0036] Step S101, define the error code for processing the business logic module.
[0037] In the design of software systems, the system will be debugged according to the logs output by the system, and the problems in the system will be located according to the logs output by the system, so as to solve the problems. Therefore, the error code of the log is generally pre-defined, and the error code can tell us what kind of error occurred and in which module. Therefore, the error code at least includes the name and error type of each sub-module of the processing business logic module.
[0038] Step S102, according to the functions of each sub-module of the processing business logic, adding the log content containing the error code to each sub-module, and adding a log for each sub-module.
[0039] Each submodule of the business logic processing is used to complete different functions. Each submodule has a corresponding error code. Therefore, according to the functions of each submodule of the business logic processing, the error code corresponding to each submodule is obtained; then, the log content containing the error code is added to each corresponding submodule, so as to add a log to each submodule. The content of the log includes not only the error code, but also the priority of the log, the time and location of the log generation, and the code line that generates the log.
[0040] Step S103, when the main module for processing business logic is called, the main module outputs the log content corresponding to each submodule according to the predefined log priority level.
[0041] When the main module that processes the business logic is called, the main module calls the submodule contained in the main module according to the business logic; when calling the submodule, the submodule itself will not output the log content, because the output of the log content is placed in the main module. Therefore, when the submodule is called, although the log is generated, the output action is completed by the main module. Therefore, when the submodule is executed, the main module that processes the business logic outputs the log content corresponding to the called submodule according to the pre-defined log priority. The pre-defined log priorities include: fatal, error and debug, with severity from high to low. However, it is not limited to this, and log priorities of different severity can be used according to actual applications.
[0042] Programs with logging functions. Usually, the registers that are allowed to be output can be controlled in some form. For example, if only the error level is allowed to be output, then logs below the debug level cannot be output. Obviously, the lower the level of the output log, the more detailed the information it outputs. Most software does not output debug-level information during normal operation. This is a choice made considering system performance.
[0043] Using the method provided by this application, the specific embodiments are as follows:
[0044] The traditional log output method is process-oriented. To illustrate, we need to use a simplified invoicing program fragment of the tax control system, as shown below.
[0045]
[0046]
[0047]
[0048] Thread_call_process() is an entry function for receiving invoice issuance and is responsible for processing the received invoice flow message.
[0049] This program snippet uses three levels of logs: fatal, error, and debug, from high to low severity. Programs with logging capabilities usually have some form of control over the registers that are allowed to be output. For example, if only the error level is allowed to be output, then logs below the debug level cannot be output. Obviously, the lower the level of the output log, the more detailed the information it outputs. Most software does not output debug-level information during normal operation, which is a choice made considering system performance.
[0050] For the above program, when an error occurs in invoice_number_analyze(), head_alloc() or det_alloc(), an error log will be output. This simplified way of outputting logs in the program is called process-oriented flat output by the author. This method has the following characteristics:
[0051] The output actions of the log are scattered in each function, and the obtained log is very similar to a "running account", which makes it difficult to sort out the overall logic when checking errors. Since the error logs are scattered, it is not easy to maintain when logs need to be added or reduced. The output log information is prone to duplication and causes a lot of redundancy. This form of error handling can reflect the running path of the program through the log, which is helpful for analyzing problems.
[0052] The error log output in this way is more limited because each function can only see the limited information inside the function itself. In order to organize the error information more effectively, we need to explore other methods. In order to solve the problems of the process-oriented method, we can consider putting the action of outputting the log when an error occurs at a higher level of processing logic. For the above program, the higher level of processing logic is in the invoice_make_process() function, because no matter how many functions are called later, the message is received and processed in this function. In addition, all functions called by invoice_make_process() are processed based on the invoice_t structure. If the error processing is placed in the invoice_make_process() function, the revised code is as follows.
[0053]
[0054]
[0055]
[0056] A new method of implementing log output is designed, in which all error log output is placed in the invoice_make_process() function, while other called functions do not contain any log output behavior. In addition, errstr(error) prints out the specific error code name, which can tell us what kind of error occurred and in which module it occurred.
[0057] The reason for adopting this error handling method is that no matter what type of error it is, it is related to the relevant business logic. In other words, the error information is centrally output for the business logic, and the output log is no longer a small fragment.
[0058] In this sample program, only information related to the invoice_t data structure in the business logic is output. In actual projects, other information can be output according to the situation for error checking. For example, if there is other information in the msg_t data structure, and it is helpful to understand this information when an error occurs, you can also choose to output it when an error occurs. Since the log output when an error occurs is centrally managed, it will be easy to add and delete, and it will be less likely to have redundant logs. It will be easier for subsequent maintenance engineers to more comprehensively consider which logs need to be output for error checking.
[0059] In addition, in this way, if the calling path of the program is obtained, a mechanism needs to be designed or a variable needs to be introduced to record the running path of the program, and output it from the centralized control point when an error occurs. The program will definitely generate some intermediate messages in the process of processing logic. This information may not be directly placed in the data structure that can be seen by the centralized control point, and this intermediate information may be very important for error cause analysis. In order to solve this problem, the data structure that can be seen by the centralized control point can be changed, the intermediate results can also be stored in it, and then it can be output at the centralized control point. It can effectively solve various problems in application scenarios.
[0060] The traditional process-oriented flat output only shows fragments of the data structure, which makes it difficult to effectively describe the scene when the error occurs. With the centralized output oriented to business logic, the error log is no longer a fragment. The centralized log output method oriented to business logic puts the action of outputting logs when errors occur at a higher level of processing logic, which can effectively solve the limitations of the process-oriented log output method. In practice, the centralized output method oriented to business logic should be considered as the main method, combined with various other methods, which can effectively help R&D personnel troubleshoot problems and improve the robustness of software products.
[0061] Based on the same inventive concept, the present application also provides a device 200 for implementing log output for business logic, such as Figure 2 As shown, including:
[0062] An error code definition unit 210 is used to define an error code for processing a business logic module;
[0063] The log adding unit 220 is used to add the log content containing the error code to each sub-module according to the function of each sub-module of the processing business logic, and add a log for each sub-module;
[0064] The log content output unit 230 is used to, when the main module for processing business logic is called, output the log content corresponding to each submodule according to the predefined log priority level.
[0065] Preferably, the log adding unit includes:
[0066] An error code acquisition subunit, used to acquire the error code corresponding to each submodule according to the function of each submodule of the processing business logic;
[0067] The log adding subunit is used to add the error code to each corresponding submodule, thereby adding a log to each submodule.
[0068] Preferably, the log content output unit includes:
[0069] The module calling sub-unit is used for, when calling the main module for processing the business logic, the main module calls the sub-module included in the main module according to the business logic;
[0070] The log content output subunit is used for outputting the log content corresponding to the called submodule according to the predefined log priority when the submodule is executed.
[0071] The present application provides a method for implementing log output oriented to business logic, which can enable error log information to be output in a logic-oriented centralized manner, thereby solving the problems of error logs being scattered and difficult to maintain, and the output log information being easily repeated and causing a large amount of redundancy.
[0072] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that the specific implementation modes of the present invention can still be modified or replaced by equivalents. Any modification or equivalent replacement that does not depart from the spirit and scope of the present invention should be included in the scope of the claims of the present invention.
Claims
1. A method for implementing log output for business logic. It is characterized in that include: Define error codes for processing business logic modules; According to the functions of each submodule of the processing business logic, the log content containing the error code is added to each submodule, and logs are added to each submodule, including: According to the functions of each submodule of the processing business logic, obtain the error code corresponding to each submodule; Add the log content containing the error code to each corresponding sub-module, thereby adding a log to each sub-module; When the main module for processing business logic is called, the main module outputs the log content corresponding to each submodule according to the predefined log priority level, including: When the main module for processing business logic is called, the main module calls the submodules contained in the main module according to the business logic; When the submodule is executed, the main module that processes the business logic outputs the log content corresponding to the called submodule according to the predefined log priority.
2. The method according to claim 1, It is characterized in that Error codes, including: The name and error type of each sub-module of the processing business logic module.
3. The method according to claim 1, It is characterized in that Predefined log priorities, including: fatal, error, and debug.
4. The method according to claim 1, It is characterized in that Output the log content corresponding to the called submodule, including: Output the error code corresponding to the called submodule, the priority of the log, and the time and location of the log generation.
5. A device for implementing log output for business logic, It is characterized in that include: An error code definition unit is used to define error codes for processing business logic modules; A log adding unit, used to add the log content containing the error code to each sub-module according to the function of each sub-module of the processing business logic, and add a log to each sub-module, including: An error code acquisition subunit, used to acquire the error code corresponding to each submodule according to the function of each submodule of the processing business logic; A log adding subunit, used for adding the error code to each corresponding submodule, thereby adding a log to each submodule; The log content output unit is used to output the log content corresponding to each submodule according to the predefined log priority level when the main module for processing business logic is called, including: The module calling sub-unit is used for, when calling the main module for processing the business logic, the main module calls the sub-module included in the main module according to the business logic; The log content output subunit is used for outputting the log content corresponding to the called submodule according to the predefined log priority when the submodule is executed.
Citation Information
Patent Citations
Method and device for diagnosing fault event in data center
CN102143008A
Method and system for monitoring application logs
CN102981943A
Log service method based on rule
CN104461844A
Method for extending output level of Apache Log4j log
CN106155864A
Test method and device based on log buried point, storage medium and terminal
CN108710573A
