Rapid log collection strategy based on RUST attribute macro

Log collection code is automatically generated during the compilation phase through Rust attribute macros, which solves the code redundancy and performance problems in traditional log collection methods, achieves efficient and simplified log management and recording, and is suitable for large software systems.

CN118626340APending Publication Date: 2024-09-10XIAMEN CLOUD FRAME ANALYSIS TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410974844.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-19
Publication Date
2024-09-10

AI Technical Summary

Technical Problem

Traditional log collection methods lead to code redundancy, performance issues and management difficulties. Especially in large software systems, the log implementation of different modules is not unified, which increases complexity and performance overhead.

Method used

Use Rust attribute macros to automatically generate log collection code during the compilation phase, identify marked code fragments by parsing the syntax tree and insert logging instructions, and support multiple log levels and high-performance writing strategies, such as lock-free message queues and full APPEND mode files Write.

Benefits of technology

Simplify logging, reduce manual writing, reduce performance overhead, improve log flushing speed and maintainability, and support asynchronous storage and efficient log management.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118626340A
    Figure CN118626340A
Patent Text Reader

Abstract

According to the rapid log collection strategy based on the Rust attribute macro, the log code is generated in the compiling stage by defining the Rust attribute macro, and therefore the log collection function is integrated into the target code. The macro identifies the marked code snippets by parsing the syntax tree of the target code, and automatically generates and injects log code during compilation. The generated log code comprises log message formatting, log level setting and efficient log writing. According to the strategy, the log collection performance can be remarkably improved, the maintenance of log codes is simplified, a unified log collection mechanism is provided, and the method is suitable for the log collection requirement of a Rust system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to a log collection technology in software development, and specifically to a log collection strategy based on Rust attribute macros. Background Art

[0002] In software development, log collection is an important tool for recording system operation status, debugging information, error information, etc. Traditional log collection methods usually rely on manually writing a large amount of log code, which not only increases code complexity but may also cause performance problems. Especially in large software systems, the log collection requirements and implementation methods of different modules may vary, which further increases the complexity of log management.

[0003] The existing log collection system has the following limitations:

[0004] Code redundancy: Manually written logging code is prone to duplication and redundancy, making it difficult to maintain and update.

[0005] Performance issues: Logging code is usually executed at runtime, which will bring additional performance overhead.

[0006] Difficult management: The log implementations of different modules are not unified, which makes log management and analysis complicated.

[0007] Therefore, a log collection strategy based on Rust attribute macros is proposed to address the above problems. Summary of the invention

[0008] The purpose of the present invention is to provide a log collection strategy based on Rust attribute macros to solve the problems raised in the above background technology.

[0009] To achieve the above object, the present invention provides the following technical solutions:

[0010] A log collection strategy based on Rust attribute macros includes the following steps:

[0011] Step 1: Define a Rust attribute macro to automatically generate log collection code during the compilation phase;

[0012] Step 2: Use the attribute macro in the target code to mark the functions that need log collection;

[0013] Step 3: During compilation, the attribute macro injects the log code into the target code according to predefined rules to implement the log collection function;

[0014] Step 4: After the compilation is completed, the log collection code is deployed together with the target code to record the system operation information in real time.

[0015] Defining a Rust attribute macro involves parsing the target code's syntax tree, identifying the code snippets to be marked, and inserting logging instructions in those snippets.

[0016] The log code generated by the attribute macro includes the following:

[0017] Log messages are formatted with a timestamp, the name of the calling function, and the log level.

[0018] The log level settings include but are not limited to: Debug, Info, Warning, Error, and Fatal.

[0019] Optimized, low-overhead, thread-based log writing, supporting tokio asynchronous storage and consumption.

[0020] Preferably, for high performance, use a lock-free message queue, store the log in the queue first, and then flush it to disk, without blocking the caller.

[0021] Preferably, use full APPEND mode file writing to achieve fast writing by taking advantage of the high efficiency of hard disk sequential writing; when channel pressure increases, write logs in batches at one time.

[0022] Preferably, it supports waiting for the logs to be flushed to disk together to reduce the number of IO times.

[0023] Compared with the prior art, the present invention has the following beneficial effects:

[0024] 1. Simplify log recording: The log collection code automatically generated by the present invention through the attribute macro is not only readable and maintainable, but also does not need to be manually written, thereby reducing the waste of manpower.

[0025] 2. Reduce overhead: The present invention can automatically generate log collection code in the compilation phase by using attribute macros, thereby avoiding the performance overhead caused by executing the log code at runtime.

[0026] 3. Performance optimization: The present invention is based on threads, uses a lock-free message queue, and writes logs in full APPEND mode, thereby improving the speed of flushing logs to disk. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Figure 1 It is a schematic diagram of the system architecture, including a macro definition module, a target code module, a compiler module, a log code module and a log output module.

[0028] Figure 2 It is a flowchart of attribute macro parsing and generation, showing the two main steps of the parsing phase and the generation phase.

[0029] Figure 3It is an output flow chart that shows the log output process. DETAILED DESCRIPTION

[0030] In order to explain the technical content, structural features, achieved objectives and effects of the present invention in detail, the following is a detailed description in conjunction with the implementation modes and the accompanying drawings.

[0031] Example:

[0032] See also Figure 1 , this embodiment provides a technical solution:

[0033] A log collection strategy based on Rust attribute macros includes the following steps:

[0034] Step 1: Define a Rust attribute macro to automatically generate log collection code during the compilation phase;

[0035] Step 2: Use the attribute macro in the target code to mark the functions that need log collection;

[0036] Step 3: During compilation, the attribute macro injects the log code into the target code according to predefined rules to implement the log collection function;

[0037] Step 4: After the compilation is completed, the log collection code is deployed together with the target code to record the system operation information in real time.

[0038] Defining a Rust attribute macro involves parsing the target code's syntax tree, identifying the code snippets to be marked, and inserting logging instructions in those snippets.

[0039] The log code generated by the attribute macro includes the following:

[0040] Log messages are formatted with a timestamp, the name of the calling function, and the log level.

[0041] The log level settings include but are not limited to: Debug, Info, Warning, Error, and Fatal.

[0042] In this embodiment, logs are written based on threads, supporting tokio asynchronous storage and consumption.

[0043] In this embodiment, a lock-free message queue is used, and the log is first stored in the queue and then flushed to the disk without blocking the caller.

[0044] In this embodiment, the full APPEND mode file writing is used to achieve fast writing by utilizing the high efficiency of hard disk sequential writing; when the channel pressure increases, the logs are written in batches at one time.

[0045] In this embodiment, it supports waiting for the logs to be flushed to the disk together to reduce the number of IO times.

[0046] The present invention will be further described below in conjunction with the accompanying drawings:

[0047] 1. Macro definition module:

[0048] Specifically, by parsing the syntax tree of the target code, identifying the calling function name and the user-defined log level, a macro matching the corresponding level is generated, such as the Info macro. After the macro is expanded, it can be regarded as a statement in the target code, which contains logging instructions.

[0049] 2. Object code module:

[0050] Specifically, use the attribute macro of the level required by the business to mark the function that needs log collection.

[0051] 3. Compiler module:

[0052] Specifically, the attribute macro is parsed, and the log code is injected into the target code according to the predefined rules to realize the log collection function.

[0053] Log code module:

[0054] Specifically, log code is generated, including the formatting and output of log messages.

[0055] Log output module:

[0056] Specifically, the log information is output to the specified log file, supporting multiple log formats such as JSON, CSV, etc.

[0057] Specifically, logs are written based on threads, stored in lock-free message queues, and then consumed asynchronously by consumers.

[0058] Specifically, use full APPEND mode file writing to achieve fast writing by taking advantage of the high efficiency of hard disk sequential writing; when channel pressure increases, write logs in batches at one time.

[0059] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.

Claims

1. A fast log collection strategy based on Rust attribute macros, characterized in that: The strategy includes the following steps: Step 1: Define a Rust attribute macro to automatically generate log collection code during the compilation phase; Step 2: Use the attribute macro in the target code to mark the functions that need log collection; The log code generated by the attribute macro includes the following: Formatting of log messages; Setting the log level; Logs are efficiently written to the hard disk; Step 3: During compilation, the attribute macro injects the log code into the target code according to predefined rules to implement the log collection function; Step 4: After the compilation is completed, the log collection code is deployed together with the target code to record the system operation information in real time.

2. The fast log collection strategy based on Rust attribute macros as described in claim 1, characterized in that: The attribute macro parses the syntax tree of the target code, identifies the marked code fragments, and inserts logging instructions into these fragments.

3. The fast log collection strategy based on Rust attribute macro as described in claim 1, characterized in that: The formatting of the log message includes inserting a timestamp, a calling function name and a log information level.

4. The fast log collection strategy based on Rust attribute macros as described in claim 1, characterized in that: The log level settings include, but are not limited to, debug, information, warning, error, and fatal.

5. The fast log collection strategy based on Rust attribute macro as described in claim 1, characterized in that: The efficient writing of logs to the hard disk includes writing logs based on threads, using a lock-free message queue method, storing logs in a queue first and then flushing to the disk without blocking the caller, using full APPEND mode file writing, writing logs in batches at one time when channel pressure increases, and supporting waiting for logs to be flushed to the disk together.