Log output method and device

By generating and writing the log output code of the abstract syntax tree when compiling the source code, the problem of log output automation in the existing technology is solved, development efficiency and system performance are improved, and standardized and refined log output is achieved.

CN113760291BActive Publication Date: 2024-10-18BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202010973017.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-09-16
Publication Date
2024-10-18
Estimated Expiration
2040-09-16

AI Technical Summary

Technical Problem

The existing log output method cannot automatically perform logging, resulting in low efficiency. Developers need to manually add logging code, which requires a heavy workload. Moreover, the log content lacks a unified standard and is difficult to customize.

Method used

By detecting whether the source code has been compiled, the abstract syntax tree is obtained, the log output code is generated based on the log annotation information, and the corresponding location is written in the abstract syntax tree to achieve automatic log output.

Benefits of technology

It improves development efficiency, realizes automated log output, reduces the impact on system operating performance, provides standardized and refined log output, and avoids the problem of incomplete log information.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113760291B_ABST
    Figure CN113760291B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and device for log output, and relates to the field of computer technology. A specific implementation of the log output method includes: if it is detected that the source code is compiled, then an abstract syntax tree obtained by parsing the source code is obtained; if there is log annotation information in the source code, then a log output code is generated according to the log annotation information, and a target position in the abstract syntax tree where the log output code is to be written is determined; according to the target position, the log output code is written into the abstract syntax tree to generate a first abstract syntax tree, and log output is performed through the first abstract syntax tree. This implementation can automatically perform log output and improve development efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a log output method and device. Background Art

[0002] In the process of system development, logging and output are an indispensable part. After the system is launched, developers and operation and maintenance personnel can understand the actual operation status of the line through logs. In addition, when there is a problem with the system, the system's operating data can be restored through logs, thereby helping developers to repair the system faster and more accurately. In the prior art, logging can be performed in the following ways: 1) Developers manually add logging code to record and output logs; 2) AOP (Aspect Oriented Programming) is used to uniformly add logging code before and after the method for logging and output. However, the above two logging and output methods cannot automatically log, and the output efficiency is low. Summary of the invention

[0003] In view of this, an embodiment of the present invention provides a method and device for log output, which can solve the problem that the existing log output method cannot automatically record logs.

[0004] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for log output is provided.

[0005] The log output method of the embodiment of the present invention includes:

[0006] If it is detected that the source code is compiled, obtaining an abstract syntax tree obtained by parsing the source code;

[0007] If log annotation information exists in the source code, generating log output code according to the log annotation information, and determining a target position in the abstract syntax tree where the log output code is to be written;

[0008] According to the target location, the log output code is written into the abstract syntax tree to generate a first abstract syntax tree, and log output is performed through the first abstract syntax tree.

[0009] Optionally, if it is detected that the source code is compiled, the step of parsing an abstract syntax tree from the source code includes:

[0010] If it is detected that the source code is compiled, a syntax analysis is performed on the source code to obtain a syntax analysis result of the source code;

[0011] The source code is structurally transformed according to the syntax analysis result to obtain an abstract syntax tree.

[0012] Optionally, if there is log annotation information in the source code, the step of generating log output code according to the log annotation information includes:

[0013] If the log annotation information exists in the source code, the log output code is generated according to the generation parameters of the log output code included in the log annotation information.

[0014] Optionally, the generation parameters of the log output code include one or more of the following: log record prefix information, blacklist configuration, whitelist configuration and log output level; wherein the whitelist configuration includes: parameter names and / or attribute values ​​of logs to be collected; the blacklist configuration includes: parameter names to be excluded and / or attribute values ​​to be excluded in log collection.

[0015] Optionally, the step of generating the log output code according to the generation parameters of the log output code included in the log annotation information includes:

[0016] Determine whether there is a whitelist configuration in the generation parameters of the log output code;

[0017] If yes, generating the log output code according to the parameter name and / or attribute value of the log to be collected included in the whitelist configuration;

[0018] Otherwise, determine whether there is a blacklist configuration in the generation parameters of the log output code;

[0019] If there is a blacklist configuration in the generation parameters of the log output code, the parameter name and / or attribute value of the log to be collected is determined according to the blacklist configuration, and the log output code is generated according to the determined parameter name and / or attribute value.

[0020] Optionally, the step of outputting logs through the first abstract syntax tree includes:

[0021] The first abstract syntax tree is converted into bytecode, and log output is performed by executing the bytecode.

[0022] Optionally, the log output code includes an input parameter log code and an output parameter log code, and the step of determining a target location in the abstract syntax tree where the log output code is to be written includes:

[0023] Obtain the method input parameter position and method output parameter position of the code block containing log annotation information in the abstract syntax tree;

[0024] The position before the method input parameter position of the code block is determined as the target position for the input parameter log code to be written into the abstract syntax tree, and the position after the method output parameter position of the code block is determined as the target position for the output parameter log code to be written into the abstract syntax tree.

[0025] To achieve the above objective, according to another aspect of an embodiment of the present invention, a log output device is provided.

[0026] The log output device of the embodiment of the present invention includes:

[0027] An acquisition module, configured to acquire an abstract syntax tree obtained by parsing the source code if it is detected that the source code is compiled;

[0028] A determination module, configured to generate a log output code according to the log annotation information if there is log annotation information in the source code, and determine a target position in the abstract syntax tree where the log output code is to be written;

[0029] The insertion module is used to write the log output code into the abstract syntax tree to generate a first abstract syntax tree according to the target position, and perform log output through the first abstract syntax tree.

[0030] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device is provided.

[0031] The electronic device according to the embodiment of the present invention comprises:

[0032] one or more processors;

[0033] a storage device for storing one or more programs,

[0034] When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described above.

[0035] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer readable medium is provided.

[0036] The computer-readable medium of the embodiment of the present invention stores a computer program, and when the program is executed by a processor, the method described above is implemented.

[0037] One embodiment of the above invention has the following advantages or beneficial effects:

[0038] In an embodiment of the present invention, by modifying the abstract syntax tree when compiling the source code, the corresponding log output code is automatically added to the abstract syntax tree. The above method can realize automatic log output and improve development efficiency.

[0039] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0041] Figure 1 is a flow chart of a method for log output according to a first embodiment of the present invention;

[0042] Figure 2 is a schematic diagram of a parsing process of an abstract syntax tree according to an embodiment of the present invention;

[0043] Figure 3 is a flow chart of a log output method according to a second embodiment of the present invention;

[0044] Figure 4 is a flow chart of a method for log output according to a third embodiment of the present invention;

[0045] Figure 5 It is a module schematic diagram of a log output device according to an embodiment of the present invention;

[0046] Figure 6 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0047] Figure 7 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0048] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0049] Generally speaking, different developers have different opinions on when to log and whether the log content is complete. In addition, too much logging will affect the system performance, while too little logging will not meet the needs. Therefore, proper and complete logging and output play an extremely important role in system development and debugging and system online maintenance.

[0050] The existing logging method has the following problems:

[0051] 1) Developers manually add logging code, which is a lot of work and affects development efficiency. In addition, it is very arbitrary, there is no unified standard for log content, and it is impossible to find the log accurately when using it, and you need to find the source code;

[0052] 2) There is a problem of too much or too little log content;

[0053] 3) Only fixed log description information can be added, which is difficult to customize, cannot adapt to specific business scenarios, and cannot be customized;

[0054] 4) Log records are not standardized, the log content output is too much, and it is impossible to record in a targeted manner;

[0055] 5) Perform section interception before and after the access method at runtime, affecting the running performance.

[0056] In order to solve the problems existing in the existing log recording and output methods, an embodiment of the present invention provides a log output method. Figure 1 FIG. 1 is a flow chart of a method for outputting logs according to a first embodiment of the present invention. Figure 1 As shown, the method may include the following steps S101 to S103.

[0057] Step S101: if it is detected that the source code is compiled, an abstract syntax tree obtained by parsing the source code is obtained.

[0058] The source code can be a code file written in any language. For example, if the source code is a Java file, there is a syntax analyzer in the Java language, which reads and parses the source code as a string and creates an abstract syntax tree, which is the necessary preliminary work for a program to complete compilation.

[0059] Before obtaining the abstract syntax tree obtained by parsing the source code in step S101, if it is detected that the source code is compiled, the abstract syntax tree is parsed from the source code. In order to parse the abstract syntax tree from the source code, the source code is first subjected to syntax analysis to obtain the syntax analysis result of the source code. A syntax analyzer can be used to perform syntax analysis on the source code. Then, the source code is structurally transformed according to the syntax analysis result to obtain an abstract syntax tree. It can be understood that each assignment statement in the source code can be parsed into a regular syntax tree through the syntax analysis method, and then the abstract syntax tree of the source code is formed by the regular syntax tree.

[0060] like Figure 2 As shown, Figure 2 Schematic diagram of the parsing process of the abstract syntax tree of the embodiment of the present invention, which shows the detailed process of parsing an assignment statement into a regular syntax tree through syntax analysis.

[0061] Step S102: if there is log annotation information in the source code, a log output code is generated according to the log annotation information, and a target position in the abstract syntax tree where the log output code is to be written is determined.

[0062] Add log annotation information to the method or class that needs to record logs. When the source code is compiled, the log output code will be automatically added to the bytecode for the method with this log annotation information. The annotation code is as follows:

[0063]

[0064]

[0065] According to the technical solution of the embodiment of the present invention, when generating the log output code, the log output code can be specifically generated according to the generation parameters of the log output code included in the log annotation information. In step S102, the generation parameters of the log output code include one or more of the following: log record prefix information, blacklist configuration, whitelist configuration and log output level. Among them, the whitelist configuration includes: parameter names and / or attribute values ​​of the logs to be collected; the blacklist configuration includes: parameter names and / or attribute values ​​to be excluded in log collection. The log record prefix information is used to define the prefix information of the log, and the log record prefix information has a default value and can also be defined by the user. The exclusion parameter name is mainly used when the input parameter contains multiple parameters, and the specified parameter can be logged without logging, which can improve the system log performance and reduce the log output volume. Excluding the specified attribute value is used to indicate that the specified attribute value in the parameter object is excluded and is not output to the log, so that some useless log information can be excluded, thereby improving system performance and reducing the log volume. If there are the same attribute names in multiple input parameter objects, they will all be excluded. If the whitelist configuration is specified, only the corresponding attribute information will be output. In addition, different output levels can be set for different log types, and the log annotation information type includes one or more of the following types: error, warning, off, default, debug, etc. For example, the log annotation information type can be set to the highest level for vulnerability repair, so that developers can understand the process of system repair. At the same time, it can be determined whether to generate log output code for log annotation information according to the log output level to control the log output process.

[0066] Generally, all parameters can be set for logging or some parameters can be set for logging. In order to improve the system log performance and reduce the log output volume, the specified parameters that do not generate logs can be pre-set. If there is a log annotation in the source code and there are specified parameters in the log annotation, the log output code corresponding to the specified parameters in the log annotation is not generated. It is understandable that if the input parameter contains multiple parameters, the specified parameters may not be logged.

[0067] Since the source code includes at least one code block, when searching whether there is log annotation information in the abstract syntax tree, the search can be performed in units of code blocks, and then log input codes are generated respectively according to the log annotation information of each code block.

[0068] Among them, when the log output code is generated according to the generation parameters of the log output code, how to generate the log output code can be determined according to the specific content of the generation parameters. It should be noted that, under normal circumstances, the whitelist configuration and the blacklist configuration cannot be configured and function at the same time. Therefore, when generating the log output code, the process is roughly as follows: determine whether there is a whitelist configuration in the generation parameters of the log output code; if so, generate the log output code according to the parameter name and / or attribute value of the log to be collected included in the whitelist configuration; otherwise, determine whether there is a blacklist configuration in the generation parameters of the log output code; if there is a blacklist configuration in the generation parameters of the log output code, determine the parameter name and / or attribute value of the log to be collected according to the blacklist configuration, and generate the log output code according to the determined parameter name and / or attribute value.

[0069] Before generating the log output code according to the log annotation information in step S102, you can add @AutoLog annotation before the method to which the log needs to be added, and configure the corresponding attributes according to the actual situation, or you can use the default configuration without configuration. In one embodiment of the present invention, for example: before outputting the log, add the prefix "gateway queryGroupStock" content, and set the attribute information includeParams="locTag" to generate the log output code related to the log annotation information, see the following code example:

[0070] @AutoLog(

[0071] logPreMessage="gateway queryGroupStock",

[0072] includeParams="locTag" )

[0074] public List<wmsstockcollectlocdto>queryGroupStock(WmsLocStockQueryParamDTO wmsLocStockQueryParamDTO){

[0075] / / Business logic omitted

[0076] return listWmsStockCollectDTO;

[0077] }.

[0078] After the source code is compiled, the decompiled content of the generated Class class file is as follows:

[0079] @AutoLog(

[0080] logPreMessage="gateway queryGroupStock",

[0081] includeParams="locTag" )

[0083] public List <wmsstockcollectlocdto>queryGroupStock(WmsLocStockQueryParamDTO wmsLocStockQueryParamDTO){

[0084] log.info("gateway queryGroupStock in:wmsStkQueryParamDTO:{}",wmsLocStockQueryParamDTO.getLocTag());

[0085] / / Business logic omitted

[0086] log.info("gateway queryGroupStock out:listWmsStockCollectDTO:{}",JSON.toJSONString(listWmsStockCollectDTO));

[0087] return listWmsStockCollectDTO;

[0088] }.

[0089] It is understandable that the @AutoLog annotation does not require manual writing of log.info related log codes, and the decompiled code of the compiled class file will have the log.info related information before and after.

[0090] At the same time, the log annotation information can also be used to indicate the insertion position of the log output code. When it is necessary to record logs, log annotations will be made before the code block where the logs are to be recorded and output. Generally, the recorded and output logs include at least: input parameter logs and output parameter logs. In order to determine the target position of the log output code in the abstract syntax tree, the method input parameter position and method output parameter position of the code block in the abstract syntax tree where the log annotation information exists can be first obtained. Then, the position before the method input parameter position of the code block is determined as the target position in the abstract syntax tree where the input parameter log code is to be written, and the position after the method output parameter position of the code block is determined as the target position in the abstract syntax tree where the output parameter log code is to be written, so that logs can be inserted for the input parameters and output parameters respectively.

[0091] It should be noted that the line before the method input parameter position of the code block can be understood as the previous line of code corresponding to the method input parameter position, and the line after the method output parameter position of the code block can be understood as the next line of code corresponding to the method output parameter position.

[0092] Step S103: According to the target location, the log output code is written into the abstract syntax tree to generate a first abstract syntax tree, and log output is performed through the first abstract syntax tree.

[0093] After step S103, in order to facilitate machine code execution, the first abstract syntax tree may be converted into bytecodes, and log output may be performed by executing the bytecodes.

[0094] In an embodiment of the present invention, by modifying the abstract syntax tree when compiling the source code, the corresponding log output code (or log printing code) is automatically added to the abstract syntax tree. The above method can realize automatic log output and improve development efficiency.

[0095] Figure 3 FIG. 1 is a flow chart of a log output method according to a second embodiment of the present invention. Figure 3 As shown, the method may include the following steps S301 to S308.

[0096] Step S301: If it is detected that the source code is compiled, the source code is obtained.

[0097] In step S301, the source code may be a code file written in any language, such as Java (Java is an object-oriented programming language), but is not limited thereto.

[0098] Step S302: parse the source code to obtain an abstract syntax tree.

[0099] In step S302, when the developer compiles the source code, an abstract syntax tree is parsed from the source code. The parsing process is roughly as follows: first, the source code is parsed to obtain a syntax analysis result of the source code; then, the source code is structurally transformed according to the syntax analysis result to obtain an abstract syntax tree.

[0100] Step S303: Determine whether there is log annotation information in the source code. When the source code is compiled, the annotation processor will be automatically triggered. The annotation processor will determine whether there is log annotation information in the source code and perform corresponding processing according to the log annotation information. If yes, execute step S304; otherwise, repeat step S301.

[0101] The log annotation information includes generation parameters for generating log output code, and the location where the log output code is to be written into the abstract syntax tree can be determined according to the log annotation information. In step S303, the generation parameters of the log output code include one or more of the following: log record prefix information, blacklist configuration, whitelist configuration and log output level. Among them, the whitelist configuration includes: parameter names and / or attribute values ​​of the logs to be collected; the blacklist configuration includes: parameter names and / or attribute values ​​to be excluded in log collection. The log record prefix information is used to define the prefix information of the log, and the log record prefix information has a default value and can also be defined by the user. The exclusion parameter name is mainly used when the input parameter contains multiple parameters, and the specified parameter can be logged without logging, which can improve the system log performance and reduce the log output volume. The attribute value to be excluded is used to indicate that the specified attribute value in the exclusion parameter object is not output to the log, so that some useless log information can be excluded, thereby improving system performance and reducing the log volume. If there are the same attribute names in multiple input parameter objects, they will all be excluded. If the whitelist configuration is specified, only the corresponding attribute information will be output. Different log types can be set to different output levels, and the log annotation information types include one or more of the following: error, warning, off, default, debug, etc. For example, the log annotation information type can be set to the highest level for vulnerability repair, so that developers can understand the system repair process. At the same time, it can be determined whether to generate log output code from the log annotation information according to the log output level to control the log output process.

[0102] Step S304: Read the abstract syntax tree of the source code.

[0103] In step S304, the abstract syntax tree of the current class is read, and then step S305 is executed.

[0104] Step S305: Generate a log output code according to the log annotation information.

[0105] In step S305, the log annotation information includes generation parameters of the log output code. In order to generate the log output code, the log output code can be generated according to the generation parameters of the log output code included in the log annotation information. Since the source code includes at least one code block, when searching whether there is log annotation information in the abstract syntax tree, the search can be performed in units of code blocks, and then log input codes are generated respectively according to the log annotation information of each code block.

[0106] Among them, when generating the log output code according to the generation parameters of the log output code, how to generate the log output code can be determined according to the specific content of the generation parameters. It should be noted that, under normal circumstances, the whitelist configuration and the blacklist configuration cannot work at the same time. Therefore, when generating the log output code, the process is roughly as follows: determine whether there is a whitelist configuration in the generation parameters of the log output code; if so, generate the log output code according to the parameter name and / or attribute value of the log to be collected included in the whitelist configuration; otherwise, determine whether there is a blacklist configuration in the generation parameters of the log output code; if there is a blacklist configuration in the generation parameters of the log output code, determine the parameter name and / or attribute value of the log to be collected according to the blacklist configuration, and generate the log output code according to the determined parameter name and / or attribute value. Then determine the parameter name and / or attribute value of the log to be collected according to the blacklist configuration, and generate the log output code according to the determined parameter name and / or attribute value.

[0107] Generally, all parameters can be set for logging or some parameters can be set for logging. In order to improve the system log performance and reduce the log output volume, the parameter names and / or attribute values ​​to be excluded in the log collection can be pre-set in the blacklist, and the corresponding logs are not generated. It is understandable that if there is a log annotation in the source code and the parameter names and / or attribute values ​​to be excluded exist in the log annotation, then these parameter names and / or attribute values ​​to be excluded are deleted from the input parameters and / or input parameter attributes of the method to be collected logs, and the parameter names and / or attribute values ​​obtained after deletion are used as the input parameters and / or input parameter attributes of the method to be collected logs for generating log output code. By executing the log output code, it can be achieved that the parameter names and / or attribute values ​​to be excluded are not logged.

[0108] Step S306: determine the target position in the abstract syntax tree where the log output code is to be written, and write the log output code into the abstract syntax tree according to the target position to generate a first abstract syntax tree, and perform log output through the first abstract syntax tree.

[0109] In step S306, the log annotation information can also be used to indicate the insertion position of the log output code. Generally, the log to be output includes at least: an input parameter log and an output parameter log. In order to determine the target position where the log output code is written into the abstract syntax tree, the method input parameter position and the method output parameter position of the code block in the abstract syntax tree where the log annotation information exists can be first obtained. Then, the position before the method input parameter position of the code block is determined as the target position where the input parameter log code is to be written into the abstract syntax tree, and the position after the method output parameter position of the code block is determined as the target position where the output parameter log code is to be written into the abstract syntax tree. In this way, the log output code can be inserted for the input parameters and output parameters respectively.

[0110] It should be noted that the line before the method input parameter position of the code block can be understood as the previous line of code corresponding to the method input parameter position, and the line after the method output parameter position of the code block can be understood as the next line of code corresponding to the method output parameter position.

[0111] Before step S306, attribute information on the custom annotation may be obtained, such as the log output level, parameter names and / or attribute values ​​to be excluded in log collection.

[0112] Step S307: Determine whether the first abstract syntax tree meets the preset specification. If yes, execute step S308; otherwise, repeat step S306.

[0113] In step S307, the preset specification is related to the type of the source code. For example, if the source code is a Java source code, the preset specification may be a Java specification, that is, Javac will automatically detect whether the abstract syntax tree generated in step S306 complies with the Java specification.

[0114] Step S308: convert the first abstract syntax tree into bytecode, and perform log output by executing the bytecode.

[0115] In step S308, bytecode is generated from the first abstract syntax tree containing the log output code to facilitate machine execution.

[0116] In the embodiment of the present invention, through step S301 to step S308, the log code can be automatically generated and written into the abstract syntax tree. This implementation has the advantages of being convenient, fast, standard and accurate.

[0117] In an embodiment of the present invention, Java annotation processor technology is used to automatically add corresponding log output code to the abstract syntax tree by modifying the abstract syntax tree when compiling the source code. The above method can realize automatic log output and improve development efficiency.

[0118] In the embodiment of the present invention, when the source code is detected to be compiled, the log output code is automatically added to the rule syntax tree by modifying the rule syntax tree, which is equivalent to automatically adding the log code. Compared with other AOP methods, this implementation belongs to the editor level, has a more advanced timing and higher efficiency.

[0119] It can be understood that when the source code is compiled, the log annotation AutoLog in the code is identified, and the corresponding log output code is added before and after the method annotated with this type, so as to automatically add the log output code to the bytecode.

[0120] Figure 4 FIG. 1 is a flow chart of a method for outputting logs according to a third embodiment of the present invention. Figure 4 As shown, the method may include the following steps S401 to S412.

[0121] Step S401: If it is detected that the source code is compiled, the source code is subjected to syntax analysis to obtain a syntax analysis result of the source code.

[0122] In step S401, the source code can be a code file written in any language. For example, if the source code is a Java file, there is a syntax analyzer in the Java language, which reads and parses the source code as a string and creates an abstract syntax tree, which is the necessary preliminary work for a program to complete compilation.

[0123] Step S402: Performing structural transformation on the source code according to the syntax analysis result to obtain an abstract syntax tree.

[0124] Step S403: If there is log annotation information in the source code, then search for code blocks with log annotation identifiers in the abstract syntax tree to obtain log annotation information for each code block. Step S404 or step S405 is executed after step S403.

[0125] Step S404: Determine whether there is a whitelist configuration in the generation parameters of the log output code; if so, execute step S405; otherwise, execute step S406.

[0126] Step S405: Generate the log output code according to the parameter name and / or attribute value of the log to be collected included in the whitelist configuration. Then execute step S408.

[0127] In step S405, when a code block has multiple log annotations, in order to generate a log output code, the log annotation information of each code block may be obtained, and the log output code may be generated respectively according to the obtained log annotation information.

[0128] In order to generate the log output code, the log output code can be generated according to the generation parameters of the log output code included in the log annotation information. Since the source code includes at least one code block, when searching whether there is log annotation information in the abstract syntax tree, the code block can be used as a unit for searching, and then the log input code can be generated respectively according to the log annotation information of each code block.

[0129] Step S406: Determine whether there is a blacklist configuration in the generation parameters of the log output code. If yes, execute step S407; otherwise, end the process.

[0130] Step S407: Determine the parameter name and / or attribute value of the log to be collected according to the blacklist configuration, and generate a log output code according to the determined parameter name and / or attribute value. Then execute step S408.

[0131] In step S407, when a code block has multiple log annotations, in order to generate a log output code, the log annotation information of each code block may be obtained, and the log output code may be generated respectively according to the obtained log annotation information.

[0132] You can get the log annotation information on the custom annotation, such as the log output level (or log printing level), the parameter name and / or attribute value to be excluded.

[0133] Step S408: Read the abstract syntax tree of the source code.

[0134] Step S409: Determine the target position of the log output code in the abstract syntax tree.

[0135] In step S409, the log annotation information can also be used to indicate the insertion position of the log output code. Generally, the log annotation includes at least: input parameter log and output parameter log. In order to determine the target position, the method input parameter position and method output parameter position of the code block in the abstract syntax tree where the log annotation information exists can be first obtained. Then, the position before the method input parameter position of the code block is determined as the target position where the input parameter log code is to be written into the abstract syntax tree, and the position after the method output parameter position of the code block is determined as the target position where the output parameter log code is to be written into the abstract syntax tree. In this way, the log output code can be inserted for the input parameter and output parameter of the method to be collected.

[0136] It should be noted that the code block before the method input position can be understood as the previous line of code corresponding to the input position, and the code block after the method output position can be understood as the next line of code corresponding to the method output position. Step S410: Write the log output code to the target position corresponding to the abstract syntax tree to form a first abstract syntax tree.

[0137] Step S411: Determine whether the first abstract syntax tree meets the preset specification. If yes, execute step S412; otherwise, repeat step S410.

[0138] Step S412: Generate bytecode according to the first abstract syntax tree.

[0139] The log output method of the embodiment of the present invention has the following advantages:

[0140] 1) It can improve development efficiency. Developers don't need to pay too much attention to how to output log content, such as: inventory query method A, input parameter = X, output parameter = Y. Just configure it through annotations. You can even configure all methods on the class.

[0141] 2) It can improve system performance. Not using global aspects can reduce the access level and improve system performance.

[0142] 3) Standardized output can be achieved. Logs can be output in a standard format to facilitate later log searches.

[0143] 4) It can achieve refined output and avoid the problem of incomplete log information caused by insufficient experience of developers.

[0144] Figure 5 is a module diagram of a log output device according to an embodiment of the present invention, see Figure 5 , the log output device 500 includes:

[0145] The acquisition module 501 is used to acquire an abstract syntax tree obtained by parsing the source code if it is detected that the source code is compiled;

[0146] A determination module 502 is used to generate a log output code according to the log annotation information if there is log annotation information in the source code, and determine a target position in the abstract syntax tree where the log output code is to be written;

[0147] The inserting module 503 is used to write the log output code into the abstract syntax tree according to the target position to generate a first abstract syntax tree, and perform log output through the first abstract syntax tree.

[0148] Optionally, the log output device 500 further includes:

[0149] An analysis module, configured to perform syntax analysis on the source code to obtain a syntax analysis result of the source code if it is detected that the source code is compiled;

[0150] The conversion module is used to perform structural conversion on the source code according to the syntax analysis result to obtain an abstract syntax tree.

[0151] Optionally, the determining module 502 is further configured to:

[0152] If the log annotation information exists in the source code, the log output code is generated according to the generation parameters of the log output code included in the log annotation information.

[0153] Optionally, the inserting module 503 is further used for:

[0154] The first abstract syntax tree is converted into bytecode, and log output is performed by executing the bytecode.

[0155] Optionally, the generation parameters of the log output code include one or more of the following: log record prefix information, blacklist configuration, whitelist configuration and log output level; wherein the whitelist configuration includes: parameter names and / or attribute values ​​of logs to be collected; the blacklist configuration includes: parameter names to be excluded and / or attribute values ​​to be excluded in log collection.

[0156] Optionally, the determining module 502 is further configured to:

[0157] Determine whether there is a whitelist configuration in the generation parameters of the log output code;

[0158] If yes, generating the log output code according to the parameter name and / or attribute value of the log to be collected included in the whitelist configuration;

[0159] Otherwise, determine whether there is a blacklist configuration in the generation parameters of the log output code;

[0160] If there is a blacklist configuration in the generation parameters of the log output code, the parameter name and / or attribute value of the log to be collected is determined according to the blacklist configuration, and the log output code is generated according to the determined parameter name and / or attribute value.

[0161] Optionally, the inserting module 503 is further used for:

[0162] Obtain the method input parameter position and method output parameter position of the code block containing log annotation information in the abstract syntax tree;

[0163] The position before the method input parameter position of the code block is determined as the target position for the input parameter log code to be written into the abstract syntax tree, and the position after the method output parameter position of the code block is determined as the target position for the output parameter log code to be written into the abstract syntax tree.

[0164] In an embodiment of the present invention, by modifying the abstract syntax tree when compiling the source code, the corresponding log output code (or log printing code) is automatically added to the abstract syntax tree. The above method can realize automatic log output and improve development efficiency.

[0165] Figure 6 An exemplary system architecture 600 is shown to which the method or apparatus for log output according to an embodiment of the present invention may be applied.

[0166] like Figure 6 As shown, system architecture 600 may include terminal devices 601, 602, 603, network 604 and server 605. Network 604 is used to provide a medium for communication links between terminal devices 601, 602, 603 and server 605. Network 604 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.

[0167] Users can use terminal devices 601, 602, 603 to interact with server 605 through network 604 to receive or send messages, etc.

[0168] The terminal devices 601 , 602 , and 603 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers, etc.

[0169] It should be noted that the log output method provided in the embodiment of the present invention is generally executed by the server 605 , and accordingly, the log output device is generally set in the server 605 .

[0170] It should be understood that Figure 6 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.

[0171] Reference below Figure 7 , which shows a schematic diagram of the structure of a computer system 700 of a terminal device suitable for implementing an embodiment of the present invention. Figure 7 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0172] like Figure 7 As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage part 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, the ROM 702, and the RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.

[0173] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, a mouse, etc.; an output section 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, a modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 710 as needed, so that a computer program read therefrom is installed into the storage section 708 as needed.

[0174] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 709, and / or installed from the removable medium 711. When the computer program is executed by the central processing unit (CPU) 701, the above-mentioned functions defined in the system of the present invention are executed.

[0175] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0176] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0177] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: if it is detected that the source code is compiled, then an abstract syntax tree obtained by parsing the source code is obtained; if there is log annotation information in the source code, then a log output code is generated according to the log annotation information, and a target position in the abstract syntax tree where the log output code is to be written is determined; according to the target position, the log output code is written into the abstract syntax tree to generate a first abstract syntax tree, and log output is performed through the first abstract syntax tree.

[0178] According to the technical solution of the embodiment of the present invention, automatic log output can be achieved, which can improve development efficiency.

[0179] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.< / wmsstockcollectlocdto> < / wmsstockcollectlocdto>

Claims

1. A log output method, characterized in that: include: If it is detected that the source code is compiled, obtaining an abstract syntax tree obtained by parsing the source code; If log annotation information exists in the source code, generating log output code according to the log annotation information, and determining a target position in the abstract syntax tree where the log output code is to be written; According to the target location, writing the log output code into the abstract syntax tree to generate a first abstract syntax tree, and performing log output through the first abstract syntax tree; The log output code includes an input parameter log code and an output parameter log code, and the step of determining a target position in the abstract syntax tree where the log output code is to be written includes: obtaining a method input parameter position and a method output parameter position of a code block in the abstract syntax tree where log annotation information exists; The position before the method input parameter position of the code block is determined as the target position for the input parameter log code to be written into the abstract syntax tree, and the position after the method output parameter position of the code block is determined as the target position for the output parameter log code to be written into the abstract syntax tree.

2. The method according to claim 1, characterized in that Before the step of obtaining an abstract syntax tree obtained by parsing the source code, the method further includes: If it is detected that the source code is compiled, a syntax analysis is performed on the source code to obtain a syntax analysis result of the source code; The source code is structurally transformed according to the syntax analysis result to obtain an abstract syntax tree.

3. The method according to claim 1, characterized in that If there is log annotation information in the source code, the step of generating log output code according to the log annotation information includes: If the log annotation information exists in the source code, the log output code is generated according to the generation parameters of the log output code included in the log annotation information.

4. The method according to claim 3, characterized in that The generation parameters of the log output code include one or more of the following: log record prefix information, blacklist configuration, whitelist configuration and log output level; wherein the whitelist configuration includes: parameter names and / or attribute values ​​of the logs to be collected; the blacklist configuration includes: parameter names to be excluded and / or attribute values ​​to be excluded in log collection.

5. The method according to claim 4, characterized in that The step of generating the log output code according to the generation parameters of the log output code included in the log annotation information comprises: Determine whether there is a whitelist configuration in the generation parameters of the log output code; If yes, generating the log output code according to the parameter name and / or attribute value of the log to be collected included in the whitelist configuration; Otherwise, determine whether there is a blacklist configuration in the generation parameters of the log output code; If there is a blacklist configuration in the generation parameters of the log output code, the parameter name and / or attribute value of the log to be collected is determined according to the blacklist configuration, and the log output code is generated according to the determined parameter name and / or attribute value.

6. The method according to claim 1, characterized in that The step of outputting logs through the first abstract syntax tree includes: The first abstract syntax tree is converted into bytecode, and log output is performed by executing the bytecode.

7. A log output device, characterized in that: include: An acquisition module, configured to acquire an abstract syntax tree obtained by parsing the source code if it is detected that the source code is compiled; A determination module, configured to generate a log output code according to the log annotation information if there is log annotation information in the source code, and determine a target position in the abstract syntax tree where the log output code is to be written; An insertion module, configured to write the log output code into the abstract syntax tree to generate a first abstract syntax tree according to the target position, and perform log output through the first abstract syntax tree; The log output code includes an input parameter log code and an output parameter log code, and the step of determining a target position in the abstract syntax tree where the log output code is to be written includes: obtaining a method input parameter position and a method output parameter position of a code block in the abstract syntax tree where log annotation information exists; The position before the method input parameter position of the code block is determined as the target position for the input parameter log code to be written into the abstract syntax tree, and the position after the method output parameter position of the code block is determined as the target position for the output parameter log code to be written into the abstract syntax tree.

8. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 6.

9. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Electronic equipment, method and device for standardizing logging code output

    CN106598825A

  • Log collection method, device and system and computer storage medium

    CN109885549A

  • Log recording method, computer equipment and storage medium

    CN111522708A

  • Log content protection method and device, computer equipment and storage medium

    CN111651781A

  • Method, system, and program for displaying program execution process

    JP2008059515A