Log recording method and device based on dynamic context awareness and medium
Through dynamic proxy and context information management mechanism, the problem of incomplete logging in the existing technology is solved, and the context information is fully recorded in asynchronous and responsive programming environments is realized, which improves the accuracy and efficiency of log analysis and problem positioning.
Patent Information
- Application Number
- CN202510501141.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-21
- Publication Date
- 2025-08-01
AI Technical Summary
Existing automatic logging operation logging methods cannot effectively capture context information in asynchronous threads and responsive programming, resulting in incomplete logging, affecting problem location and project management costs.
Through dynamic proxy and context information management mechanisms, the target method is intercepted, the log recording section is inserted, the context information is recorded and asynchronously sent to the message queue, ensuring that the log contains complete context data.
It realizes the accuracy of tracking the request life cycle between different services or methods, improves the analysis efficiency of logs and the accuracy of problem location, and reduces the risk of system response blocking.
Smart Images

Figure CN120407534A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and particularly to a log recording method, device and medium based on dynamic context awareness. Background Art
[0002] During the delivery process of software projects, it is inevitable to perform operations such as adding, modifying, and deleting databases. During the operation of the database, if data is not backed up in time or operation logs are not recorded, it is difficult to determine abnormal operations in a timely manner when business operations have problems, increasing the time cost and economic cost of project management. Therefore, effectively recording database operation information during the delivery process is a necessary matter, which can not only show the complete flow process of project data but also facilitate querying business operation records.
[0003] In existing methods for automatically recording operation logs, AOP (Aspect-Oriented Programming) aspects are usually used to achieve automatic recording. AOP decouples the log logic from the business logic through a horizontal aspect and inserts log records through dynamic proxy or bytecode enhancement without modifying the business code. However, this method has limitations in the proxy mechanism. Static methods, private methods, and final methods in business code cannot be intercepted by dynamic proxy, resulting in the failure of the aspect; and in asynchronous threads or reactive programming, it is also difficult for AOP aspects to pass context information, resulting in context loss. Summary of the Invention
[0004] To solve the above problems, the present application proposes a log recording method based on dynamic context awareness, including:
[0005] Receiving a response request, determining a target class corresponding to the execution of the response request, and calling a target method in the target class;
[0006] Intercepting the calling process of the target method through a dynamic proxy object corresponding to the target class and inserting a log recording aspect;
[0007] Executing the target method, and recording the log of the response request based on a preset trigger point cut through the log recording aspect.
[0008] On the other hand, the present application also proposes a log recording device based on dynamic context awareness, including:
[0009] At least one processor; and,
[0010] A memory communicatively connected to the at least one processor; wherein,
[0011] The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute a log recording method based on dynamic context awareness as described in the above example.
[0012] On the other hand, the present application also proposes a non-volatile computer storage medium storing computer-executable instructions, and the computer-executable instructions are set to be a log recording method based on dynamic context awareness as described in the above example.
[0013] The log recording method based on dynamic context awareness proposed by the present application can bring the following
[0014] Beneficial effects:
[0015] By combining the context information management mechanism, these information can be dynamically captured during the execution of the method and appended to the log. This can ensure that the log recording is no longer isolated, but contains the complete context related to the specific operation, making subsequent analysis and problem tracking more accurate and effective.
[0016] By embedding the context information into the log, the life cycle of the request can be easily traced between different services or methods. The dynamic proxy and context information management mechanism can ensure that each method call carries sufficient context information, enabling the execution status, input and output parameters, and related context data of each method to be clearly seen during debugging. This not only helps to quickly discover errors, but also makes the log more developer-friendly. Description of the Drawings
[0017] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation of the present application. In the drawings:
[0018] Figure 1 It is a schematic flow chart of a log recording method based on dynamic context awareness in an embodiment of the present application;
[0019] Figure 2 It is a schematic diagram of a log recording device based on dynamic context awareness in an embodiment of the present application. Detailed Embodiments
[0020] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below in conjunction with specific embodiments of this application and the corresponding drawings. Obviously, the described embodiments are only a part of the embodiments of this application, rather than all of the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments in this application without creative efforts belong to the scope of protection of this application.
[0021] The following will detail the technical solutions provided by each embodiment of this application in conjunction with the drawings.
[0022] As Figure 1 shown, an embodiment of this application provides a logging method based on dynamic context awareness, including:
[0023] S101: Receive a response request, determine the target class corresponding to the execution of the response request, and call the target method in the target class.
[0024] Specifically, when receiving a response request, the response request or task carries necessary context information, including user information, request ID, transaction ID, etc. Create a context object corresponding to the response request, and initialize the corresponding parameters of the context object based on the context information.
[0025] Furthermore, determine the target class corresponding to the execution of the response request, where the target class contains one or more target methods for executing the response request, determine the required target method, and call the required target method.
[0026] It should be noted that each client request (such as an HTTP request, an RPC request, etc.) has its corresponding target class and corresponds to one or more target methods, and the target methods implement the specific processing logic for these requests. The target class is the actual business class in the system, which contains the core business logic of the system. For example, when a user submits a form, the system will use the target method to save data, verify information, calculate results, etc.
[0027] S102: Intercept the call process of the target method through the dynamic proxy object corresponding to the target class, and insert a logging aspect.
[0028] Specifically, before the target method is executed, intercept the call process of the target method through the dynamic proxy object corresponding to the target class, and insert a logging aspect according to the method corresponding to the aspect logic.
[0029] Before that, it is necessary to generate a dynamic proxy object corresponding to the target class. Specifically, the bytecode in the target class is read through a bytecode enhancement library, and according to the aspect logic and exception capture logic, the bytecode is modified to obtain a dynamic proxy object corresponding to the target class, and the dynamic proxy object can insert additional logic during method execution.
[0030] Among them, the aspect logic refers to inserting additional operations before or after the execution of the target method, such as logging, statistical execution time, transaction management, etc.; the exception capture logic refers to adding additional code to the exception handling process of the method, such as capturing exceptions and performing certain cleanup operations.
[0031] It should be noted that the bytecode enhancement library will load the bytecode of the target class, usually a.class file. Bytecode is a binary file generated by the Java compiler after compiling the Java source code, which describes information such as the structure, methods, fields, and constant pool of the class.
[0032] S103: Execute the target method, and record the log of the response request through the log record aspect based on a preset trigger pointcut.
[0033] Specifically, when it is determined that the target method has not been executed, the context information of the response request is obtained through the log record aspect and recorded in the log. When it is determined that the target method has been executed, the return information of the response request is obtained through the log record aspect and recorded in the log. When it is determined that an exception occurs during the execution of the target method, the exception information is recorded in the log through the log record aspect.
[0034] Among them, through the exception capture method of the dynamic proxy object, the execution exception code is captured, and when it is determined that an exception occurs during the execution of the target method, the execution is stopped.
[0035] Furthermore, the log data is sent asynchronously to the message queue, and based on the log reading requirements, the log data is read from the message queue, and the read log is output and stored in the log database.
[0036] It should be noted that during the method call process, the key data of the method execution is dynamically recorded, and the recorded content is automatically adjusted according to the context information. Recording before method execution: Before the method call, the context information of the current request (such as user ID, request ID, etc.) and the parameter values of the method are obtained through the AOP interceptor, and then the log is recorded. This step helps to trace the starting stage of the request. Recording after method execution: When the method execution is completed (without exception), the return value, execution time, etc. of the method are recorded. This step can help analyze the result and performance of the method execution.
[0037] Among them, when an exception is thrown during method execution, the dynamic proxy object will catch the exception and record the exception information (such as exception type, exception message, stack trace, etc.) in the log. The exception log not only includes the exception information but also carries the context information of the current request (such as request ID, user ID), which can help developers trace the root cause of the problem. For example, the entire call chain can be found in the distributed tracing system (such as Zipkin, Jaeger, etc.) through the request ID to view what exceptions occurred during the processing of this request. The exception information will be automatically associated with the context of the current request (for example, the current user, request ID), ensuring that it is easier to trace the location and related information where the exception occurred during troubleshooting.
[0038] It should be noted that during the method execution process, the log data will not be directly written to the persistent storage, but will first be written to a buffer or message queue (such as Kafka, RabbitMQ, etc.). The purpose of this method is to avoid blocking the execution of the main business logic during the log recording process. The log data is read from the queue and stored in the final log database or log system (such as ELK, Splunk, etc.). Through asynchronous processing, the system can handle a large amount of log data without affecting the real-time response of the system due to log operations.
[0039] In the embodiments of the present application, the execution process may be as follows: First, relevant fields required in the actual business are assembled into an entity table, which includes database execution statements, operators, operation types (update, delete, insert), etc. The fields of this log will be designed as a front-end page for display. Define a common aspect annotation and automatically introduce this aspect when operating on business documents. Then, define a pointcut. As the name implies, the aspect focuses on the whole, while the pointcut focuses on a specific aspect. It is necessary to determine where the pointcut is executed. During the process of recording operation logs, it is necessary to define that during the process of numerical changes in business documents or the addition of new documents, this pointcut does not need to be defined for some simple business logic constraint validations. Then, define Advice notifications. Using the five timing opportunities of the notifications, enhancement actions can be completed at certain pointcuts. For example, to enhance the pre-notification to obtain key information such as the execution parameters and context variables of relevant methods at this time. Then, when the pointcut of the aspect is captured, it indicates that relevant business information can be recorded at this time. Automatically obtain the method name, parameters, and database execution statements executed in the context in the inserted aspect and record them. Then, record the logged-in user information, login date, and login IP in the current system, which usually involves obtaining relevant information of the current system from the security context or session. When a user calls the document update operation of the background interface, the aspect will automatically capture and identify the Session in the current system process, from which relevant information of the current user and relevant system dates can be obtained. However, the actual login IP of this user is not actually recorded in the session. The method of calling an external interface can be considered to obtain the IP where the user is located. Using aspect-oriented programming technology, log records can be automatically inserted into the intermediate table designed in the first step in the business logic.
[0040] It should be noted that the technical implementation method of the present application may be: Define the table structure of the audit log, where ID is the unique primary key of the database table, operationType is the operation type of the database (insert, update, or delete), uid represents the session ID when the database executes the DML statement, uname represents the name of the current operator, ip represents the IP address where the current operator is located, funcName represents which function is operated, createDate represents the date when the current database session is created, and param represents the specific statement executed by the current database.
[0041] Define the aspect AOP for log recording, use the around enhancement pointcut and exception throwing enhancement pointcut of the custom annotation, and at the same time perform AOP aspect processing on the method of the custom annotation. For example, use the @Aspect annotation to declare the aspect on the class, use the @PointCut annotation to define the pointcut and mark the method, and use @Around and @AfterThrowing to indicate the pointcut timing.
[0042] Obtain relevant business execution logic in the aspect. When the code performs relevant database operations, the aspect will automatically execute the previously defined method. At this time, parameters can be organized and inserted into the log table, and the operationType is assigned according to the first keyword (SELECT, UPDATE, INSERT, DELETE) of the relevant statement executed in the code.
[0043] This application reduces the dependence on hard coding and improves the flexibility and maintainability of the system by dynamically configuring the aspect and the logging strategy. By combining the context information management mechanism, these information can be dynamically captured during the method execution and appended to the log. This can ensure that the log record is no longer isolated, but contains the complete context related to the specific operation, making subsequent analysis and problem tracking more accurate and effective.
[0044] By embedding the context information into the log, the life cycle of the request can be easily traced between different services or methods. The dynamic proxy and the context information management mechanism can ensure that each method call carries sufficient context information, enabling the execution status, input and output parameters, and related context data of each method to be clearly seen during the debugging process. This not only helps to quickly discover errors, but also makes the log more friendly to developers.
[0045] As Figure 2 shown, an embodiment of this application also proposes a log recording device based on dynamic context awareness, including:
[0046] At least one processor; and,
[0047] A memory communicatively connected to the at least one processor; wherein,
[0048] The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute a log recording method based on dynamic context awareness as described in any one of the above embodiments.
[0049] An embodiment of this application also provides a non-volatile computer storage medium storing computer-executable instructions, and the computer-executable instructions are set to: a log recording method based on dynamic context awareness as described in any one of the above embodiments.
[0050] Each embodiment in this application is described in a progressive manner. For the same or similar parts among the embodiments, reference can be made to each other, and the key points of each embodiment are the differences from other embodiments. In particular, for the device and medium embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and reference can be made to the relevant parts of the method embodiments for the relevant content.
[0051] The devices and media provided in the embodiments of this application correspond one by one to the methods. Therefore, the devices and media also have beneficial technical effects similar to the corresponding methods. Since the beneficial technical effects of the methods have been described in detail above, the beneficial technical effects of the devices and media will not be elaborated here.
[0052] Those skilled in the art should understand that the embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk memories, CD-ROMs, optical memories, etc.) containing computer-usable program code.
[0053] The present invention is described with reference to the flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each flow and / or block in the flowchart and / or block diagram, as well as the combination of flows and / or blocks in the flowchart and / or block diagram, can be realized by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate a device for realizing the functions specified in Figure 1 one or more flows and / or blocks Figure 1 one or more blocks.
[0054] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory generate a manufactured article including an instruction device that realizes the functions specified in Figure 1 one or more flows and / or blocks Figure 1 one or more blocks.
[0055] These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 steps of the functions specified in one block or multiple blocks.
[0056] In a typical configuration, a computing device includes one or more processors (CPUs), an input / output interface, a network interface, and memory.
[0057] The memory may include non-permanent memory in the computer-readable medium, in the form of random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash memory (flash RAM). The memory is an example of a computer-readable medium.
[0058] Computer-readable media includes permanent and non-permanent, removable and non-removable media that can store information by any method or technology. The information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette tapes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media, such as modulated data signals and carrier waves.
[0059] It should also be noted that the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, commodity or device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or also includes elements inherent to such process, method, commodity or device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the presence of additional identical elements in the process, method, commodity or device comprising the element.
[0060] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, various changes and modifications can be made to the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included within the scope of the claims of the present application.
Claims
1. A logging method based on dynamic context awareness, characterized in that Including: Receiving a response request, determining a target class corresponding to the execution of the response request, and invoking a target method in the target class; Intercepting the invocation process of the target method through a dynamic proxy object corresponding to the target class, and inserting a log record aspect; Executing the target method, and recording the log of the response request through the log record aspect based on a preset trigger pointcut; 2. A log recording method based on dynamic context awareness according to claim 1, characterized in that, The recording the log of the response request through the log record aspect based on a preset trigger pointcut specifically includes: Determining that the target method has not been executed, obtaining context information of the response request through the log record aspect, and recording the context information in the log; Determining that the execution of the target method is completed, obtaining return information of the response request through the log record aspect, and recording the return information in the log; Determining that an exception occurs during the execution process of the target method, and recording the exception information in the log through the log record aspect.
3. A log recording method based on dynamic context awareness according to claim 2, characterized in that The determining that an exception occurs during the execution process of the target method specifically includes: Capturing an execution exception code through an exception capture method of the dynamic proxy object, determining that an exception occurs during the execution process of the target method, and stopping the execution.
4. A logging method based on dynamic context awareness according to claim 3, characterized in that, After the determining that an exception occurs during the execution process of the target method and stopping the execution, the method further includes: Returning the exception information to the running interface.
5. A log recording method based on dynamic context awareness according to claim 1, characterized in that, After the recording the log of the response request based on a preset trigger pointcut, the method further includes: Asynchronously sending the log data to a message queue; Reading the log data from the message queue based on a log reading requirement, and outputting and storing the read log in a log database.
6. The logging method based on dynamic context awareness according to claim 1, wherein, Before the passing through the dynamic proxy object corresponding to the target class, the method further includes: Reading bytecode in the target class through a bytecode enhancement library; Modifying the bytecode according to aspect logic and exception capture logic to obtain a dynamic proxy object corresponding to the target class.
7. A log recording method based on dynamic context awareness according to claim 6, characterized in that, The intercepting the invocation process of the target method through the dynamic proxy object corresponding to the target class and inserting a log record aspect specifically includes: Before the execution of the target method, intercepting the invocation process of the target method through the dynamic proxy object corresponding to the target class; Inserting a log record aspect according to the aspect logic.
8. A log recording method based on dynamic context awareness according to claim 1, characterized in that After the receiving the response request, the method further includes: Creating a context object corresponding to the response request; Initializing parameters in the context object based on the context information of the response request.
9. A logging device based on dynamic context awareness, characterized in that, Including: At least one processor; And, A memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute a method for log recording based on dynamic context awareness as described in any one of claims 1 to 8.
10. A non-volatile computer storage medium storing computer-executable instructions, characterized in that, The computer-executable instructions are set to a method for log recording based on dynamic context awareness as described in any one of claims 1 to 8.
Citation Information
Patent Citations
System log processing method and device and medium
CN116126819A
Log management method, system and device and storage medium
CN118277211A
Method and device for realizing full-link tracking of service data and storage medium
CN119544772A
Log generation method, system, device and storage medium
CN119766674A
Full-link log management method and device based on request address, equipment and medium
CN119814538A