Method and system for adding identification in log printing based on specific scene
By dynamically appending log information to source code files during compilation, the performance loss and compatibility problems caused by log frameworks and bytecode editing frameworks in the prior art are solved, and efficient log printing and problem tracking are achieved.
Patent Information
- Application Number
- CN202510514424.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-23
- Publication Date
- 2025-07-25
AI Technical Summary
Existing log frameworks and bytecode editing frameworks cause performance loss and compatibility issues when printing logs, affecting system performance and startup speed, and making it difficult to track location issues.
The source code file is converted into a syntax tree through the compiler, and the annotation processor dynamically appends the class name, method name and line number information of the log print statement during compilation, and trims the syntax tree to generate better compilation results.
During the compilation period, log information is dynamically added without the intervention of R&D personnel, which reduces compiler overhead, improves the perfection of log printing and problem positioning efficiency, and reduces system performance losses.
Smart Images

Figure CN120371315A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of Java R & D and operation and maintenance, and in particular to a method and system for adding identifiers to log printing based on specific scenarios. Background Art
[0002] The statements in this section merely provide background technical information related to the present invention and do not necessarily constitute prior art.
[0003] Existing log frameworks include commons-log, logback, Log4j, and Log4j2, etc. When adding class names, method names, and line numbers to existing log frameworks, Java exceptions are generated at the source code where the log is printed, and then the generated Java exceptions are captured. The class names, method names, and line numbers of the corresponding source code are obtained by analyzing the information in the Java exception stack. However, generating Java exceptions during runtime will cause serious performance losses. A single Java exception is 10 - 100 times slower than the execution speed of ordinary Java statements. When a large number of logs are printed, it will cause a serious decline in the overall performance of the system, and in severe cases, it may even cause the system to crash.
[0004] Existing Java bytecode editing frameworks include ASM, javaassist, cglib, and bytebudy, etc. Existing bytecode modification frameworks all edit the results after compilation, with complex operations and prone to compatibility problems. Existing bytecode frameworks generally analyze each class line by line during the system startup process and process the bytecode that needs to be edited. Editing bytecode during system startup will seriously slow down the system startup speed when the amount of log printing statements is relatively large, and the delay effect will increase with the increase of the system size, seriously affecting the user experience. When errors occur, startup failures, or compatibility problems occur during the startup process due to bytecode editing, these problems are extremely difficult to reproduce, locate, and analyze. Moreover, since the editing results only remain at the JVM level, it is difficult to obtain the editing results from the JVM and impossible to track the editing results at the version release level. Summary of the Invention
[0005] In order to solve the technical problems existing in the above background art, the present invention provides a method and system for adding identifiers to log printing based on specific scenarios. The present invention dynamically adds information such as line numbers, class names, and method names to the log printing of log panels such as Log4j in the source code file during compilation, making the log printing results more complete, the problems easier to locate and track, and having no runtime side effects.
[0006] To achieve the above object, the present invention adopts the following technical solutions:
[0007] A first aspect of the present invention provides a method for adding identification to log printing based on a specific scenario.
[0008] A method for adding an identifier to log printing based on a specific scenario includes:
[0009] A compiler is used to convert the acquired source code file into a syntax tree, the syntax tree is compiled, and the compiled syntax tree is sent to the annotation processor;
[0010] The annotation processor traverses the compiled syntax tree. When traversing the syntax elements of the class or method, the class information and method information are organized into stack frames and stored in the cache stack; it determines whether the syntax elements contain the log interface of the log panel one by one, and selects the current log printing statement; when it is not determined whether the current log printing level is enabled and / or the input parameters match sensitive characters, it adds log printing syntax elements to wrap the current log printing statement; it obtains the class information and method information of the current log printing statement in the cache stack, and obtains the line number information of the current log printing statement in the syntax tree, and splices the obtained information into the current log printing statement; the stack frame of the processed method or class is popped out of the stack until the syntax tree is traversed.
[0011] Furthermore, the syntax tree is compiled and the compiled syntax tree is sent to the annotation processor; the method includes: the compiler scans the annotation processor in the obtained Jar package and starts the annotation processor; the annotation processor sends the annotation type supported for processing to the compiler, so that the compiler compiles the syntax tree according to the annotation type, and sends the subtree marked by the annotation to the annotation processor for processing.
[0012] Furthermore, the syntax tree includes all syntax elements in the source code file.
[0013] Furthermore, the stack frame includes: class name, package name where the class is located, basic attributes of the class, method name, class to which the method belongs, method input parameters and input parameter types.
[0014] Furthermore, if the input parameter matches sensitive characters, after adding the log printing syntax elements, the sensitive input parameter needs to be desensitized.
[0015] Furthermore, the annotation processor determines whether there are side effects in the current log printing statement, and if so, eliminates the side effects by pruning the syntax tree.
[0016] A second aspect of the present invention provides a system for adding identification to log printing based on specific scenarios.
[0017] A system for adding identification to log printing based on specific scenarios, comprising:
[0018] A compiler unit for converting the obtained source code file into a syntax tree, compiling the syntax tree, and sending the compiled syntax tree to an annotation processor;
[0019] An annotation processor unit for traversing the compiled syntax tree. When traversing the syntax elements of a class or method, organizing the class information and method information into a stack frame and storing it in a cache stack; sequentially determining whether each syntax element contains a log interface of a log panel and selecting the current log printing statement; when it is not determined whether the current log printing level is enabled and / or the input parameter matches sensitive characters, adding a log printing syntax element to wrap the current log printing statement; obtaining the class information and method information where the current log printing statement is located in the cache stack, and obtaining the line number information of the current log printing statement in the syntax tree, and splicing the obtained information into the current log printing statement; popping the stack frame of the processed method or class until the syntax tree is traversed.
[0020] The third aspect of the present invention provides a computer device, which includes:
[0021] A processor suitable for executing a computer program;
[0022] A computer-readable storage medium stores a computer program, and when the computer program is executed by the processor, the steps in the method for adding an identifier for log printing based on a specific scenario described in the first aspect above are implemented.
[0023] The fourth aspect of the present invention provides a computer-readable storage medium, which stores a computer program, and the computer program is suitable for being loaded and executed by a processor to implement the steps in the method for adding an identifier for log printing based on a specific scenario described in the first aspect above.
[0024] The fifth aspect of the present invention provides a computer program product or a computer program.
[0025] The present invention provides a computer program product or a computer program, which includes computer instructions stored in a computer-readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the steps in the method for adding an identifier for log printing based on a specific scenario described in the first aspect above.
[0026] Compared with the prior art, the beneficial effects of the present invention are:
[0027] The present invention provides a method and system for adding identifiers to log printing based on specific scenarios. The compiler is used to convert the obtained source code file into a syntax tree, compile the syntax tree, and send the compiled syntax tree to the annotation processor; the annotation processor traverses the compiled syntax tree. When traversing the syntax elements of a class or method, the class information and method information are organized into a stack frame and stored in the cache stack; it is determined one by one whether the syntax elements contain the log interface of the log panel to select the current log printing statement; when it is not determined whether the current log printing level is enabled and / or the input parameter matches sensitive characters, a log printing syntax element is added to wrap the current log printing statement; the class information and method information where the current log printing statement is located are obtained from the cache stack, and the line number information of the current log printing statement in the syntax tree is obtained, and the obtained information is spliced into the current log printing statement; the stack frame of the processed method or class is popped until the entire syntax tree is traversed. When dynamically appending log printing information (such as class name, method name, line number) during compilation, the present invention does not require intervention by R & D personnel and does not require additional coding overhead.
[0028] The present invention can analyze and identify the syntax tree of the log printing statement in the compiler, modify the log printing statement with secondary overhead, specifically trim and optimize the syntax tree, and generate a better compilation result. Brief Description of the Drawings
[0029] The specification drawings forming a part of the present invention are used to provide a further understanding of the present invention. The schematic embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention.
[0030] Figure 1 is a flowchart of the method for adding identifiers to log printing based on specific scenarios shown in the embodiments of the present invention;
[0031] Figure 2 is a flowchart of adding identifiers to log printing shown in the embodiments of the present invention;
[0032] Figure 3 is a detailed flowchart of adding identifiers to log printing shown in the embodiments of the present invention;
[0033] Figure 4 is a structural diagram of the system for adding identifiers to log printing based on specific scenarios shown in the embodiments of the present invention;
[0034] Figure 5 is a structural diagram of a computer device shown in the embodiments of the present invention. Detailed Embodiments
[0035] The present invention will be further described below in conjunction with the drawings and embodiments.
[0036] It should be noted that the following detailed description is illustrative and intended to provide further explanation of the present invention. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by those of ordinary skill in the technical field to which the present invention pertains.
[0037] It should be noted that the terms used herein are merely for describing specific embodiments and are not intended to limit the exemplary embodiments according to the present invention. As used herein, unless the context clearly indicates otherwise, the singular form is also intended to include the plural form. In addition, it should be understood that when the terms "comprise" and / or "include" are used in this specification, they specify the presence of features, steps, operations, devices, components, and / or combinations thereof.
[0038] To facilitate the understanding of the technical solution of the present invention, some technical terms related to the present invention are introduced below.
[0039] Java annotation is a form of metadata that provides additional information about the program code, but this information does not directly change the running logic of the program. Since annotations were introduced in Java 5, they have become an important feature of the Java programming language. They can be used to mark class, method, variable, parameter, and package declarations, etc. From the perspective of source code editing, an annotation is a label that adds some information to the syntax element being labeled, and the label itself is also part of the information content. When developers use Java annotations, they can add some parameters to the annotations. These parameters must be static and non-modifiable (such as strings, numbers, class names, etc.). By adding parameters, the information carried by the annotations can be further enriched.
[0040] Java Annotation Processor is a plugin for the Java compiler that can read, process, and generate new source files or resource files during the code compilation stage. By using the annotation processor, specific logic can be executed based on annotations during compilation, such as automatically generating code, verifying certain constraints in the project, etc.
[0041] A Jar package (Java Archive) is a file format that packs multiple files into one file. It is usually used to gather many Java class files and related metadata and resources (such as images, property files, etc.) into one file for easy distribution and deployment. Jar package files use.jar as the file extension and are the standard archive file format for Java applications and components.
[0042] Based on this, this embodiment provides a method for adding identifiers to log printing based on a specific scenario, as Figure 1 shown, including:
[0043] Use a compiler to convert the obtained source code file into a syntax tree, compile the syntax tree, and send the compiled syntax tree to the annotation processor;
[0044] The annotation processor traverses the compiled syntax tree. When traversing the syntax elements of a class or method, organize the class information and method information into a stack frame and store it in the cache stack; in the syntax elements, judge one by one whether there is a log interface of the log panel, and select the current log printing statement; when it is not judged whether the current log printing level is enabled and / or the input parameter matches sensitive characters, add a log printing syntax element to wrap the current log printing statement; obtain the class information and method information where the current log printing statement is located in the cache stack, and obtain the line number information of the current log printing statement in the syntax tree, and splice the obtained information into the current log printing statement; pop the stack frame of the processed method or class until the syntax tree is traversed.
[0045] The present invention is applicable to the scenario where enterprise-level Java applications print logs through the Slf4j log panel or common log frameworks. Through Java annotation processor technology, analyze all log printing codes in the existing code during compilation, trim the syntax tree, and actively append the class name and line number of the source code file where the current log is printed to the end of the content to be printed, which is more conducive to tracking and positioning problems.
[0046] The method for adding an identifier to log printing based on a specific scenario described in this embodiment includes: a lexical analysis stage, a syntax analysis stage, a semantic analysis stage, an optimization stage, a bytecode generation stage, and a bytecode verification stage. In the lexical analysis stage and the syntax analysis stage, use a Java compiler to compile the syntax tree; in the semantic analysis stage, call a Java annotation processor for annotation processing; in the optimization stage, avoid side effects, desensitize sensitive input parameters, and trim the syntax tree; in the bytecode generation stage, the Java compiler continues to compile according to the results of the annotation processor and outputs a bytecode file (that is, the file after splicing class, method, line number, etc. information to the log printing statement), and the compilation is completed; in the bytecode verification stage, verify the bytecode file.
[0047] The main body of the present invention is divided into two major parts: an annotation used by developers during project development (hereinafter referred to as @LogModify), and an annotation processor that needs to be relied on in the project (hereinafter referred to as LogAnnoProcessor). The annotation and the annotation processor are encapsulated in a Jar package (hereinafter referred to as log-anno-proc.jar).
[0048] Introduce the dependent Jar package (log-anno-proc.jar) according to the actual package manager used in the project (such as Maven, Gradle).
[0049] Figure 2 It is a flowchart showing the addition of an identifier for log printing in an embodiment of the present invention; Figure 3 It is a detailed flowchart showing the addition of an identifier for log printing in an embodiment of the present invention; as Figure 2 , Figure 3 shown:
[0050] In some possible implementation manners, when the source code file is compiled, the Java compiler will convert the source code file into a Java syntax tree, and the Java syntax tree contains all the syntax elements in the source code;
[0051] After the Java compiler prepares the syntax tree, it will automatically scan the annotation processors in the dependent Jar package (such as log-anno-proc.jar) and start the annotation processor (such as LogAnnoProcessor);
[0052] After the annotation processor is started, it will notify the Java compiler of the annotation types supported by the current annotation processor (such as @LogModify);
[0053] During application, there will be a large number of log printing statements in the source code, but it is not necessary to append information such as class names and method names. Therefore, the @LogModify annotation can be added as needed. This annotation can be added to the package name, class name, and method name to specify the scope to be processed by adding the annotation.
[0054] The Java compiler compiles the Java syntax tree according to the annotation type and passes the subtrees marked by the annotation in the syntax tree to the annotation processor for processing;
[0055] The annotation processor processes the syntax tree. For example: modify the syntax tree, generate resource files according to the syntax tree, and exit after the processing is completed;
[0056] The Java compiler continues to compile according to the result of the annotation processor and outputs the bytecode file to complete the compilation.
[0057] In some possible implementation manners, during the compilation of the source code file, the annotation processor LogAnnoProcessor will automatically retrieve the syntax elements in the syntax tree and process them.
[0058] In some possible implementation manners, the process of the annotation processor processing includes:
[0059] After the annotation processor is started, it receives the annotated syntax tree;
[0060] Traverse the syntax tree, gradually traverse the syntax elements of classes and methods, and extract the syntax elements of Java statements from the methods;
[0061] During the traversal process, whenever a class element or method element is encountered, organize the class information and method information in the above text into a stack frame and cache it in the stack. The stack frame contains information received from the context such as the class name, the package name where the class is located, the method name, and the method input parameters.
[0062] Judge one by one in the statement elements whether they contain the corresponding Slf4J API (or API interfaces of common frameworks such as logback and Log4j2), and judge the currently used logging framework according to the specific classes imported in the source code import. Further analyze and process the expression elements determined to be log prints; Java believes that a line of code statement is composed of one or more expressions, and a method call that completes a log print by calling a logging framework can be regarded as a statement; when the method call of the logging framework is part of a lambda, etc., this method call should be processed separately as an expression, rather than treating the lambda containing the log method call, etc. as the processing unit.
[0063] Analyze whether the current log print statement has side effects. If there are side effects, avoid the side effects; in this embodiment, by modifying the log print methods with side effects in the source code file, a better compilation result is generated.
[0064] When the log print level is not judged to be enabled, add log print syntax elements to wrap the current log print statement;
[0065] For example: currently, it is necessary to print debug-level logs, but in the actual operation of the business, the output of debug-level logs is not necessarily continuously enabled. At this time, if there is a part in the parameters of the log print that needs to be calculated before calling the log print method, side effects will occur; for example: the statement with side effects log.debug(“method params:{}”,JsonUtils.toJson(params)); in this statement, before the actual call of the debug method, JsonUtils.toJson(params) will be calculated first, and the calculation result will be passed to log.debug; if the debug-level log output is not enabled during the business operation, then no log print will be generated finally, and the pre-calculated JsonUtils.toJson(params) will waste CPU and memory resources in vain.
[0066] When the input parameter matches sensitive characters (such as: ciph, password, etc.), add log print syntax elements to wrap the current log print statement and desensitize the sensitive input parameters;
[0067] Get the top element in the cache stack, which is the class and method of the current statement. The syntax tree already contains the line number information of the current statement element. Add the obtained class name, method name, line number and other elements to the log print statement.
[0068] After processing a method, the stack frame of the current method is popped, and after processing a class, the stack frame of the current class is popped;
[0069] After the syntax tree traversal is completed, the context cache information is cleared and the modified syntax tree is returned to the Java compiler; after the annotation processor completes the processing, it exits, and then the Java compiler will continue to optimize the modified syntax tree, generate bytecode, and verify the bytecode.
[0070] In this embodiment, the side effect in the log printing statement means that the developer may introduce side effects in the log output due to negligence during the coding process, for example:
[0071] (1) When printing debug-level (or other level) logs, there is additional calculation logic in the log printing statement, for example, the object is converted into a JSON string and then passed to the log printing API. However, in the context, it is not determined whether the current log level is enabled, resulting in the calculation in the statement being executed even when the corresponding log level does not need to be output, wasting system resources;
[0072] (2) When printing logs, sensitive data such as passwords, ID cards, and tokens are output to the logs, resulting in sensitive information leakage;
[0073] (3) When printing logs, external resources are introduced through JNDI and other methods. If the external resources are not secure, it may lead to privilege escalation vulnerabilities or security vulnerabilities such as arbitrary code execution.
[0074] The present invention aims to solve the problem that when a business or system-level operation and maintenance problem occurs in the system, the log printed by the system lacks specific line number, class name, method name and other information, which causes troubles in tracking, troubleshooting and locating the problem. The present invention provides a method for adding identifiers to log printing based on specific scenarios. By dynamically adding line number, class name, method name and other information to the log panel such as Log4j in the source code file during compilation, the log printing result is more complete and the problem is easier to locate and track.
[0075] According to the prior art, when faced with multiple versions of running instances of the same system, it is necessary to match the running Jar package with the static source code according to the log, confirm the difference between the running Jar package and the static source code, and eliminate the problems such as version differences caused by misoperation during the operation and maintenance process; the present invention analyzes and optimizes log printing statements during compilation, analyzes the syntax tree of the log printing source code with side effects, tailors and optimizes the syntax tree in a targeted manner, outputs better compilation results, improves processing efficiency, and reduces compiler overhead.
[0076] As described above in combination Figure 1 A method for adding an identifier to log printing based on a specific scenario provided by an embodiment of the present invention has been introduced in detail. Next, a system for adding an identifier to log printing based on a specific scenario provided by an embodiment of the present invention will be introduced with reference to the accompanying drawings.
[0077] Figure 4 is a schematic structural diagram of a system for adding an identifier to log printing based on a specific scenario shown in an embodiment of the present invention. Referring to Figure 4 , the system described in the present invention includes:
[0078] A compiler unit for converting the obtained source code file into a syntax tree, compiling the syntax tree, and sending the compiled syntax tree to an annotation processor;
[0079] An annotation processor unit for traversing the compiled syntax tree. When traversing the syntax elements of a class or method, organizing the class information and method information into a stack frame and storing it in a cache stack; judging one by one whether the syntax elements contain the log interface of the log panel and selecting the current log printing statement; when it is not judged whether the current log printing level is enabled and / or the input parameter matches sensitive characters, adding a log printing syntax element to wrap the current log printing statement; obtaining the class information and method information where the current log printing statement is located in the cache stack, and obtaining the line number information of the current log printing statement in the syntax tree, and splicing the obtained information into the current log printing statement; popping the stack frame of the processed method or class until the syntax tree is traversed.
[0080] In some embodiments, the compiler unit is specifically configured to scan the annotation processors in the obtained Jar package and start the annotation processors; so that the annotation processors send the supported annotation types to the compiler, and the compiler compiles the syntax tree according to the annotation types and sends the subtrees marked by the annotations to the annotation processors for processing.
[0081] In some embodiments, all the syntax elements in the source code file are included in the syntax tree.
[0082] In some embodiments, the stack frame includes: class name, package name where the class is located, basic attributes of the class, method name, class to which the method belongs, method input parameters, and input parameter types.
[0083] In some possible implementation manners, the annotation processor unit further includes an interface module, a syntax tree management module, a class listening module, a method listening module, a syntax tree modification module, a side effect elimination syntax tree pruning module, a desensitization syntax tree modification module, and an information module.
[0084] Specifically, the interface module is used to dock with the Java annotation processor interface, provide supported annotation types, receive Java annotation processor parameters, and forward the received syntax tree to the syntax tree management module, class listener module, method listener module, syntax tree modification module, side-effect elimination syntax tree pruning module, and desensitization syntax tree modification module.
[0085] The syntax tree management module is responsible for traversing the syntax tree and executing specific listener modules based on the traversed elements, and has independent listener interfaces for various syntax elements.
[0086] The class listener module is used to trigger the registered class listener instance when a class element is found during the traversal of the syntax tree management module. This instance records relevant information about the current class, such as: class name, package where the class is located, basic attributes of the class (static class, parent class information, etc.).
[0087] The method listener module is used to trigger the registered class listener instance when the syntax tree management module traverses to a class element. This instance records relevant information about the current method, such as: the class it belongs to, method input parameters, input parameter types, etc.
[0088] The syntax tree modification module is used to call the syntax tree modification module according to the order of the configured syntax tree modifiers, and different syntax tree modification module instances implement different modifications.
[0089] The side-effect elimination syntax tree pruning module is used to judge the side effects in the log printing statements and prune and optimize the syntax tree to eliminate the side effects.
[0090] The desensitization syntax tree modification module is used to desensitize sensitive log printing parameters.
[0091] The additional information syntax tree modification module is used to add basic information such as class name, method name, and line number to the log printing statements.
[0092] The information module is used to output logs during the working process of the annotation processor, etc.
[0093] A system for adding identifiers to log printing based on specific scenarios proposed by the present invention has the advantages of low intrusion, low perception, low compilation period overhead, and zero runtime overhead.
[0094] The system for adding identifiers to log printing based on specific scenarios according to the embodiments of the present invention can correspond to executing the methods described in the embodiments of the present invention, and the above and other operations and / or functions of each module of the system for adding identifiers to log printing based on specific scenarios are respectively for implementing Figure 1 the corresponding processes of each method in, and for the sake of brevity, will not be described in detail here.
[0095] See Figure 5The structural diagram of the computer device shown, the computer device includes a processor, a communication interface, and a computer-readable storage medium. Among them, the processor, the communication interface, and the computer-readable storage medium can be connected through a bus or other means. Among them, the communication interface is used to receive and send data. The computer-readable storage medium can be stored in the memory of the computer device, and the computer-readable storage medium is used to store computer programs, and the computer programs include program instructions, and the processor is used to execute the program instructions stored in the computer-readable storage medium. The processor (or CPU (Central Processing Unit, central processing unit)) is the computing core and control core of the computer device, and it is suitable for implementing one or more instructions, specifically suitable for loading and executing one or more instructions to implement the corresponding steps in the method embodiment of adding an identifier to the log printing based on a specific scenario.
[0096] This embodiment provides a computer-readable storage medium (Memory). The computer-readable storage medium is a memory device in the computer device and is used to store programs and data. It can be understood that the computer-readable storage medium here can include both the built-in storage medium in the computer device and, of course, the extended storage medium supported by the computer device. The computer-readable storage medium provides a storage space, and this storage space stores the processing system of the computer device.
[0097] And, one or more instructions suitable for being loaded and executed by the processor are also stored in this storage space. These instructions can be one or more computer programs (including program code). It should be noted that the computer-readable storage medium here can be a high-speed RAM memory or a non-volatile memory, such as at least one disk memory; optionally, it can also be at least one computer-readable storage medium located far from the aforementioned processor.
[0098] In one embodiment, one or more instructions are stored in the computer-readable storage medium; the processor loads and executes one or more instructions stored in the computer-readable storage medium to implement the corresponding steps in the method embodiment of adding an identifier to the log printing based on a specific scenario.
[0099] This embodiment provides a computer program product or a computer program. The computer program product or the computer program includes computer instructions, and the computer instructions are stored in the computer-readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the corresponding steps in the method embodiment of adding an identifier to the log printing based on a specific scenario.
[0100] Those skilled in the art should understand that the embodiments of the present invention can be provided as a method, a system, or a computer program product. Therefore, the present invention can take the form of a hardware embodiment, a 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 and optical memories, etc.) that contain computer-usable program code.
[0101] The present invention is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to embodiments of the present invention. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, and the combination of flows and / or blocks in the flowcharts and / or block diagrams, can be implemented 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, such that the instructions executed by the processor of the computer or other programmable data processing devices generate means for implementing the functions specified in Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.
[0102] 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, such that the instructions stored in the computer-readable memory generate a manufactured article including instruction means that implement the functions specified in Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.
[0103] These computer program instructions can also be loaded onto a computer or other programmable data processing device, such 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 Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.
[0104] Those of ordinary skill in the art can understand that to implement all or part of the processes in the above-described embodiment methods, it can be completed by instructing relevant hardware through a computer program. The program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the above-described method embodiments. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only memory (ROM), or a random access memory (RAM), etc.
[0105] The above are only the preferred embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention may have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included within the protection scope of the present invention.
Claims
1. A method for adding an identifier to log printing based on a specific scenario, characterized in that, Including: Using a compiler to convert the obtained source code file into a syntax tree, compile the syntax tree, and send the compiled syntax tree to an annotation processor; The annotation processor traverses the compiled syntax tree. When traversing the syntax elements of a class or method, it organizes the class information and method information into a stack frame and stores it in a cache stack; Judging one by one in the syntax elements whether there is a log interface of the log panel, and selecting the current log printing statement; when it is not judged whether the current log printing level is enabled and / or the input parameter matches sensitive characters, adding a log printing syntax element to wrap the current log printing statement; obtaining the class information and method information where the current log printing statement is located in the cache stack, and obtaining the line number information of the current log printing statement in the syntax tree, and splicing the obtained information into the current log printing statement; Popping the stack frame of the processed method or class until the syntax tree is traversed.
2. The method for adding an identifier to log printing based on a specific scenario according to claim 1, wherein The method of compiling the syntax tree and sending the compiled syntax tree to the annotation processor includes: the compiler scans the annotation processors in the obtained Jar package and starts the annotation processor; the annotation processor sends the supported annotation types to the compiler, so that the compiler compiles the syntax tree according to the annotation types and sends the subtrees marked by the annotations to the annotation processor for processing.
3. The method for adding an identifier to log printing based on a specific scenario according to claim 1 or 2, characterized in that, All syntax elements in the source code file are included in the syntax tree.
4. The method for adding an identifier to log printing based on a specific scenario according to claim 1, characterized in that, The stack frame includes: class name, package name where the class is located, basic attributes of the class, method name, class to which the method belongs, method input parameters, and input parameter types.
5. The method for adding an identifier to log printing based on a specific scenario according to claim 1, wherein, When the input parameter matches sensitive characters, after adding the log printing syntax element, it is also necessary to desensitize the sensitive input parameter.
6. The method for adding an identifier to log printing based on a specific scenario according to claim 1, wherein The annotation processor judges whether there is a side effect in the current log printing statement. If so, the side effect is eliminated by trimming the syntax tree.
7. A system for adding identifiers to log prints based on specific scenarios, characterized in that, Including: A compiler unit for converting the obtained source code file into a syntax tree, compiling the syntax tree, and sending the compiled syntax tree to an annotation processor; An annotation processor unit for traversing the compiled syntax tree. When traversing the syntax elements of a class or method, it organizes the class information and method information into a stack frame and stores it in a cache stack; Judging one by one in the syntax elements whether there is a log interface of the log panel, and selecting the current log printing statement; when it is not judged whether the current log printing level is enabled and / or the input parameter matches sensitive characters, adding a log printing syntax element to wrap the current log printing statement; obtaining the class information and method information where the current log printing statement is located in the cache stack, and obtaining the line number information of the current log printing statement in the syntax tree, and splicing the obtained information into the current log printing statement; Popping the stack frame of the processed method or class until the syntax tree is traversed.
8. A computer device, characterized in that A processor suitable for executing a computer program; A computer-readable storage medium, in which a computer program is stored. When the computer program is executed by the processor, the steps in the method for adding an identifier for log printing based on a specific scenario according to any one of claims 1-6 are implemented.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, and the computer program is adapted to be loaded and executed by a processor to perform the steps in the method for adding an identifier to log printing based on a specific scenario according to any one of claims 1-6.
10. A computer program product, characterized in that, The computer program product includes a computer program, and when the computer program is executed by a processor, it implements the steps in the method for adding an identifier to log printing based on a specific scenario according to any one of claims 1-6.