Business change log recording method, electronic equipment and readable storage medium

By obtaining and saving field change information of business data and using Spring event mechanism to generate logs, the problem of coupling log records and business code in the existing technology is solved, and detailed field-level logging and decoupling of logs and business codes is realized. .

CN119201957APending Publication Date: 2024-12-27ZHONGKE YUNGU TECH
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202411363613.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-27
Publication Date
2024-12-27

AI Technical Summary

Technical Problem

In the prior art, the business change logging method needs to be encoded in the business method, resulting in high coupling with the business code, and the field-level detailed logging cannot be realized. In addition, the AOP sectional technology can only record method-level logs and cannot record internal methods. detail.

Method used

By obtaining field change information of business data, saving in thread local variables, and using the Spring event mechanism to generate change logs, decoupling log records and business codes, and realizing field-level detailed logging.

Benefits of technology

It realizes field-level detailed logging, which facilitates post-maintenance, and obtains change logs through event mechanisms, avoiding the coupling between business code and log records.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119201957A_ABST
    Figure CN119201957A_ABST
Patent Text Reader

Abstract

The invention provides a business change log recording method, electronic equipment and a readable storage medium, and the method comprises the steps: obtaining field change information of business data, and storing the field change information in a thread local variable, the field change information comprising an original value and an update value of a field; and when a service change submission request is received, subscribing to the spring event to obtain the change detail information, and generating a change log according to the change detail information, the change detail information including the field change information. According to the method and the device, the change fields in the business change are recorded, and the change logs are obtained through the event mechanism and are stored in the database, so that log records are more detailed, the log records are decoupled from the business, and later maintenance is facilitated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data processing technology, and in particular to a business change log recording method, an electronic device and a readable storage medium. Background Art

[0002] Change logs exist in almost every system, and these systems have a set of APIs for recording operation logs. Operation logs are different from system logs. Operation logs must be simple and easy to understand. Users can view change logs to intuitively obtain important business information such as the time when the change occurred, the content of the change, and the operator.

[0003] The current logging methods include manual logging, AOP aspect logging, and so on. However, the manual logging method requires coding logging in the business method, which is highly coupled with the business code and inconvenient to maintain. The AOP aspect method is executed through interception method, and the logging granularity is at the method call level. It can only record method input parameters and results, and has no perception of the details occurring inside the method, and cannot perform field-level logging.

[0004] Therefore, there is an urgent need for a business change log recording method, an electronic device and a readable storage medium to solve the above problems. Summary of the invention

[0005] The technical problem solved by the present invention is to provide a business change log recording method, an electronic device and a readable storage medium, which can record the changed fields in the business change, obtain the change log through the event mechanism, and save it to the database, decoupling the log record from the business, making the log record more detailed and convenient for later maintenance.

[0006] The present invention solves the technical problem by adopting the following technical solutions:

[0007] A business change log recording method includes: obtaining field change information of business data and saving the field change information in a thread local variable, wherein the field change information includes the original value and updated value of the field; when receiving a business change submission request, subscribing to a spring event to obtain change detail information, and generating a change log according to the change detail information, wherein the change detail information includes at least one field change information.

[0008] Optionally, after the step of obtaining the field change information of the business data, the step includes: when the script updates the database, obtaining the change detail information from the thread local variable, and broadcasting the change detail information through the spring event mechanism.

[0009] In a preferred embodiment of the present invention, before the above-mentioned step of obtaining field change information of business data and saving the field change information in thread local variables, it includes: in response to executing the business data, detecting whether the business data has changed; when detecting that the business data has changed, outputting change notification information.

[0010] In a preferred embodiment of the present invention, the above-mentioned acquisition of field change information of business data includes: acquiring the business interface of the business data that has changed; when the business interface is a preset interface, acquiring the business change by using the cglib database dynamic enhancement method to record the field change information of the business data.

[0011] In a preferred embodiment of the present invention, after the above-mentioned steps of subscribing to the spring event to obtain the change detail information when receiving the business change submission request, and generating a change log based on the change detail information, it includes: obtaining the call chain information corresponding to the execution of the business data; associating the change log with the call chain information.

[0012] In a preferred embodiment of the present invention, before the above-mentioned step of obtaining field change information of business data and saving the field change information in thread local variables, it includes: generating call chain information in response to executing the business data, and the call chain information includes a call chain identifier and a call chain name.

[0013] In a preferred embodiment of the present invention, the step of obtaining change detail information from the thread local variables when the script updates the database includes: obtaining user information, call chain information and at least one field change information during the execution of the business data; generating the change detail information based on the user information, call chain information and at least one field change information.

[0014] In a preferred embodiment of the present invention, after the above-mentioned steps of subscribing to the spring event to obtain the change detail information when a business change submission request is received, and generating a change log based on the change detail information, it includes: storing the change log in a business database, or storing it as a file; deleting the change detail information stored in the thread local variable.

[0015] An electronic device comprises: a memory and a processor, wherein a business change log recording program is stored in the memory, and when the business change log recording program is executed by the processor, the steps of the business change log recording method as described in any one of the above are implemented.

[0016] A readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the business change log recording method as described in any one of the above are implemented.

[0017] The technical effect achieved by the present invention using the above technical solution is: by recording the original value and the changed value of the changed field in the business data, the log record is a more detailed record at the field level; the change log is obtained and stored through the event subscription mechanism, and the log record is decoupled from the business code, which is convenient for the maintenance of subsequent projects.

[0018] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the following specifically cites a preferred embodiment and describes it in detail with the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 A flowchart of a method for recording a business change log according to the present invention;

[0020] Figure 2 The present invention is a flowchart of a method for recording change logs in a cglib enhanced manner. DETAILED DESCRIPTION

[0021] In order to further explain the technical means and effects taken by the present invention to achieve the predetermined invention purpose, the embodiments of the present invention are described in detail below. Examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements with the same or similar functions. The embodiments described below are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the embodiments of the present invention. Through the description of the specific implementation methods, the technical means and effects taken by the present invention to achieve the predetermined purpose can be more deeply and specifically understood, and the attached drawings are only for reference and explanation purposes, and are not used to limit the present invention.

[0022] See also Figure 1 , Figure 1 The present invention is a flowchart of a method for recording business change logs.

[0023] like Figure 1 As shown, the business change log recording method provided in this embodiment includes the following steps:

[0024] S11: Obtain field change information of business data, and save the field change information in thread local variables; wherein the field change information includes the original value and updated value of the field.

[0025] Field change information is the data change information of business information. The original value is the value before the business information is changed, and the updated value is the value after the business information is changed.

[0026] Thread local variables (ThreadLocal) provide a copy of the variable value for each thread that uses the variable. It is a special thread binding mechanism in Java. Each thread can independently change its own copy without conflicting with the copies of other threads.

[0027] S12: When receiving a business change submission request, subscribe to the spring event to obtain the change details information, and generate a change log according to the change details information.

[0028] The change details information includes at least one field change information. The business change submission request is to submit the changed business data to the database for storage.

[0029] A change log is a document or file that records changes to a system, application, document, or any other type of data. It records all important changes in detail, including when the change was made, what was changed, why the change was made, and who performed the change.

[0030] Specifically, when the business code calls the set method of the enhanced Bean, the enhanced logic will save the field change information of the Bean attribute to the thread context (without business awareness). After the business code completes the business operation (changes data), all field change information is stored in the thread context, and the business change submission event is listened to through the spring event mechanism (synchronous), and the data change details in the thread context are retrieved in the spring event listener to generate an MQ message, and then the MQ consumer performs time-consuming operations such as parsing and saving the change log.

[0031] Through the above method, the original value and changed value of the changed field in the business data are recorded, so that the log record is a more detailed record at the field level; the change log is obtained and saved through the event mechanism, and the log record is decoupled from the business code, which is convenient for the maintenance of the later project.

[0032] Optionally, after the step of obtaining the field change information of the business data, the step includes: when the script updates the database, obtaining the change detail information from the thread local variable, and broadcasting the change detail information through the spring event mechanism.

[0033] Spring Events is a mechanism provided by the Spring framework for delivering event notifications between different parts of an application. This mechanism is based on the Observer Pattern, allowing components in an application (such as services, dao layers, and controllers) to publish events when specific events occur, and other components interested in these events can register as listeners to receive these events.

[0034] It should be noted that if only the change details are obtained from the thread local variables when the script updates the database; but if no business change submission request is received subsequently, the change log will not be recorded, thereby achieving consistency between the business and the log.

[0035] Optionally, before the step of obtaining field change information of business data and saving the field change information in thread local variables, the method includes: detecting whether the business data has changed in response to executing the business data; and outputting change notification information when detecting that the business data has changed.

[0036] Exemplarily, the business data may be a business bean. When a user requires the system or terminal device to perform an operation on a business bean, the user may initiate an operation instruction (such as query and modification) corresponding to the business bean to the terminal device. The terminal device may respond to the operation instruction initiated by the user and detect whether the business bean has changed. When a business bean changes, a change notification is sent to the control center or log change module of the system or terminal, and finally, whether a change log needs to be recorded is confirmed based on the changed business bean.

[0037] Optionally, when a change in the current business is detected, after the step of outputting the change notification information, it includes: obtaining the business interface of the business data that has changed; when the business interface is a preset interface, obtaining the business change by using the cglib database dynamic enhancement method to record the field change information of the business data.

[0038] Among them, cglib is a runtime code generation library based on Java bytecode technology.

[0039] Specifically, by using the cglib database to dynamically enhance the set method, you can update the properties of the business bean and record the values ​​before and after the field change. Therefore, while implementing the business bean change, you can also obtain the original value and updated value of the changed field in the business bean.

[0040] Exemplarily, by using the CGLIB (Code Generation Library) library, the set method of the Java Bean is dynamically enhanced at runtime. When the set method is called, the original value and the updated value of the field are first recorded and saved in the thread local variable ThreadLocal.

[0041] In this embodiment, the business information is a business bean. Through the enhanced set method, when updating the business bean attribute, the values ​​of its fields before and after the change are recorded, and the deleted fields, modified fields and / or newly added fields in the business bean are described in an explanatory manner, thereby obtaining field change information. For example, field a at position X in the business bean is changed to field b. There can be multiple changes in one business change, so when a change occurs, a record is made, and one record can also contain multiple field change information.

[0042] Optionally, before the step of obtaining field change information of business data and saving the field change information in thread local variables, the method includes: generating call chain information in response to executing the business data, the call chain information including a call chain identifier and a call chain name.

[0043] Call chain: When the system completes a business call, the call information (time, interface, level, result) between services is logged, and then all the logged data is connected into a tree chain to generate a call chain.

[0044] The call chain identifier includes: the call chain name and ID, etc.

[0045] Optionally, when a business change submission request is received, the spring event is subscribed to obtain the change detail information, and after the step of generating a change log based on the change detail information, it includes: obtaining the call chain information corresponding to the execution of the business data; and associating the change log with the call chain information.

[0046] Specifically, when the microservice receives a business request, the unified framework will intercept the request and check whether there is a call chain id in the request. If not, a unique call chain id will be generated. Then, the unified framework will pass the call chain to the downstream business. The log collection service will obtain and record the call chain information when collecting application logs and interface logs, so that developers can search logs through the call chain when locating problems, thereby connecting related logs under the same call chain.

[0047] Among them, the unified framework is a one-stop microservice rapid development solution within the company, including: basic component library, basic service aggregation SDK, business gateway, authentication and authorization system, multi-tenant system, and front-end and back-end construction tools that support rapid construction of project basic projects.

[0048] Optionally, when the script updates the database, the step of obtaining change detail information from the thread local variables includes: obtaining user information, call chain information and at least one field change information during the execution of the business data; generating the change detail information based on the user information, call chain information and at least one field change information.

[0049] The field change information of the business bean in the current business operation, the call chain information corresponding to the current business operation, and the user information executing the current business operation are stored in the thread local variables / thread context. In order to obtain the change log and user information through the call chain information later, it is necessary to associate the change log and user information with the call chain to generate the change details information, and finally broadcast the change details information through the spring event mechanism, thereby decoupling the business code from the log record.

[0050] Specifically, by combining the call chain tracking capability of the unified framework, the call chain id of the user operation is recorded when recording the change log. When querying the operation log later, the corresponding log can be directly retrieved according to the call chain id, so as to know what data is updated by a certain operation of the user; especially in the system of microservice architecture, the call chain mechanism can quickly check which user changed a certain piece of data in the online environment and what operation, as well as all the log information associated with this operation, so as to facilitate the location of online problems and business audits.

[0051] Optionally, when a business change submission request is received, the spring event is subscribed to obtain the change detail information, and after the step of generating a change log based on the change detail information, it includes: storing the change log in a business database, or storing it as a file; deleting the change detail information stored in the thread local variable.

[0052] After receiving the business change submission request, the business change details are obtained through the spring event, and then the change details are saved in the business database, and the field change information, call chain and user information stored in the thread local variables are deleted.

[0053] In other implementations, after the business field change information is stored in the thread local variable, or after the change detail information is sent out through the spring event, if no business change submission request is received subsequently in the current business operation, the field change information stored in the thread local variable can also be deleted.

[0054] See also Figure 2 , Figure 2 The present invention is a flowchart of a method for recording change logs in a cglib enhanced manner.

[0055] like Figure 2 As shown, the cglib enhanced method for recording change logs provided by this implementation includes the following processes:

[0056] The user performs business operations to query business beans. When it is detected that the business bean has changed, determine whether the business bean change needs to record a change log. If necessary, use the CGLIB (Code Generation Library) library, which is a code generation library. After enhancing the set method, call the set method again to record the original value and updated value of the field (that is, the field change information), save it in the thread local variable ThreadLocal, and implement the change of the business bean. If not required, call the set method directly to change the business bean. After the business bean is changed by the set method, the dao layer executes seql to save the change to the business database, and when the business is saved, the change details are obtained through the spring event subscription. After the business changes are saved in the database, the operation results are returned.

[0057] Specifically, when executing a database script to update the database, obtain the change details information recorded in the thread local variable ThreadLocal (before recording the field change information, the call chain information and user information of the business operation are also recorded), and send the change details through the spring event mechanism.

[0058] Specifically, when submitting business changes to the database, subscribe to spring events, obtain change details, and record change logs; if other operations produce exceptions after executing the database script update, resulting in the cancellation / rollback of the business changes, the change logs will not be recorded, thereby ensuring the transaction consistency of the logs and business operations.

[0059] The business change log recording method of this embodiment records changes in business bean fields through the enhanced set method of the cglib library; the field change information is stored in the thread local variable, and the change information is obtained through the spring event mechanism to save it in the database, decoupling the log record from the business code to facilitate the maintenance of the later project; the change log can be quickly queried through the call link information associated with the change log, and if no business change submission request is received, the change field and other information stored in the thread local variable will not be saved to the database, ensuring the transaction consistency of the log and business operations.

[0060] The present invention also provides an electronic device, comprising: a memory and a processor, wherein a business change log recording program is stored in the memory, and when the business change log recording program is executed by the processor, the steps of the business change log recording method as described in any one of the above are implemented.

[0061] The present invention also provides a readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the business change log recording method as described in any one of the above are implemented.

[0062] It should be understood that, although the steps in the flowchart of the accompanying drawings are displayed in sequence as indicated by the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least a portion of the steps in the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a portion of the sub-steps or stages of other steps.

[0063] Through the description of the above implementation methods, those skilled in the art can clearly understand that the embodiments of the present invention can be implemented by hardware, or by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the embodiments of the present invention can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.), including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each implementation scenario of the embodiments of the present invention.

[0064] The preferred embodiments of the present invention are described in detail above in conjunction with the accompanying drawings, but the present invention is not limited to the specific details in the above embodiments. The above embodiments and drawings are exemplary. The modules or processes in the drawings are not necessarily required to implement the embodiments of the present invention and cannot be understood as limitations on the present invention. Within the technical concept of the present invention, the technical scheme of the present invention can be subjected to a variety of simple modifications and combinations, and these simple modifications and combinations all belong to the protection scope of the present invention.

Claims

1. A method for recording a business change log, characterized in that: include: Obtaining field change information of business data, and saving the field change information to thread local variables, the field change information including the original value and updated value of the field; When a business change submission request is received, a spring event is subscribed to obtain change detail information, and a change log is generated according to the change detail information, wherein the change detail information includes at least one field change information.

2. The business change log recording method according to claim 1, characterized in that: After the step of obtaining the field change information of the business data, the following steps are included: When the script updates the database, the change detail information is obtained from the thread local variable, and the change detail information is broadcast through the spring event mechanism.

3. The business change log recording method according to claim 1, characterized in that: Before the step of obtaining the field change information of the business data and storing the field change information in the thread local variable, the method includes: In response to executing the business data, detecting whether the business data is changed; When a change is detected in the business data, a change notification message is output.

4. The business change log recording method according to claim 3, characterized in that: The obtaining of field change information of business data includes: Obtaining a business interface for the business data that has changed; When the business interface is a preset interface, the business change is obtained by using the cglib database dynamic enhancement method to record the field change information of the business data.

5. The business change log recording method according to claim 2, characterized in that: After the step of subscribing to the spring event to obtain the change details information when receiving the business change submission request and generating the change log according to the change details information, it includes: Obtain call chain information corresponding to executing the business data; The change log is associated with the call chain information.

6. The business change log recording method according to claim 5, characterized in that: Before the step of obtaining the field change information of the business data and storing the field change information in the thread local variable, the method includes: In response to executing the business data, call chain information is generated, and the call chain information includes a call chain identifier and a call chain name.

7. The service change log recording method according to claim 6, characterized in that: The step of obtaining change detail information from the thread local variable when the script updates the database includes: When the script updates the database, user information, call chain information, and at least one field change information in the process of executing the business data are obtained; The change detail information is generated according to the user information, the call chain information and at least one field change information.

8. The business change log recording method according to claim 1, characterized in that: After the step of subscribing to the spring event to obtain the change details information when receiving the business change submission request and generating a change log according to the change details information, it includes: Storing the change log in a business database or as a file; The change detail information stored in the thread local variable is deleted.

9. An electronic device, characterized in that: The electronic device comprises: a memory and a processor, wherein a service change log recording program is stored in the memory, and when the service change log recording program is executed by the processor, the steps of the service change log recording method according to any one of claims 1 to 8 are implemented.

10. A readable storage medium, characterized in that: The readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the service change log recording method according to any one of claims 1 to 8 are implemented.

Citation Information

Cited By

  • Master data management system, method and equipment

    CN120277116A

  • Database application log level modification method and device, electronic equipment and storage medium

    CN120849220A