CN113760291A - Log output method and device - Google Patents

Log output method and device Download PDF

Info

Publication number
CN113760291A
CN113760291A CN202010973017.0A CN202010973017A CN113760291A CN 113760291 A CN113760291 A CN 113760291A CN 202010973017 A CN202010973017 A CN 202010973017A CN 113760291 A CN113760291 A CN 113760291A
Authority
CN
China
Prior art keywords
log
code
syntax tree
abstract syntax
log output
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202010973017.0A
Other languages
Chinese (zh)
Inventor
宋金明
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Century Trading Co Ltd
Beijing Wodong Tianjun Information Technology Co Ltd
Original Assignee
Beijing Jingdong Century Trading Co Ltd
Beijing Wodong Tianjun Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Jingdong Century Trading Co Ltd, Beijing Wodong Tianjun Information Technology Co Ltd filed Critical Beijing Jingdong Century Trading Co Ltd
Priority to CN202010973017.0A priority Critical patent/CN113760291A/en
Publication of CN113760291A publication Critical patent/CN113760291A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis

Abstract

The invention discloses a method and a device for outputting logs, and relates to the technical field of computers. One specific implementation of the method for outputting the log comprises the following steps: if the source code is compiled, acquiring an abstract syntax tree obtained by analyzing the source code; if the source code has the log annotation information, generating a log output code according to the log annotation information, and determining a target position of the log output code to be written into the abstract syntax tree; and according to the target position, 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 embodiment can automatically output the log and improve the development efficiency.

Description

Log output method and device
Technical Field
The invention relates to the technical field of computers, in particular to a method and a device for outputting logs.
Background
In the system development process, the log recording and outputting are an indispensable link. After the system is on line, development and operation and maintenance personnel can know the actual running condition on the line through the log. And when the system has problems, the running data of the system can be restored through the log, so that developers are helped to repair the system more quickly and accurately. In the prior art, logging can be performed in the following manner: 1) a developer manually adds a log recording code to record and output a log; 2) and uniformly adding log recording codes before and after the method in an Aspect Programming (AOP) mode to record and output logs. However, the above two methods of logging and outputting cannot automatically log, and output efficiency is low.
Disclosure of Invention
In view of this, embodiments of the present invention provide a method and an apparatus for log output, which can solve the problem that the existing log output method cannot automatically perform log recording.
To achieve the above object, according to an aspect of an embodiment of the present invention, there is provided a method of logging output.
The log output method of the embodiment of the invention comprises the following steps:
if the source code is compiled, acquiring an abstract syntax tree obtained by analyzing the source code;
if the source code has the log annotation information, generating a log output code according to the log annotation information, and determining a target position of the log output code to be written into the abstract syntax tree;
and according to the target position, 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.
Optionally, if it is detected that the source code is compiled, the step of parsing the abstract syntax tree from the source code includes:
if the source code is compiled, carrying out syntactic analysis on the source code to obtain a syntactic analysis result of the source code;
and carrying out structural conversion on the source code according to the syntax analysis result to obtain an abstract syntax tree.
Optionally, if the source code has the log annotation information, the step of generating the log output code according to the log annotation information includes:
and if the source code has the log annotation information, generating a log output code according to the generation parameter of the log output code included in the log annotation information.
Optionally, the generation parameters of the log output code include one or more of: recording prefix information, black list configuration, white list configuration and log output level by the log; wherein the white list configuration comprises: collecting the parameter name and/or attribute value of the log; the blacklist configuration comprises: the name of the parameter to be excluded and/or the value of the attribute to be excluded in the log collection.
Optionally, the step of generating a log output code according to a generation parameter of the log output code included in the log annotation information includes:
judging whether white list configuration exists in the generation parameters of the log output codes;
if yes, configuring parameter names and/or attribute values of the logs to be collected according to the white list, and generating log output codes;
otherwise, judging whether blacklist configuration exists in the generation parameters of the log output codes;
and if the generation parameters of the log output codes have blacklist configuration, determining parameter names and/or attribute values of the logs to be collected according to the blacklist configuration, and generating the log output codes according to the determined parameter names and/or attribute values.
Optionally, the step of outputting the log through the first abstract syntax tree includes:
and converting the first abstract syntax tree into byte codes, and performing log output by executing the byte codes.
Optionally, the log output code includes an entry log code and an exit log code, and the step of determining that the log output code is to be written into a target location in the abstract syntax tree includes:
acquiring a method entry position and a method exit position of a code block with log annotation information in the abstract syntax tree;
determining the method parameter position of the code block as a target position of the parameter log code to be written in the abstract syntax tree before determining the method parameter position of the code block, and determining the method parameter position of the code block as a target position of the parameter log code to be written in the abstract syntax tree after determining the method parameter position of the code block.
To achieve the above object, according to another aspect of an embodiment of the present invention, there is provided an apparatus for log output.
The log output device of the embodiment of the invention comprises:
the obtaining module is used for obtaining an abstract syntax tree obtained by analyzing a source code if the source code is compiled;
the determining module is used for generating a log output code according to the log annotation information if the log annotation information exists in the source code, and determining a target position of the log output code to be written into the abstract syntax tree;
and the inserting module is used for writing the log output code into the abstract syntax tree according to the target position to generate a first abstract syntax tree, and performing log output through the first abstract syntax tree.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided an electronic apparatus.
The electronic device of the embodiment of the invention comprises:
one or more processors;
a storage device for storing one or more programs,
when executed by the one or more processors, cause the one or more processors to implement the method as described above.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided a computer-readable medium.
A computer-readable medium of an embodiment of the invention has stored thereon a computer program which, when executed by a processor, implements the method as described above.
One embodiment of the above invention has the following advantages or benefits:
in the embodiment of the invention, the abstract syntax tree is modified during compiling the source code, and the corresponding log output code is automatically added in the abstract syntax tree.
Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
FIG. 1 is a flow chart diagram of a method of log output in a first embodiment of the invention;
FIG. 2 is a diagram illustrating a parsing process of an abstract syntax tree according to an embodiment of the present invention;
FIG. 3 is a flowchart illustrating a method for logging output according to a second embodiment of the present invention;
FIG. 4 is a flowchart illustrating a method of log output according to a third embodiment of the present invention;
FIG. 5 is a block diagram of an apparatus for log output according to an embodiment of the present invention;
FIG. 6 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;
fig. 7 is a schematic block diagram of a computer system suitable for use in implementing a terminal device or server of an embodiment of the invention.
Detailed Description
Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
Generally, different developers will have different results when logging is performed, and whether the logging content is complete. And too much log records can influence the running performance of the system, and too few log records cannot meet the use requirement. Therefore, the logging is properly and completely performed and output, and plays an abnormally important role in system development and debugging and system online maintenance.
The existing log recording mode has the following problems:
1) developers add log record codes manually, the workload is large, and the development efficiency is influenced. In addition, the randomness is high, the log content has no unified standard, the log cannot be accurately found during use, and a source code needs to be found;
2) the problem of too much or too little log record content exists;
3) only fixed log description information can be added, and the method is difficult to define by self, cannot adapt to specific service scenes, and cannot realize customization;
4) log records are not standard, log contents are output more, and the records cannot be pertinently recorded;
5) and the section interception is carried out before and after the access method in operation, so that the operation performance is influenced.
In order to solve the problem of the existing log recording and outputting manner, an embodiment of the present invention provides a method for outputting a log, fig. 1 is a schematic flow chart of the method for outputting a log according to the first embodiment of the present invention, and as shown in fig. 1, the method may include steps S101 to S103 as follows.
Step S101: and if the source code is detected to be compiled, acquiring an abstract syntax tree obtained by analyzing the source code.
The source code can be a code file written in any language. Taking the source code as a Java file as an example, a syntax analyzer exists in the Java language, and the syntax analyzer reads and analyzes the source code as a character string and establishes an abstract syntax tree, which is a necessary early work for a program to complete compilation.
Before acquiring the abstract syntax tree parsed from 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 an abstract syntax tree from a source code, firstly, the source code is parsed to obtain a parsing result of the source code. Wherein the source code may be parsed using a parser. And then carrying out structural conversion on the source code according to a 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 by the syntax analysis manner, and then the abstract syntax tree of the source code is formed by the regular syntax tree.
As shown in fig. 2, fig. 2 is a schematic diagram of a parsing process of an abstract syntax tree according to an embodiment of the present invention. A detailed process of parsing an assignment statement into a regular syntax tree is shown.
Step S102: and if the source code has the log annotation information, generating a log output code according to the log annotation information, and determining a target position of the log output code to be written in the abstract syntax tree.
Adding log annotation information in the method or class needing to log. When the source code is compiled, the log output code is automatically added to the byte code for the method of adding the log annotation information. The annotation code is, for example, as follows:
Figure BDA0002684785040000061
Figure BDA0002684785040000071
according to the technical scheme of the embodiment of the invention, when the log output code is generated, the log output code can be generated according to the generation parameter 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: the log records prefix information, black list configuration, white list configuration and log output level. Wherein the white list configuration comprises: collecting the parameter name and/or attribute value of the log; the blacklist configuration comprises: the name of the parameter to be excluded and/or the value of the attribute to be excluded in the log collection. The log record prefix information is used for defining the prefix information of the log, and the log record prefix information has a default value and can be defined by a user. The excluded parameter name is mainly used for logging the designated parameter when the entry parameter contains a plurality of parameters, so that the log performance of the system can be improved and the log output quantity can be reduced. The exclusion designation attribute value is used to indicate the designation attribute value in the exclusion parameter object, and is not output to the log, so that some useless log information can be excluded, thereby improving the system performance and reducing the log amount. If there are identical attribute names in multiple entries, they are excluded. If the white list configuration is specified, only the corresponding attribute information is output. In addition, different log types can be set to different output levels, and the log annotation information types comprise one or more of the following items: error (error), warning (war), off (off), default (info), and bug fix (debug), among other types. For example: the level of the log annotation information type for repairing the vulnerability can be set to be the highest, so that developers can know the repairing process of the system conveniently. Meanwhile, whether the log annotation information is generated into a log output code or not can be determined according to the log output level so as to control the log output process.
Generally, all parameters may be set for logging or some parameters may be set for logging. In order to improve the system log performance and reduce the log output amount, a specified parameter for not generating a log may be set in advance. And if the source code has the log annotation and the designated parameter exists in the log annotation, not generating the log output code corresponding to the designated parameter in the log annotation. It is understood that if a parameter contains multiple parameters, the specified parameter may not be logged.
Because the source code comprises at least one code block, whether the abstract syntax tree has the log annotation information or not can be searched by taking the code block as a unit, and then the log input code is respectively generated according to the log annotation information of each code block.
When the log output code is generated according to the generation parameter of the log output code, how to generate the log output code can be determined according to the specific content of the generation parameter. It should be noted that, in general, the white list configuration and the black list configuration cannot be configured and operated at the same time. Therefore, when generating the log output code, the process is roughly as follows: judging whether white list configuration exists in the generation parameters of the log output codes; if yes, configuring parameter names and/or attribute values of the logs to be collected according to the white list, and generating log output codes; otherwise, judging whether blacklist configuration exists in the generation parameters of the log output codes; and if the generation parameters of the log output codes have blacklist configuration, determining parameter names and/or attribute values of the logs to be collected according to the blacklist configuration, and generating the log output codes according to the determined parameter names and/or attribute values.
Before generating a log output code according to the log annotation information in step S102, the @ AutoLog annotation may be added before the method that the log needs to be added, and the corresponding attribute may be configured according to the actual situation, or a default configuration may not be configured. In one embodiment of the invention, such as: adding a prefix "gateway querygroupstack" content before outputting the log, and setting attribute information includeParams as "locTag" to generate a log output code related to the log annotation information, see the following code example:
@AutoLog(
logPreMessage="gateway queryGroupStock",
includeParams="locTag"
)
public List<WmsStockCollectLocDTO>queryGroupStock(WmsLocStockQueryParamDTO wmsLocStockQueryParamDTO){
// business logic outline
return listWmsStockCollectDTO;
}。
After the source code is compiled, the decompiling content of the generated Class file is as follows:
@AutoLog(
logPreMessage="gateway queryGroupStock",
includeParams="locTag"
)
public List<WmsStockCollectLocDTO>queryGroupStock(WmsLocStockQueryParamDTO wmsLocStockQueryParamDTO){
log.info("gateway queryGroupStock in:wmsStkQueryParamDTO:{}",wmsLocStockQueryParamDTO.getLocTag());
// business logic outline
log.info("gateway queryGroupStock out:listWmsStockCollectDTO:{}",JSON.toJSONString(listWmsStockCollectDTO));
return listWmsStockCollectDTO;
}。
It will be appreciated that the log. info related log code can be annotated by @ AutoLog without manual writing, but only after compilation of the decompiled code of the class file will have log.info related information.
Meanwhile, the log annotation information can also be used for indicating the insertion position of the log output code. When logging is required, logging annotation is performed before the code blocks of the log are to be logged and output. Generally, the log of records and outputs includes at least: in order to determine the target position of the log output code in the abstract syntax tree, a method entry position and a method exit position of a code block with log annotation information in the abstract syntax tree can be obtained first. And then determining the method parameter position of the code block as the target position of the parameter log code to be written into the abstract syntax tree before determining the method parameter position of the code block as the target position of the parameter log code to be written into the abstract syntax tree after determining the method parameter position of the code block as the target position of the parameter log code to be written into the abstract syntax tree, so that logs can be inserted respectively aiming at parameter entry and parameter exit.
It should be noted that before the method entry position of the code block, the previous line of the code corresponding to the method entry position may be understood, and after the method entry position of the code block, the next line of the code corresponding to the method entry position may be understood.
Step S103: and according to the target position, 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.
After step S103, in order to facilitate machine execution of the code, the first abstract syntax tree may be converted into bytecode and log-output by executing the bytecode.
In the embodiment of the invention, the abstract syntax tree is modified during compiling the source code, and the corresponding log output code (or called log printing code) is automatically added into the abstract syntax tree.
Fig. 3 is a flowchart illustrating a method for outputting logs according to a second embodiment of the present invention, and as shown in fig. 3, the method may include steps S301 to S308 as follows.
Step S301: and if the source code is detected to be compiled, acquiring the source code.
In step S301, the source code may be a code file written in any language. The source code may be Java (Java is a door-to-object programming language) source code, although this is not a limitation.
Step S302: an abstract syntax tree is parsed from the source code.
In step S302, when the developer compiles the source code, an abstract syntax tree is parsed from the source code. The process of parsing is roughly: firstly, carrying out syntactic analysis on the source code to obtain a syntactic analysis result of the source code; and then carrying out structural conversion on the source code according to a syntax analysis result to obtain an abstract syntax tree.
Step S303: judging whether the source code has the log annotation information or not, automatically triggering an annotation processor when the source code is compiled, and judging whether the source code has the log annotation information or not by the annotation processor and carrying out corresponding processing according to the log annotation information. If yes, go to step S304; otherwise, step S301 is repeatedly performed.
The log annotation information includes a generation parameter for generating the log output code, and a position where the log output code is to be written in the abstract syntax tree is determined according to the log annotation information. In step S303, the generation parameters of the log output code include one or more of: the log records prefix information, black list configuration, white list configuration and log output level. Wherein the white list configuration comprises: collecting the parameter name and/or attribute value of the log; the blacklist configuration comprises: the name of the parameter to be excluded and/or the value of the attribute to be excluded in the log collection. The log record prefix information is used for defining the prefix information of the log, and the log record prefix information has a default value and can be defined by a user. The excluded parameter name is mainly used for logging the designated parameter when the entry parameter contains a plurality of parameters, so that the log performance of the system can be improved and the log output quantity can be reduced. The attribute value to be excluded is used for indicating the designated attribute value in the excluded parameter object and is not output to the log, so that some useless log information can be excluded, thereby improving the system performance and reducing the log quantity. If there are identical attribute names in multiple entries, they are excluded. If the white list configuration is specified, only the corresponding attribute information is output. Different log types may be set to different output levels, the log annotation information types including one or more of: error (error), warning (war), off (off), default (info), and bug fix (debug), among other types. For example: the level of the log annotation information type for repairing the vulnerability can be set to be the highest, so that developers can know the repairing process of the system conveniently. Meanwhile, whether the log annotation information is generated into a log output code or not can be determined according to the log output level so as to control the log output process.
Step S304: and reading the abstract syntax tree of the source code.
In step S304, the abstract syntax tree of the current class is read, and then step S305 is performed.
Step S305: and generating a log output code according to the log annotation information.
In step S305, the log annotation information includes a generation parameter of the log output code. In order to generate the log output code, the log output code may be generated according to a generation parameter of the log output code included in the log annotation information. Because the source code comprises at least one code block, whether the abstract syntax tree has the log annotation information or not can be searched by taking the code block as a unit, and then the log input code is respectively generated according to the log annotation information of each code block.
When the log output code is generated according to the generation parameter of the log output code, how to generate the log output code can be determined according to the specific content of the generation parameter. It should be noted that, in general, the white list configuration and the black list configuration cannot be simultaneously activated. Therefore, when generating the log output code, the process is roughly as follows: judging whether white list configuration exists in the generation parameters of the log output codes; if yes, configuring parameter names and/or attribute values of the logs to be collected according to the white list, and generating log output codes; otherwise, judging whether blacklist configuration exists in the generation parameters of the log output codes; and if the generation parameters of the log output codes have blacklist configuration, determining parameter names and/or attribute values of the logs to be collected according to the blacklist configuration, and generating the log output codes according to the determined parameter names and/or attribute values. Determining the parameter name and/or attribute value of the log to be collected according to the blacklist configuration, and generating a log output code according to the determined parameter name and/or attribute value.
Generally, all parameters may be set for logging or some parameters may be set for logging. In order to improve the system log performance and reduce the log output quantity, the parameter names and/or attribute values to be excluded in the log collection can be preset in the blacklist, and the corresponding log is not generated. It can be understood that, if a log annotation exists in the source code and the log annotation has a parameter name and/or an attribute value to be excluded, the parameter name and/or the attribute value to be excluded are deleted from the parameter entry and/or the parameter entry attribute of the method of the log to be acquired, and the parameter name and/or the attribute value obtained after deletion are used as the parameter entry and/or the parameter entry attribute of the method of the log to be acquired to generate the log output code. By executing the log output code, log recording of parameter names and/or attribute values to be excluded can be achieved.
Step S306: and determining a target position of the log output code to be written into the abstract syntax tree, writing the log output code into the abstract syntax tree according to the target position to generate a first abstract syntax tree, and performing log output through the first abstract syntax tree.
In step S306, the log annotation information may also be used to indicate the insertion position of the log output code. Generally, the log to be output includes at least: in order to determine the target position of the log output code written into the abstract syntax tree, a method entry position and a method exit position of a code block with log annotation information in the abstract syntax tree can be obtained first. And then determining the method parameter position of the code block as the target position of the parameter log code to be written into the abstract syntax tree before determining the method parameter position of the code block as the target position of the parameter log code to be written into the abstract syntax tree after determining the method parameter position of the code block as the target position of the parameter log code to be written into the abstract syntax tree, so that log output codes can be inserted respectively aiming at parameter entry and parameter exit.
It should be noted that before the method entry position of the code block, the previous line of the code corresponding to the method entry position may be understood, and after the method entry position of the code block, the next line of the code corresponding to the method entry position may be understood.
Before step S306, attribute information on the custom annotation may be obtained, such as: log output level, name of parameter to be excluded in log collection and/or attribute value.
Step S307: and judging whether the first abstract syntax tree conforms to a preset specification. If yes, go to step S308; otherwise, step S306 is repeatedly performed.
In step S307, the preset specification relates to the kind of the source code, such as: if the source code is a Java source code, the preset specification may be a Java specification, that is, the Java will automatically detect whether the abstract syntax tree generated in step S306 meets the Java specification.
Step S308: and converting the first abstract syntax tree into byte codes, and performing log output by executing the byte codes.
In step S308, the first abstract syntax tree containing the log output code is generated into bytecode for machine execution.
In the embodiment of the present invention, through steps S301 to S308, the generation of the log code can be automatically completed and written into the abstract syntax tree. This embodiment has the advantages of convenience, rapidness, standardization and accuracy.
In the embodiment of the invention, by using the Java annotation processor technology, the corresponding log output code is automatically added in the abstract syntax tree by modifying the abstract syntax tree when compiling the source code, and by adopting the mode, the log output can be automatically realized, and the development efficiency can be improved.
In the embodiment of the invention, when the source code is detected to be compiled, the log output code is automatically added into the source code by modifying the rule syntax tree, which is equivalent to automatically adding the log code. Compared with other AOP methods, the method belongs to the editor level, the opportunity is more advanced, and the efficiency is higher.
It can be understood that, at the time of source code compiling, the log annotation AutoLog in the code is identified, and corresponding log output codes are added before and after the method is labeled, so as to automatically add the log output codes in the byte codes.
Fig. 4 is a flowchart illustrating a method of outputting logs according to a third embodiment of the present invention, and as shown in fig. 4, the method may include steps S401 to S412 as follows.
Step S401: and if the source code is detected to be compiled, carrying out syntactic analysis on the source code to obtain a syntactic analysis result of the source code.
In step S401, the source code may be a code file written in any language. Taking the source code as a Java file as an example, a syntax analyzer exists in the Java language, and the syntax analyzer reads and analyzes the source code as a character string and establishes an abstract syntax tree, which is a necessary early work for a program to complete compilation.
Step S402: and carrying out structural conversion on the source code according to the syntax analysis result to obtain an abstract syntax tree.
Step S403: if the source code has the log annotation information, the code blocks with the log annotation marks in the abstract syntax tree are searched, and the log annotation information of each code block is obtained. Step S404 or step S405 is executed after step S403.
Step S404: judging whether white list configuration exists in the generation parameters of the log output codes; if yes, go to step S405; otherwise, step S406 is performed.
Step S405: and generating the log output code according to the parameter name and/or the attribute value of the log to be collected, which are included in the white list configuration. Step S408 is then performed.
In step S405, when there are a plurality of log annotations for a code block, in order to generate a log output code, log annotation information for each of the code blocks may be acquired, and the log output code may be generated respectively according to the acquired log annotation information.
In order to generate the log output code, the log output code may be generated according to a generation parameter of the log output code included in the log annotation information. Because the source code comprises at least one code block, whether the abstract syntax tree has the log annotation information or not can be searched by taking the code block as a unit, and then the log input code is respectively generated according to the log annotation information of each code block.
Step S406: and judging whether blacklist configuration exists in the generation parameters of the log output codes. If yes, go to step S407; otherwise, the flow ends.
Step S407: and determining the parameter name and/or the attribute value of the log to be collected according to the blacklist configuration, and generating a log output code according to the determined parameter name and/or attribute value. Step S408 is then performed.
In step S407, when there are a plurality of log annotations for one code block, in order to generate a log output code, log annotation information for each of the code blocks may be acquired, and the log output code may be generated respectively according to the acquired log annotation information.
Log annotation information on the custom annotation may be obtained, such as: log output level (or referred to as log print level), parameter names to be excluded, and/or attribute values.
Step S408: and reading the abstract syntax tree of the source code.
Step S409: determining a target location of the log output code in the abstract syntax tree.
In step S409, the log annotation information may also be used to indicate the insertion location of the log output code. Generally, the log annotations include at least: in order to determine the target position, a method entry position and a method exit position of a code block with log annotation information in the abstract syntax tree can be obtained first. And then determining the method parameter input position of the code block as a target position of the parameter input log code to be written into the abstract syntax tree before, and determining the method parameter output position of the code block as a target position of the parameter output log code to be written into the abstract syntax tree after, so that log output codes can be inserted respectively aiming at parameter input and parameter output of the method of the log to be collected.
It should be noted that before the method reference position of the code block, it may be understood as the previous line of the code corresponding to the reference position, and after the method reference position of the code block, it may be understood as the next line of the code corresponding to the reference position. Step S410: and writing the log output code into a target position corresponding to the abstract syntax tree to form a first abstract syntax tree.
Step S411: and judging whether the first abstract syntax tree conforms to a preset specification. If yes, go to step S412; otherwise, step S410 is repeatedly performed.
Step S412: the bytecode is generated according to the first abstract syntax tree.
The log output method of the embodiment of the invention has the following advantages:
1) the development efficiency can be improved. The development personnel do not pay much attention to how to output the log content, such as: and (4) an inventory query method A, wherein the input parameter is X, and the output parameter is Y. Only by annotating the configuration. All methods may even be configured on a class.
2) System performance may be improved. And the access level can be reduced and the system operation performance can be improved without using a global section.
3) A standardized output can be achieved. The log output can be carried out by adopting a standard format so as to facilitate the later-stage log searching.
4) The method can realize refined output and avoid the problem of incomplete log information caused by insufficient experience of developers.
Fig. 5 is a block diagram of an apparatus for log output according to an embodiment of the present invention, and referring to fig. 5, the apparatus 500 for log output includes:
an obtaining module 501, configured to obtain an abstract syntax tree obtained by parsing a source code if it is detected that the source code is compiled;
a determining module 502, configured to generate a log output code according to the log annotation information if the source code has the log annotation information, and determine a target position where the log output code is to be written in the abstract syntax tree;
and an inserting module 503, configured to write the log output code into the abstract syntax tree according to the target location to generate a first abstract syntax tree, and perform log output through the first abstract syntax tree.
Optionally, the apparatus 500 for log output further includes:
the analysis module is used for carrying out syntactic analysis on the source code to obtain a syntactic analysis result of the source code if the fact that the source code is compiled is detected;
and the conversion module is used for carrying out structural conversion on the source code according to the syntax analysis result so as to obtain an abstract syntax tree.
Optionally, the determining module 502 is further configured to:
and if the source code has the log annotation information, generating a log output code according to the generation parameter of the log output code included in the log annotation information.
Optionally, the insertion module 503 is further configured to:
and converting the first abstract syntax tree into byte codes, and performing log output by executing the byte codes.
Optionally, the generation parameters of the log output code include one or more of: recording prefix information, black list configuration, white list configuration and log output level by the log; wherein the white list configuration comprises: collecting the parameter name and/or attribute value of the log; the blacklist configuration comprises: the name of the parameter to be excluded and/or the value of the attribute to be excluded in the log collection.
Optionally, the determining module 502 is further configured to:
judging whether white list configuration exists in the generation parameters of the log output codes;
if yes, configuring parameter names and/or attribute values of the logs to be collected according to the white list, and generating log output codes;
otherwise, judging whether blacklist configuration exists in the generation parameters of the log output codes;
and if the generation parameters of the log output codes have blacklist configuration, determining parameter names and/or attribute values of the logs to be collected according to the blacklist configuration, and generating the log output codes according to the determined parameter names and/or attribute values.
Optionally, the insertion module 503 is further configured to:
acquiring a method entry position and a method exit position of a code block with log annotation information in the abstract syntax tree;
determining the method parameter position of the code block as a target position of the parameter log code to be written in the abstract syntax tree before determining the method parameter position of the code block, and determining the method parameter position of the code block as a target position of the parameter log code to be written in the abstract syntax tree after determining the method parameter position of the code block.
In the embodiment of the invention, the abstract syntax tree is modified during compiling the source code, and the corresponding log output code (or called log printing code) is automatically added into the abstract syntax tree.
Fig. 6 illustrates an exemplary system architecture 600 to which the method of logging or the apparatus of logging of an embodiment of the invention may be applied.
As shown in fig. 6, the system architecture 600 may include terminal devices 601, 602, 603, a network 604, and a server 605. The network 604 serves to provide a medium for communication links between the terminal devices 601, 602, 603 and the server 605. Network 604 may include various types of connections, such as wire, wireless communication links, or fiber optic cables, to name a few.
A user may use the terminal devices 601, 602, 603 to interact with the server 605 via the network 604 to receive or send messages or the like.
The terminal devices 601, 602, 603 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and the like.
It should be noted that the method for outputting logs provided by the embodiment of the present invention is generally executed by the server 605, and accordingly, the apparatus for outputting logs is generally disposed in the server 605.
It should be understood that the number of terminal devices, networks, and servers in fig. 6 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 7, shown is a block diagram of a computer system 700 suitable for use with a terminal device implementing an embodiment of the present invention. The terminal device shown in fig. 7 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present invention.
As shown in fig. 7, the computer system 700 includes a Central Processing Unit (CPU)701, which can perform various appropriate actions and processes in accordance with a program stored in a Read Only Memory (ROM)702 or a program loaded from a storage section 708 into a Random Access Memory (RAM) 703. In the RAM 703, various programs and data necessary 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 bus 704.
The following components are connected to the I/O interface 705: an input portion 706 including a keyboard, a mouse, and the like; an output section 707 including a display such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage section 708 including a hard disk and the like; and a communication section 709 including a network interface card such as a LAN card, a modem, or the like. 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, or the like is mounted on the drive 710 as necessary, so that a computer program read out therefrom is mounted into the storage section 708 as necessary.
In particular, according to the embodiments of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method illustrated in the flow chart. In such an embodiment, the computer program can be downloaded and installed from a network through the communication section 709, and/or installed from the removable medium 711. The computer program performs the above-described functions defined in the system of the present invention when executed by the Central Processing Unit (CPU) 701.
It should be noted that the computer readable medium shown in the present invention can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, 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 disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present invention, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to comprise: if the source code is compiled, acquiring an abstract syntax tree obtained by analyzing the source code; if the source code has the log annotation information, generating a log output code according to the log annotation information, and determining a target position of the log output code to be written into the abstract syntax tree; and according to the target position, 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.
According to the technical scheme of the embodiment of the invention, automatic log output can be realized, and the development efficiency can be improved.
The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (10)

1. A method of logging output, comprising:
if the source code is compiled, acquiring an abstract syntax tree obtained by analyzing the source code;
if the source code has the log annotation information, generating a log output code according to the log annotation information, and determining a target position of the log output code to be written into the abstract syntax tree;
and according to the target position, 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.
2. The method of claim 1, wherein prior to the step of obtaining the abstract syntax tree parsed from the source code, the method further comprises:
if the source code is compiled, carrying out syntactic analysis on the source code to obtain a syntactic analysis result of the source code;
and carrying out structural conversion on the source code according to the syntax analysis result to obtain an abstract syntax tree.
3. The method of claim 1, wherein if log annotation information exists in the source code, the step of generating a log output code according to the log annotation information comprises:
and if the source code has the log annotation information, generating a log output code according to the generation parameter of the log output code included in the log annotation information.
4. The method of claim 3, wherein the generation parameters of the log output code include one or more of: recording prefix information, black list configuration, white list configuration and log output level by the log; wherein the white list configuration comprises: collecting the parameter name and/or attribute value of the log; the blacklist configuration comprises: the name of the parameter to be excluded and/or the value of the attribute to be excluded in the log collection.
5. The method according to claim 4, wherein the step of generating a log output code according to a generation parameter of the log output code included in the log annotation information comprises:
judging whether white list configuration exists in the generation parameters of the log output codes;
if yes, configuring parameter names and/or attribute values of the logs to be collected according to the white list, and generating log output codes;
otherwise, judging whether blacklist configuration exists in the generation parameters of the log output codes;
and if the generation parameters of the log output codes have blacklist configuration, determining parameter names and/or attribute values of the logs to be collected according to the blacklist configuration, and generating the log output codes according to the determined parameter names and/or attribute values.
6. The method of claim 1, wherein the step of logging through the first abstract syntax tree comprises:
and converting the first abstract syntax tree into byte codes, and performing log output by executing the byte codes.
7. The method of claim 1, wherein the log output code comprises an entry log code and an exit log code, and wherein determining the target location at which the log output code is to be written in the abstract syntax tree comprises:
acquiring a method entry position and a method exit position of a code block with log annotation information in the abstract syntax tree;
determining the method parameter position of the code block as a target position of the parameter log code to be written in the abstract syntax tree before determining the method parameter position of the code block, and determining the method parameter position of the code block as a target position of the parameter log code to be written in the abstract syntax tree after determining the method parameter position of the code block.
8. An apparatus for log output, comprising:
the obtaining module is used for obtaining an abstract syntax tree obtained by analyzing a source code if the source code is compiled;
the determining module is used for generating a log output code according to the log annotation information if the log annotation information exists in the source code, and determining a target position of the log output code to be written into the abstract syntax tree;
and the inserting module is used for writing the log output code into the abstract syntax tree according to the target position to generate a first abstract syntax tree, and performing log output through the first abstract syntax tree.
9. An electronic device, comprising:
one or more processors;
a storage device for storing one or more programs,
when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-7.
10. A computer-readable medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-7.
CN202010973017.0A 2020-09-16 2020-09-16 Log output method and device Pending CN113760291A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010973017.0A CN113760291A (en) 2020-09-16 2020-09-16 Log output method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010973017.0A CN113760291A (en) 2020-09-16 2020-09-16 Log output method and device

Publications (1)

Publication Number Publication Date
CN113760291A true CN113760291A (en) 2021-12-07

Family

ID=78785688

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010973017.0A Pending CN113760291A (en) 2020-09-16 2020-09-16 Log output method and device

Country Status (1)

Country Link
CN (1) CN113760291A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114756183A (en) * 2022-06-16 2022-07-15 深圳市信润富联数字科技有限公司 Data printing method, device, equipment and storage medium

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114756183A (en) * 2022-06-16 2022-07-15 深圳市信润富联数字科技有限公司 Data printing method, device, equipment and storage medium

Similar Documents

Publication Publication Date Title
US11151018B2 (en) Method and apparatus for testing a code file
US9747190B2 (en) Analysis system, analysis method, and computer program product
CN103294598B (en) A kind of source code inspection method and device
CN112394942B (en) Distributed software development compiling method and software development platform based on cloud computing
CN111124479B (en) Method and system for analyzing configuration file and electronic equipment
CN111736840A (en) Compiling method and running method of applet, storage medium and electronic equipment
CN111740948B (en) Data packet issuing method, dynamic updating method, device, equipment and medium
US10866803B2 (en) Generating interaction libraries
CN113760729A (en) Code detection method and device
CA2675692C (en) Compiler-assisted program source code filter
CN110659210A (en) Information acquisition method and device, electronic equipment and storage medium
CN113760291A (en) Log output method and device
CN112988578A (en) Automatic testing method and device
CN111258802A (en) Method for capturing application program crash information and related equipment
US8819645B2 (en) Application analysis device
CN115640279A (en) Method and device for constructing data blood relationship
CN108614704B (en) Code compiling method and device
CN109783133B (en) Code packaging method and device, computer equipment and storage medium
CN114047923A (en) Error code positioning method, device, storage medium and electronic equipment
CN112947941A (en) Method and device for adding exception handling code
CN113821486B (en) Method and device for determining dependency relationship between pod libraries and electronic equipment
Reid et al. Using the TypeScript compiler to fix erroneous Node. js snippets
CN117591104B (en) Model generation method and device, electronic equipment and storage medium
CN113608996B (en) Mirror image compiling test method, system, device and readable storage medium
CN110297639B (en) Method and apparatus for detecting code

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination