Log coloring method, device, electronic device and computer-readable storage medium
By introducing the proxy function in the log coloring package and dynamically inserting the log printing function, the problem that the log coloring tool in the existing technology cannot implement log coloring without changing the logic of the log printing function is solved, and log coloring is achieved without affecting the original log content.
Patent Information
- Application Number
- CN202010524465.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-10
- Publication Date
- 2025-09-12
- Estimated Expiration
- 2040-06-10
AI Technical Summary
Existing log collection tools do not have the log coloring function, and modifying the log printing function logic will affect the output of the original log content. Log coloring cannot be achieved without changing the log printing function logic.
By introducing the proxy function in the log coloring package, dynamically inserting and replacing the log printing function, the log printing function is replaced with the proxy function by dynamic instrumentation to achieve log coloring, and the colored log is output through the log output function.
Log coloring is implemented without changing the logic of the log printing function, ensuring that the original log content is not affected. It is also suitable for scenarios where modification of the log printing function logic is not allowed, reducing development workload.
Smart Images

Figure CN113778348B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and more specifically, to a log coloring method, device, electronic device, and computer-readable storage medium. Background Art
[0002] Log coloring is an important method in daily log analysis. The colored logs complete the statistical dimensions that are missing in the logs themselves. Storing the colored logs can prepare for later log analysis.
[0003] Currently, existing log collection tools lack the ability to colorize logs. To achieve this, you typically need to modify the logic of the original log printing function, i.e., modify the code corresponding to the original log printing function, which will affect the output of the original log content. Furthermore, in some scenarios where the logic of the original log printing function cannot be modified, this method cannot achieve log coloring. Summary of the Invention
[0004] The main purpose of the embodiments of the present application is to provide a log coloring method, device, electronic device and computer-readable storage medium. Through the scheme of the embodiments of the present application, log coloring can be achieved without changing the logic of the log printing function.
[0005] In a first aspect, an embodiment of the present application provides a log coloring method, the method comprising:
[0006] When the business code is running, the log printing function code corresponding to the business code is called. The log printing function code includes a log printing function, a log output function, and an introduced log coloring package. The log coloring package includes a proxy function for coloring the log;
[0007] When running to the log printing function code, the log printing function is replaced with the proxy function, so that the log corresponding to the business code is dyed through the proxy function to obtain a dyed log;
[0008] The dye log is output by calling the log output function of the log printing function code.
[0009] In an optional embodiment of the first aspect, if the log coloring package further includes a custom coloring code segment, wherein the custom coloring code segment includes a business parameter corresponding to the business code; the method further includes:
[0010] By running the custom coloring code segment, the parameter information corresponding to the business parameters is obtained;
[0011] The dyeing log also includes business parameters and parameter information corresponding to the business parameters.
[0012] In an optional embodiment of the first aspect, the method further includes:
[0013] Import the log coloring package into the main package file corresponding to the business code.
[0014] In an optional embodiment of the first aspect, replacing the log printing function with a proxy function includes:
[0015] Use dynamic instrumentation to replace the log printing function with a proxy function.
[0016] In an optional embodiment of the first aspect, a log printing function is replaced with a proxy function by a dynamic instrumentation method, including:
[0017] The dynamic instrumentation method based on JMP is used to replace the log printing function with the proxy function.
[0018] In an optional embodiment of the first aspect, the method further includes:
[0019] Call the log printing function to print the log corresponding to the business code through the log printing function;
[0020] Output the log corresponding to the business code by calling the log output function.
[0021] In an optional embodiment of the first aspect, the log output function includes a first log output function and a second log output function;
[0022] Output the log corresponding to the business code by calling the log output function, including:
[0023] Output the log corresponding to the business code by calling the first log output function;
[0024] Output the dye log by calling the log output function of the log printing function code, including:
[0025] Output the dye log by calling the second log output function.
[0026] In an optional embodiment of the first aspect, outputting the dyeing log by calling a log output function of a log printing function code includes:
[0027] By calling the log output function of the log printing function code, the dyeing log is output to the log file.
[0028] In an optional embodiment of the first aspect, the log file includes a first file and a second file, and outputting a log corresponding to the business code by calling a first log output function includes:
[0029] Output the log corresponding to the business code to the first file by calling the first log output function;
[0030] Output the dye log by calling the second log output function, including:
[0031] The dyeing log is output to the second file by calling the second log output function.
[0032] In a second aspect, the present application provides a log coloring device, comprising:
[0033] The code calling module is used to call the log printing function code corresponding to the business code when running the business code. The log printing function code includes a log printing function, a log output function and an imported log coloring package. The log coloring package includes a proxy function for coloring the log;
[0034] The log coloring module is used to replace the log printing function with the proxy function when running to the log printing function code, so as to color the log corresponding to the business code through the proxy function to obtain a colored log;
[0035] The log output module is used to output the dye log by calling the log output function of the log printing function code.
[0036] In an optional embodiment of the second aspect, if the log coloring package further includes a custom coloring code segment, wherein the custom coloring code segment includes a business parameter corresponding to the business code; the device further includes:
[0037] The custom module is used to obtain parameter information corresponding to the business parameters by running the custom coloring code segment; wherein the coloring log also includes the business parameters and the parameter information corresponding to the business parameters.
[0038] In an optional embodiment of the second aspect, the device further includes:
[0039] The coloring package import module is used to import the log coloring package into the main package file corresponding to the business code.
[0040] In an optional embodiment of the second aspect, when the log coloring module replaces the log printing function with the proxy function, it is specifically configured to:
[0041] Use dynamic instrumentation to replace the log printing function with a proxy function.
[0042] In an optional embodiment of the second aspect, when the log coloring module replaces the log printing function with the proxy function using the dynamic instrumentation method, the log coloring module is specifically configured to:
[0043] The dynamic instrumentation method based on JMP is used to replace the log printing function with the proxy function.
[0044] In an optional embodiment of the second aspect, the device further includes:
[0045] The original log printing module is used to call the log printing function to print the log corresponding to the business code through the log printing function; and output the log corresponding to the business code by calling the log output function.
[0046] In an optional embodiment of the second aspect, the log output function includes a first log output function and a second log output function;
[0047] The original log printing module is used to output the log corresponding to the business code by calling the log output function.
[0048] Output the log corresponding to the business code by calling the first log output function;
[0049] When the log output module outputs the dye log by calling the log output function of the log printing function code, it is specifically used to:
[0050] Output the dye log by calling the second log output function.
[0051] In an optional embodiment of the second aspect, when the log output module outputs the dye log by calling the log output function of the log printing function code, it is specifically configured to:
[0052] By calling the log output function of the log printing function code, the dyeing log is output to the log file.
[0053] In an optional embodiment of the second aspect, the log file includes a first file and a second file;
[0054] When the original log printing module outputs the log corresponding to the business code by calling the first log output function, it is specifically used to:
[0055] Output the log corresponding to the business code to the first file by calling the first log output function;
[0056] When the log output module outputs the dye log by calling the second log output function, it is specifically used to:
[0057] The dyeing log is output to the second file by calling the second log output function.
[0058] In a third aspect, an embodiment of the present application provides an electronic device, which includes a processor and a memory; readable instructions are stored in the memory, and when the readable instructions are loaded and executed by the processor, the method shown in the above-mentioned first aspect or any optional embodiment of the first aspect is implemented.
[0059] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program. When at least one computer program is loaded and executed by a processor, it implements the method shown in the above-mentioned first aspect or any optional embodiment of the first aspect.
[0060] The beneficial effects of the technical solution provided by the present application are as follows: a log coloring package is introduced into the log printing function code corresponding to the business code, and the log coloring package includes a proxy function for coloring the log. When the business code is running, the log printing function code corresponding to the business code is called. When the log printing function code is run, the log printing function is replaced with the proxy function, so that the log corresponding to the business code is colored through the proxy function to obtain a colored log, and the colored log is output by calling the log output function of the log printing function code. In this solution, additional log processing logic is added to the log printing function code to realize the coloring of the log, that is, the coloring of the log is realized through the proxy function, without modifying the processing logic of the log printing function, so that the original content of the log corresponding to the business code can be affected. Moreover, for some situations where the logic of the log printing function is not allowed to be modified, log coloring can also be achieved through this solution. BRIEF DESCRIPTION OF THE DRAWINGS
[0061] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for describing the embodiments of the present application.
[0062] Figure 1 A schematic diagram of a log dyeing method according to an embodiment of the present application;
[0063] Figure 2 A schematic diagram of example code corresponding to a log coloring method provided in one embodiment of the present application;
[0064] Figure 3 A schematic diagram of an example code corresponding to a custom coloring code segment provided in one embodiment of the present application;
[0065] Figure 4 A schematic diagram of a code corresponding to a calling function a provided in one embodiment of the present application;
[0066] Figure 5 A schematic diagram of an embodiment of the present application providing a method for implementing a function f to replace the corresponding code of a function;
[0067] Figure 6 A schematic diagram of a code corresponding to a call statement for modifying function a provided in one embodiment of the present application;
[0068] Figure 7A schematic diagram of part of the code corresponding to a dynamic instrumentation principle provided in one embodiment of the present application;
[0069] Figure 8 A schematic diagram of a code corresponding to log coloring provided in one embodiment of the present application;
[0070] Figure 9 A schematic structural diagram of a log dyeing device provided in one embodiment of the present application;
[0071] Figure 10 A schematic structural diagram of an electronic device provided in yet another embodiment of the present application. DETAILED DESCRIPTION
[0072] The following describes embodiments of the present application in detail. Examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present application and are not to be construed as limiting the present application.
[0073] It will be understood by those skilled in the art that, unless expressly stated otherwise, the singular forms "a", "an" and "the" used herein may also include the plural forms. It should be further understood that the term "comprising" used in the specification of this application refers to the presence of features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof. It should be understood that when we refer to an element as being "connected" or "coupled" to another element, it may be directly connected or coupled to the other element, or there may be intermediate elements. In addition, "connected" or "coupled" as used herein may include wireless connections or wireless couplings. The term "and / or" used herein includes all or any one of the units and all combinations of one or more associated listed items.
[0074] In order to better understand and illustrate the solutions of the embodiments of the present application, some technical terms involved in the embodiments of the present application are briefly explained below.
[0075] Log coloring: Add additional attribute tags to log lines. Logs with added attributes have new dimension attributes.
[0076] Runtime dynamic instrumentation: This method does not require business-side developers to modify the code. It inserts some probes (also called "probes") into the program while ensuring the original logical integrity of the program under test.
[0077] Log coloring is an important method in daily log analysis. The colored logs fill in the missing statistical dimensions of the logs themselves, preparing for subsequent log analysis.
[0078] Currently, existing log collection tools do not have the log coloring function. To achieve log coloring, the existing technology generally has the following implementation methods:
[0079] The first method is to modify the log framework. Specifically, add the color field to the text of the log line. When the framework outputs the log stream, the log is colored through the color field.
[0080] The second method is to write a general utility function for log printing. Specifically, append the output of this utility function to each log print location in the business code, using this utility function to color the logs. Within the utility function, you can also customize the fields to be colored and add or remove colored fields.
[0081] Both of the above methods can achieve log coloring, but there are still the following problems when achieving log coloring:
[0082] The first method has the disadvantage that it's not suitable for log processing tools that don't allow modification of the logic of their log printing functions. For example, Go language logging tools prohibit modification. For log processing tools that do allow modification, the first method is often used to modify the logging framework. This modification, which alters the logic of the log printing function, may require the business side to reinstall the Go environment image, impacting the user experience. Adding color fields to log lines can affect the output of the original log content, further impacting analysis and processing of the original log content.
[0083] The second method has the following disadvantages: Each time you print a log, you need to append the output of the general utility function to each location where the log is printed, which increases the cost of log printing. Furthermore, the second method places requirements on business code developers, requiring them to add the general utility function to each location where the log is printed, increasing their workload and making code modifications inappropriate.
[0084] Therefore, based on the above two solutions, although both can achieve log coloring, the applicability of the above two solutions is poor and needs to be improved.
[0085] The following specific embodiments describe in detail the technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0086] Figure 1A flow chart of a log coloring method provided by the present application is shown. In this example, the execution subject of the present application scheme is described using a server as an example. The server can be an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms. This application does not impose any restrictions on this.
[0087] like Figure 1 As shown, the method may include steps S110 to S130, wherein:
[0088] Step S110, when running the business code, calling the log printing function code corresponding to the business code, the log printing function code includes a log printing function, a log output function and an introduced log coloring package, and the log coloring package includes a proxy function for coloring the log.
[0089] The business code can be the code corresponding to any application, and the business code includes a log printing function code, which is used to print the log corresponding to the business code (hereinafter referred to as the original log), that is, the uncolored log. The log coloring package refers to a piece of code that can realize the coloring of the log.
[0090] Step S120, when running to the log printing function code, the log printing function is replaced with the proxy function, so as to color the log corresponding to the business code through the proxy function to obtain a colored log.
[0091] Among them, after the log coloring package is introduced into the log printing function code, when running to the log printing function code, the logic of the log printing function is not changed, and the log printing function is directly replaced by the proxy function. Then, when running to the log printing function, it is actually the proxy function that is run. Running the proxy function can realize the coloring of the log. The colored log is the log corresponding to the original log after coloring.
[0092] The log can be colored based on a set mark, for example, a set string, etc. Then, in the obtained colored log, the original log will carry the set mark.
[0093] Step S130: Output the dyeing log by calling the log output function of the log printing function code.
[0094] After the dyeing log is obtained, the dyeing log may be output to a log file through a log output function. The log file may be a pre-set log file.
[0095] In an optional solution of the present application, the dyeing log can be output to a log file. The log file can be in a txt format, and the format of the log file is not limited in the solution of the present application.
[0096] The dyeing log can also be directly stored in a designated database. The database can provide a query function for users. Users can search for the dyeing log from the database and perform subsequent analysis and processing based on the dyeing log.
[0097] A database, in short, can be thought of as a digital filing cabinet—a place where electronic files are stored, allowing users to add, query, update, and delete data. A database is a collection of data stored in a specific way, shared by multiple users, with minimal redundancy, and independent of applications.
[0098] In the solution of the present application, a database management system may also be selected as the log file to provide users with query support for logs in different languages.
[0099] A database management system (DBMS) is a computer software system designed for managing databases, typically providing basic functions such as storage, retrieval, security, and backup. Database management systems can be categorized based on the database model they support, such as relational or XML (Extensible Markup Language); the type of computers they support, such as server clusters or mobile phones; the query language used, such as SQL (Structured Query Language) or XQuery; performance priorities, such as maximum scale or maximum speed; or other classification methods. Regardless of the classification method used, some DBMSs can cross categories, for example, supporting multiple query languages simultaneously.
[0100] The solution of the embodiment of the present application is to introduce a log coloring package in the log printing function code corresponding to the business code, and the log coloring package includes a proxy function for coloring the log. When the business code is run, the log printing function code corresponding to the business code is called. When running to the log printing function code, the log printing function is replaced by the proxy function, so that the log corresponding to the business code is colored by the proxy function to obtain a colored log, and the colored log is stored in a log file by the log output function of the log printing function code. In the solution provided by the embodiment of the present application, an additional log processing logic is added to the log printing function code to realize the coloring of the log, that is, the coloring of the log is realized by the proxy function, without modifying the processing logic of the log printing function, so that the original content of the log corresponding to the business code can be not affected. Moreover, for the situation where the logic of some log printing functions is not allowed to be modified, log coloring can also be realized by this solution.
[0101] In an optional solution of the present application, the method further includes:
[0102] Import the log coloring package into the main package file corresponding to the business code.
[0103] Each business code has a corresponding main package file, and the main function of the business code can only be in the main package file. When the business code runs, the code in the main package file will be executed. In this solution, the log coloring package can be imported into the main package file corresponding to the business code. Not only does this not modify the logic of the log printing function, but it also ensures the implementation of the log coloring function.
[0104] In this solution, the business code can be based on the Go language. If the main package file wants to reference other codes, the other codes need to be referenced in the form of packages.
[0105] As an example, Figure 2 The code shown in Figure 2 The package main in the main package file represents the main package file. The part of the main package file that is highlighted by a wireframe is the content corresponding to the log coloring package. The rest of the content is the original code in the main package file (as shown in the ellipsis in the figure).
[0106] Next, when the program reaches the log printing function code (i.e., fun main()), and the log coloring and collection switch is turned on (i.e., when config.Config.LogTrace = true), the log corresponding to the business code is colored based on the proxy function, generating a colored log. This colored log is then output to a log file using the log output function (logrus.LogToConsole(b: true) shown in the figure). In this example, the log file can be the file corresponding to the console. Figure 2 The part selected by the wireframe is the colored log, where OK is the content corresponding to the original log, and INFO
[0000] , type, and logcolored are additional attribute tags added when the log is colored, that is, setting tags.
[0107] In an optional solution of the present application, the method further includes:
[0108] Call the log printing function to print the log corresponding to the business code through the log printing function;
[0109] Output the log corresponding to the business code by calling the log output function.
[0110] Since the logic of the log printing function is not modified, this alternative solution can print out the original log. In other words, the original log can be obtained by calling the log printing function while obtaining the dyed log. This does not affect the output of the original log.
[0111] In an optional solution of the present application, after obtaining the output logs, i.e., the original logs and the colored logs, the output logs can be processed accordingly based on a pre-defined processing strategy. For example, the operating status of the business code can be analyzed based on the output logs. These output logs can also be provided to technical personnel for subsequent analysis.
[0112] like Figure 2 In the sample code shown, calling the log printing function log.Println() can output the content corresponding to the original log. Figure 2 The underlined part is the original output log.
[0113] In an optional solution of the present application, the log output function includes a first log output function and a second log output function;
[0114] Output the log corresponding to the business code by calling the log output function, including:
[0115] Output the log corresponding to the business code by calling the first log output function;
[0116] Output the dye log by calling the log output function of the log printing function code, including:
[0117] Output the dye log by calling the second log output function.
[0118] Among them, the original log and the colored log can be output through the same log output function, that is, the log output function log.Println() of the log printing function code, or the log output function can be divided into two functions, that is, the first log output function and the second log output function, so that the colored log and the original log are output respectively through these two functions, so that the output of the two logs does not affect each other.
[0119] like Figure 2 In the sample code shown in , the second log output function for outputting the dyed log is logrus.LogToConsole, and the first log output function for outputting the original log is log.Println().
[0120] In an optional solution of the present application, the log file includes a first file and a second file, and the log corresponding to the business code is output by calling the first log output function, including:
[0121] Output the log corresponding to the business code to the first file by calling the first log output function;
[0122] Output the dye log by calling the second log output function, including:
[0123] The dyeing log is output to the second file by calling the second log output function.
[0124] Among them, the dyeing log and the original log can be output to different log files respectively, that is, the dyeing log is output to the second file, and the original log is output to the first file. The first file and the second file are different log files. For example, the first file can be the default log file. Usually, the output logs will be output to the first file by default. The second file can be a log file independent of the first file specified based on actual needs. The second file can only be used for the collection of dyeing logs so that subsequent related analysis and processing can be performed based on the dyeing logs.
[0125] In an optional solution of the present application, if the log coloring package further includes a custom coloring code segment, wherein the custom coloring code segment includes a business parameter corresponding to the business code; the method further includes:
[0126] By running the custom coloring code segment, the parameter information corresponding to the business parameters is obtained;
[0127] The dyeing log also includes business parameters and parameter information corresponding to the business parameters.
[0128] Among them, while coloring the log, you can also obtain parameter information corresponding to the business parameters based on the custom coloring code segment. The parameter information of the business parameters corresponding to different business codes may be different, and the business parameters can reflect the running status of the business code. Therefore, the coloring log includes business parameters and parameter information corresponding to the business parameters, which can enable you to perform effective analysis of different business codes based on the parameter information of the business parameters in the coloring log when analyzing the coloring logs corresponding to different business codes.
[0129] Among them, the business parameters can be pre-configured in the custom dyeing code segment based on the actual needs of the user. For example, the business parameter can be a user ID and / or a full-link tracking ID TraceID. If the business parameter is a user ID, then when running the custom dyeing code segment, the specific identification information of the user ID corresponding to the business code can be obtained. Through the user ID, you can know which user initiated the request to make the business code run, that is, which business the log corresponds to. If the business parameter is TraceID, then when running the custom dyeing code segment, you can get the specific information of the TraceID corresponding to the business code, and the corresponding logs in the current thread all correspond to the same TraceID, then based on the TraceID, you can also distinguish which business the log corresponds to.
[0130] In the solution of the present application, the specific form of the service parameters is not limited, and any information that can distinguish services can be used as service parameters.
[0131] As an example, Figure 3 The code shown in Figure 3 middle, Figure 3The custom coloring field shown in the figure is the custom coloring code segment. In this code segment, the business parameter is defined through coloring.CustomFields = map[string]func()interface{}. In this example, the business parameter is TraceID. The definition can obtain the business parameter TraceID when obtaining the coloring log, as shown in the return coloring.GetTraceID in the figure. When the custom coloring code segment is run, that is, when the TraceID is initialized, the coloring log can be printed through the log printing function log.Println(v...:'ok'). This coloring log is shown as INFO[OOOO]OK type "logcolored" in the figure. This coloring log also includes the TraceID and the parameter information corresponding to the TraceID, as shown in the part marked with the downward line in the figure. The coloring log is printed through the log printing function log.Println(v...:'ok1'). This coloring log is shown as INFO[OOOO]OK1 type "logcolored" in the figure. This coloring log also includes the TraceID and the parameter information corresponding to the TraceID, as shown in the part marked with the downward line in the figure. In these two dyeing logs, the business parameters and the parameter information corresponding to the business parameters are the same.
[0132] In an optional solution of this application, the log printing function is replaced with a proxy function, including:
[0133] Use dynamic instrumentation to replace the log printing function with a proxy function.
[0134] Among them, the role of dynamic instrumentation is to dynamically replace one function with another function when the code is running. Therefore, in this solution, the dynamic instrumentation technology can be used to replace the log printing function with the proxy function.
[0135] To better illustrate the implementation and effect of this optional solution of the present application, the following specifically introduces the principle of dynamic instrumentation:
[0136] See also Figure 4 The example code shown in the figure is a call to a common function a. This code mainly calls function a and implements the corresponding function of a. In the main file package package main, the function a's corresponding return 1 (return1 shown in the figure) is defined through fun a()int{return1}. When function a is run, that is, func main() shown in the figure, function a can be called and return 1.
[0137] Will Figure 4After disassembling the code in , we can get that the calling instruction of function a is the CALL instruction.
[0138] Next, see Figure 5 The code example shows that function f replaces function a. From the code, we can see that function a is defined to return 1 (return1 shown in the figure) through var a=func()int t{return1}, and function f is defined to return 2 (return2 shown in the figure) through var f=func()int t{return2}. In the main function (func main shown in the figure), a=f is defined. Therefore, when function a is executed, function f is actually called.
[0139] based on Figure 4 The calling method of function a in the code requires manual rewriting to modify the calling statement of function a so that when function a is called, function f is actually called. All places where function a is called need to be modified. The modification workload is relatively high and it is not easy to get the specific location of the call. Therefore, in order to avoid manually modifying the calling statement of function a, the calling statement of function a can be dynamically modified when the code is running. Figure 6 Modify the code as shown in .
[0140] based on Figure 6 In the code shown in the figure, the function a is defined to jump to the function f through func a()int{return f()}, and the function f is defined to return 2 (return2 shown in the figure) through var f=func()int t{return2}. Based on func a()int{return f()}, it can be seen that in this sample code, the call statement of function a is modified, and the assembly instruction for calling function f is moved to the inside of function a ( Figure 6 As shown in func a()int{returnf()}), for example, at the beginning of the instruction area of function a, function f can also replace function a, and only one position needs to be modified, that is, the place where function a is defined ( Figure 6 When function a is executed, function a is still executed (func main(){a()} shown in the figure), but this time, function a is replaced by function f. Function f corresponds to return 2. Therefore, when function a is executed, function f is actually executed.
[0141] In the pair Figure 6After disassembling the code shown in , we get the code corresponding to the disassembly of the call to function f: f(), where the assembly instruction for calling function f is:
[0142] mov rdx, [absolute value address of function] / / Put the proxy function address into the register. This address value is a value pointing to the function address.
[0143] jmp qword PTR[rdx] / / jump to the address pointed to by rdx;
[0144] Move the above two lines of instructions into function a. When function a is called, the above two lines of calling instructions are executed to call function f.
[0145] The calling instruction for calling function f can be either CALL or JMP. However, if CALL is used, function f needs to be copied to register rdx, and then called from the register using CALL. If JMP is used, function f does not need to be copied to the register. Instead, the address corresponding to function f (the absolute value address of the function) needs to be copied to the register. Then, based on this address, the function f corresponding to the address can be automatically called.
[0146] like Figure 7 The schematic diagram of part of the code corresponding to the dynamic instrumentation principle shown in Figure 7 The part marked by the downward line shown in the figure generates the assembly instruction for calling function f, finds the instruction area position of function a (the beginning of the instruction area), and replaces the instruction at the beginning of function a (the instruction corresponding to the instruction area position) with the assembly instruction for calling function f. When running to function a, function f is called by calling the assembly instruction of function f, realizing that function f replaces function a.
[0147] In an optional solution of the present application, the log printing function is replaced with a proxy function by dynamic instrumentation, including:
[0148] The dynamic instrumentation method based on JMP is used to replace the log printing function with the proxy function.
[0149] The log printing function can be called using either the JMP or CALL instruction. However, based on the working principles of these two instructions, this solution uses the JMP instruction to call the proxy function. This eliminates the need to copy the proxy function's parameters into registers; instead, the proxy function's address is simply placed into the register. The function corresponding to that address can then be called based on the proxy function's address, simplifying the program.
[0150] The following combination Figure 8The code shown describes the log coloring process of this application in detail. The specific scheme is as follows:
[0151] First, the method of this solution can be integrated into a software development kit (SDK). The SDK includes a log coloring package (the monkey package shown in the figure). This log coloring package is imported into the log printing function code corresponding to the business code (the third-party monkey package is imported as shown in the figure). The log coloring package includes a proxy function for coloring the log (proxyPrintln shown in the figure). The log printing function is log.Println shown in the figure.
[0152] The log coloring package can be imported into the main package file. The ellipsis part in the figure represents other codes in the main package file.
[0153] In this example, a custom coloring code segment (the custom coloring field shown in the figure) can also be set in the proxy function, and business parameters can be defined through the custom coloring code segment so that when the custom coloring code segment is run, the parameter information of the business parameters can be obtained, so that the obtained coloring log also includes the business parameters and the parameter information corresponding to the business parameters.
[0154] Then, when the business code is running, the log printing function code corresponding to the business code is called. When running to the log printing function code, the log printing function is replaced with a proxy function (dynamically inserting the log function as shown in the figure). When calling the log printing function, it is actually calling the proxy function (the proxy function of the log function shown in the figure). The proxy function can be used to color the log corresponding to the business code and obtain the parameter information of the business parameters at the same time. Among them, the log function shown in the figure is the log printing function mentioned in this example.
[0155] Finally, the second log output function (logrus.Withalias shown in the figure) can be called to output the colored log to the corresponding log file (the second file described above), and the original log can be output to the first file through the first log output function (logger.Output shown in the figure).
[0156] In this example, after obtaining the dyed log, the dyed log can also be reported to the Kafka system, that is, the log file can also be the Kafka system, so as to facilitate subsequent related asynchronous analysis and processing based on the dyed log.
[0157] It should be noted that the above solution is not limited to the log framework corresponding to the Go language, but is also applicable to other log frameworks. It only requires dynamic instrumentation of key log printing functions, and a similar method is used to proxy the log printing function to proxyPrintln, which can then be adapted by the log collection platform. Therefore, this solution has good universality.
[0158] The advantages of this solution are as follows:
[0159] 1. It can be used as a log coloring and collection solution for third-party platforms. You only need to import the log coloring SDK that integrates this solution. There is no need to rely on or require business developers to add coloring fields through hard-coding functions (replacing each place that needs to be printed). This can decouple the log collection and business function codes of third-party platforms, reducing development and implementation costs.
[0160] 2. It can be used as a tool for log data collection. It can dynamically collect any line of logs for printing for subsequent data analysis. After integrating the log coloring function code into the log coloring SDK, more collection rules can be added without re-publishing the business or restarting the service. The output log can be reported as the basis for subsequent log analysis.
[0161] 3. Based on the log coloring scheme of this application, two outputs can be provided, one is the colored log and the other is the original log. In this way, the log corresponding to the business code can be colored without interfering with the original log.
[0162] Based on Figure 1 The same principle as the method shown in , the embodiment of the present application also provides a log dyeing device 20, such as Figure 9 As shown in , the log coloring device 20 may include a code calling module 210, a log coloring module 220 and a log output module 230, wherein:
[0163] The code calling module 210 is used to call the log printing function code corresponding to the business code when running the business code. The log printing function code includes a log printing function, a log output function and an imported log coloring package. The log coloring package includes a proxy function for coloring the log;
[0164] The log coloring module 220 is used to replace the log printing function with the proxy function when running to the log printing function code, so as to color the log corresponding to the business code through the proxy function to obtain a colored log;
[0165] The log output module 230 is used to output the dyeing log by calling the log output function of the log printing function code.
[0166] In the solution of the embodiment of the present application, the log printing function code corresponding to the business code introduces a log coloring package, and the log coloring package includes a proxy function for coloring the log. When the business code is running, the log printing function code corresponding to the business code is called. When the log printing function code is run, the log printing function is replaced with the proxy function, so that the log corresponding to the business code is colored by the proxy function to obtain a colored log, and the colored log is output by calling the log output function of the log printing function code. In this solution, additional log processing logic is added to the log printing function code to achieve the coloring of the log, that is, the coloring of the log is achieved through the proxy function, without modifying the processing logic of the log printing function, so that the original content of the log corresponding to the business code can be not affected. Moreover, for some situations where the logic of the log printing function is not allowed to be modified, log coloring can also be achieved through this solution.
[0167] Optionally, if the log coloring package further includes a custom coloring code segment, wherein the custom coloring code segment includes a business parameter corresponding to the business code; the device further includes:
[0168] The custom module is used to obtain parameter information corresponding to the business parameters by running the custom coloring code segment; wherein the coloring log also includes the business parameters and the parameter information corresponding to the business parameters.
[0169] Optionally, the device further includes:
[0170] The coloring package import module is used to import the log coloring package into the main package file corresponding to the business code.
[0171] Optionally, when the log printing function is replaced by a proxy function, the log coloring module is used to:
[0172] Use dynamic instrumentation to replace the log printing function with a proxy function.
[0173] Optionally, when the log coloring module uses dynamic instrumentation to replace the log printing function with a proxy function, it is specifically used to:
[0174] The dynamic instrumentation method based on JMP is used to replace the log printing function with the proxy function.
[0175] Optionally, the device further includes:
[0176] The original log printing module is used to call the log printing function to print the log corresponding to the business code through the log printing function; and output the log corresponding to the business code by calling the log output function.
[0177] Optionally, the log output function includes a first log output function and a second log output function;
[0178] The original log printing module is used to output the log corresponding to the business code by calling the log output function.
[0179] Output the log corresponding to the business code by calling the first log output function;
[0180] When the log output module outputs the dye log by calling the log output function of the log printing function code, it is specifically used to:
[0181] Output the dye log by calling the second log output function.
[0182] Optionally, when the log output module outputs the colored log by calling the log output function of the log printing function code, it is specifically used to:
[0183] By calling the log output function of the log printing function code, the dyeing log is output to the log file.
[0184] Optionally, the log file includes a first file and a second file;
[0185] When the original log printing module outputs the log corresponding to the business code by calling the first log output function, it is specifically used to:
[0186] Output the log corresponding to the business code to the first file by calling the first log output function;
[0187] When the log output module outputs the dye log by calling the second log output function, it is specifically used to:
[0188] The dyeing log is output to the second file by calling the second log output function.
[0189] Since the log coloring device provided in the embodiments of the present application is a device that can execute the log coloring method in the embodiments of the present application, based on the log coloring method provided in the embodiments of the present application, those skilled in the art can understand the specific implementation of the log coloring device in the embodiments of the present application and its various variations. Therefore, how the log coloring device implements the log coloring method in the embodiments of the present application will not be described in detail here. As long as those skilled in the art implement the log coloring method in the embodiments of the present application using the log coloring device, they are within the scope of protection of this application.
[0190] Based on the same principles as the log coloring method and log coloring device provided in the embodiments of the present application, the embodiments of the present application also provide an electronic device, which may include a processor and a memory. The memory stores readable instructions, which, when loaded and executed by the processor, can implement the method shown in any of the embodiments of the present application.
[0191] As an example, Figure 10 FIG. 4 shows a schematic diagram of the structure of an electronic device 4000 to which the solution of an embodiment of the present application is applicable. Figure 10 As shown, the electronic device 4000 includes: a processor 4001 and a memory 4003. The processor 4001 and the memory 4003 are connected, for example, via a bus 4002. Optionally, the electronic device 4000 may further include a transceiver 4004. It should be noted that in actual applications, the number of transceivers 4004 is not limited to one, and the structure of the electronic device 4000 does not constitute a limitation on the embodiments of the present application.
[0192] Processor 4001 may be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It may implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. Processor 4001 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, and the like.
[0193] Bus 4002 may include a path for transmitting information between the above components. Bus 4002 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. Bus 4002 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 10 Only one thick line is used in the diagram, but this does not mean that there is only one bus or one type of bus.
[0194] The memory 4003 may be a ROM (Read Only Memory) or other types of static storage devices that can store static information and instructions, a RAM (Random Access Memory) or other types of dynamic storage devices that can store information and instructions, or an EEPROM (Electrically Erasable Programmable Read Only Memory), a CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, optical disk storage (including compact discs, laser discs, optical discs, digital versatile discs, Blu-ray discs, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited to these.
[0195] The memory 4003 is used to store application code for executing the solution of the present application, and the execution is controlled by the processor 4001. The processor 4001 is used to execute the application code stored in the memory 4003 to implement the content shown in any of the above method embodiments.
[0196] It should be understood that although the steps in the flowcharts of the accompanying drawings are shown in sequence as indicated by the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some of the steps in the flowcharts of the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a portion of the sub-steps or stages of other steps.
[0197] The above are only some of the implementation methods of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A log dyeing method, characterized in that, include: When the business code is running, the log printing function code corresponding to the business code is called, and the log printing function code includes a log printing function, a log output function and an introduced log coloring package, and the log coloring package includes a proxy function for coloring the log; When running to the log printing function code, the log printing function is replaced by the proxy function, so as to color the log corresponding to the business code through the proxy function to obtain a colored log; Output the dyeing log by calling the log output function of the log printing function code; If the log coloring package further includes a custom coloring code segment, the custom coloring code segment includes a business parameter corresponding to the business code; the method further includes: Obtain parameter information corresponding to the business parameters by running the custom coloring code segment; Based on the parameter information corresponding to the business parameter, the business code is analyzed by a server responsible for business code analysis to obtain an analysis result; The dyeing log also includes the business parameters and parameter information corresponding to the business parameters.
2. The method according to claim 1, characterized in that The method further comprises: Import the log coloring package into the main package file corresponding to the business code.
3. The method according to any one of claims 1 to 2, characterized in that The step of replacing the log printing function with the proxy function includes: The log printing function is replaced by the proxy function by adopting a dynamic instrumentation method.
4. The method according to claim 3, characterized in that The method of replacing the log printing function with the proxy function by adopting a dynamic instrumentation method includes: The log printing function is replaced by the proxy function by using a dynamic plugging method based on jmp.
5. The method according to any one of claims 1 to 2, characterized in that The method further comprises: Calling the log printing function to print the log corresponding to the business code through the log printing function; The log corresponding to the business code is output by calling the log output function.
6. The method according to claim 5, characterized in that The log output function includes a first log output function and a second log output function; Outputting the log corresponding to the business code by calling the log output function includes: Output the log corresponding to the business code by calling the first log output function; Outputting the dyeing log by calling the log output function of the log printing function code includes: The dyeing log is output by calling the second log output function.
7. The method according to claim 6, characterized in that Outputting the dyeing log by calling the log output function of the log printing function code includes: By calling the log output function of the log printing function code, the dyeing log is output to a log file.
8. The method according to claim 7, characterized in that The log file includes a first file and a second file; Outputting the log corresponding to the business code by calling the first log output function includes: Output the log corresponding to the business code to the first file by calling the first log output function; Outputting the dyeing log by calling the second log output function includes: The dyeing log is output to the second file by calling the second log output function.
9. A log dyeing device, characterized in that: include: A code calling module is used to call the log printing function code corresponding to the business code when running the business code. The log printing function code includes a log printing function, a log output function and an imported log coloring package. The log coloring package includes a proxy function for coloring the log; A log coloring module, configured to replace the log printing function with the proxy function when running to the log printing function code, so as to color the log corresponding to the business code through the proxy function to obtain a colored log; A log output module, configured to output the dyeing log by calling a log output function of the log printing function code; If the log coloring package further includes a custom coloring code segment, the custom coloring code segment includes the business parameters corresponding to the business code; the log coloring package further includes a custom module, and the custom module is used to: Obtain parameter information corresponding to the business parameters by running the custom coloring code segment; Based on the parameter information corresponding to the business parameter, the business code is analyzed by a server responsible for business code analysis to obtain an analysis result; The dyeing log also includes the business parameters and parameter information corresponding to the business parameters.
10. The log dyeing device according to claim 9, characterized in that: The log coloring device further includes a coloring package importing module, which is used to: Import the log coloring package into the main package file corresponding to the business code.
11. The log dyeing device according to any one of claims 9 to 10, characterized in that: When the log printing function is replaced by the proxy function, the log coloring module is specifically used to: The log printing function is replaced by the proxy function by adopting a dynamic instrumentation method.
12. The log dyeing device according to claim 11, characterized in that: When the log coloring module replaces the log printing function with the proxy function in a dynamic instrumentation manner, the log coloring module is specifically used to: The log printing function is replaced by the proxy function by using a dynamic plugging method based on jmp.
13. The log dyeing device according to any one of claims 9 to 10, characterized in that: The log coloring device further includes an original log printing module, which is used to: Calling the log printing function to print the log corresponding to the business code through the log printing function; The log corresponding to the business code is output by calling the log output function.
14. The log dyeing device according to claim 13, characterized in that: The log output function includes a first log output function and a second log output function; When the original log printing module outputs the log corresponding to the business code by calling the log output function, it is specifically used to: Output the log corresponding to the business code by calling the first log output function; When the log output module outputs the dyeing log by calling the log output function of the log printing function code, it is specifically used to: The dyeing log is output by calling the second log output function.
15. The log dyeing device according to claim 14, characterized in that: When the log output module outputs the dyeing log by calling the log output function of the log printing function code, it is specifically used to: By calling the log output function of the log printing function code, the dyeing log is output to a log file.
16. The log dyeing device according to claim 15, characterized in that The log file includes a first file and a second file; When the original log printing module outputs the log corresponding to the business code by calling the first log output function, it is specifically used to: Output the log corresponding to the business code to the first file by calling the first log output function; When the log output module outputs the dyeing log by calling the second log output function, the log output module is specifically configured to: The dyeing log is output to the second file by calling the second log output function.
17. An electronic device comprising a memory, a processor, and a computer program stored in the memory, wherein: The processor executes the computer program to implement the steps of the method according to any one of claims 1 to 8.
18. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 8 are implemented.
Citation Information
Patent Citations
Log highlighting method and device, storage medium and Android terminal
CN108959199A
Method and device for obtaining log, storage medium, and electronic device
CN109376074A