A method and device for integrating multiple log modules
By creating log classes and log center modules, defining a unified log output interface and log collection interface, the problem that different log modules cannot be viewed in a unified manner is solved, and the compatibility management of log modules and unified processing of log content is realized.
Patent Information
- Application Number
- CN202010484339.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-01
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2040-06-01
AI Technical Summary
In the prior art, the logs of different log modules cannot be viewed in a unified manner, resulting in compatibility issues.
By creating log classes and log center modules, defining a unified log output interface and log collection interface, the compatibility management of many different log modules is achieved.
It realizes unified management of different log modules, facilitates viewing and processing of log content, and enhances the compatibility of log modules.
Smart Images

Figure CN113760844B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development, and in particular to a method and device for fusing multiple log modules. Background Art
[0002] Existing programs include a variety of open source modules, all of which have logging functions. The logging function is used to record the process of program operation. Logs play an important role in program debugging, error or exception location, and data analysis.
[0003] However, different modules use different logs. Some modules use the glog log module, some use the xlog log module, and some use the zlog log module. When viewing logs, you can only view them one by one according to the log modules corresponding to different modules, and cannot view them all at once.
[0004] Therefore, how to make different types of log modules compatible is a technical problem that needs to be solved urgently. Summary of the invention
[0005] In view of the above problems, the present invention is proposed to provide a method and device for integrating multiple log modules to overcome the above problems or at least partially solve the above problems.
[0006] In a first aspect, the present invention provides a method for integrating multiple log modules, which is applied to the operation of a program including multiple different log modules, and includes:
[0007] Based on the display timing of the logs during program operation, the log levels of various log contents are classified to obtain four log levels, including INFO log level, WARNING log level, ERROR log level and FATAL log level. The various log contents belong to different log modules respectively.
[0008] Marking the log levels corresponding to the multiple different log contents, and marking the name of the log module to which each log content belongs;
[0009] Create a log class, define a log output interface in the log class, the log output interface is a log output interface uniformly corresponding to the multiple different log modules, and the log output interface includes: a console display interface of the log, a file writing interface of the log, and a network transmission interface of the log;
[0010] Create a log center module, define a log collection interface in the log center module, the log collection interface corresponds to the log output interface one by one, and the log collection interface includes an interface for displaying logs on a console, an interface for writing logs to a file, and an interface for transmitting logs over a network;
[0011] When the preset log output function is uniformly executed on the various log modules, the various log contents are forwarded to the log center module by calling the corresponding log output interface, so that the corresponding log collection interface in the log center module receives the various log contents and uniformly executes the preset log output function on the various log contents.
[0012] Further,
[0013] The log class is created as: class AdapterLog{AdapterLog(string logname)m_logname(logname), where logname is the name of the log module;
[0014] The console display interface of the log is defined as: Void consolelog (int level, string log message);
[0015] The file writing interface of the log is defined as: Void filelog (int level, string logmessage);
[0016] The network transmission interface of the log is defined as: Void networklog(int level, string logmessage);
[0017] Among them, level is the log level, and logmessage is the log content.
[0018] Further,
[0019] The log center module is created as: class LogCenter;
[0020] The interface for displaying logs to the console is defined as: Void consolelog(string mode, int level, string logmessage);
[0021] The interface for writing logs to files is defined as: Void filelog (string mode, int level, string logmessage);
[0022] The interface for transmitting logs over the network is defined as: Void networklog(string mode, intlevel, string logmessage);
[0023] Among them, mode is the log module to which the log content belongs, level is the log level, and logmessage is the log content.
[0024] Furthermore, it also includes:
[0025] In the log class, a plurality of switch interfaces of different log modules are defined, and the switch interface is defined as Boollogenable(int level), wherein the level is the log level;
[0026] A switch instruction collection interface is defined in the log center module. The switch instruction collection interface corresponds to the switch interface and is used to execute a switch function on a specified log module, or to execute a switch function on a log content of a preset log level in a specified log module. The switch instruction collection interface is defined as Bool logenable (string mode, int level), wherein the mode is the log module to which the log content belongs, and the level is the log level.
[0027] Furthermore, it also includes:
[0028] A plurality of different log module cache interfaces are defined in the log class, and the cache interface is defined as Voidflush(). The cache interface is used to write the log content corresponding to the cache of each log module into the log center module when the program exits, so that the log center module executes the function of writing the cached log content to a file or transmitting the cached log over the network.
[0029] Furthermore, it also includes:
[0030] When any one of the plurality of different log modules is loaded before the log center module is loaded, an exception is registered in the log center module;
[0031] The log center module is used to capture exceptions and control the program termination.
[0032] Furthermore, it also includes:
[0033] When any one of the multiple different log modules is loaded after the log center module is loaded, the exception capture of the multiple different log modules is closed.
[0034] In a second aspect, the present invention further provides a device for integrating multiple log modules, which is applied to the operation of a program including multiple different log modules, including:
[0035] A classification module is used to classify the log levels of various log contents based on the display timing of the logs during program operation to obtain four log levels, wherein the four log levels include: INFO log level, WARNING log level, ERROR log level and FATAL log level, and the various log contents belong to different log modules respectively;
[0036] A marking module, used to mark the log levels corresponding to the multiple different log contents, and mark the name of the log module to which each log content belongs;
[0037] A log class creation module, used to create a log class, in which a log output interface is defined, and the log output interface is a log output interface uniformly corresponding to the multiple different log modules, and the log output interface includes a console display interface of the log, a file writing interface of the log, and a network transmission interface of the log;
[0038] A log center module creation module, used to create a log center module, define a log collection interface in the log center module, the log collection interface corresponds to the log output interface one by one, and the log collection interface includes an interface for displaying logs on a console, an interface for writing logs to a file, and an interface for transmitting logs over a network;
[0039] A unified execution module is used to forward various different log contents to the log center module by calling corresponding log output interfaces when uniformly executing the preset log output function for the various different log modules, so that the corresponding log collection interfaces in the log center module receive the various different log contents and uniformly execute the preset log output function for the various different log contents.
[0040] In a third aspect, the present invention provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned method steps when executing the program.
[0041] In a fourth aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, which implements the above-mentioned method steps when executed by a processor.
[0042] One or more technical solutions in the embodiments of the present invention have at least the following technical effects or advantages:
[0043] The present invention provides a method for fusing multiple log modules, which is applied to the operation of a program including multiple different log modules, including: based on the display timing of the log during the operation of the program, classifying the log levels of multiple different log contents to obtain four types of log levels, the four types of log levels include: INFO log level, WARNING log level, ERROR log level and FATAL log level, and the multiple different log contents belong to different log modules respectively; marking the log levels corresponding to the multiple different log contents respectively, and marking the name of the log module to which each log content belongs; creating a log class, defining a log output interface in the log class, the log output interface is a log output interface uniformly corresponding to the multiple different log modules, the log output interface includes a console display interface for the log, a file writing interface for the log and a network transmission interface for the log; creating a log center module, in the log center module A log collection interface is defined in the block, and the log collection interface corresponds to the log output interface one by one. The log collection interface includes an interface for displaying logs on a console, an interface for writing logs to files, and an interface for transmitting logs over a network. When a preset log output function is uniformly executed for a variety of different log modules, a variety of different log contents are forwarded to the log center module by calling the corresponding log output interface. The various different log contents belong to different log modules respectively, so that the corresponding log collection interface in the log center module receives a variety of different log contents, and uniformly executes the preset log output function for the various different log contents. By making slight modifications to the original log module, unifying the format of the log level, and adding a log center module, the log center module manages the various log modules uniformly while retaining the original functions, thereby making the different log modules compatible with each other.
[0044] Furthermore, if a new log module is added to the program, the new log module can also be compatible. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present invention. Also, throughout the accompanying drawings, the same reference figures are used to represent the same components. In the drawings:
[0046] Figure 1 A schematic diagram of the steps of a method for integrating multiple log modules in Embodiment 1 of the present invention is shown;
[0047] Figure 2 A schematic diagram showing the structure of a device for integrating multiple log modules in Embodiment 2 of the present invention is shown;
[0048] Figure 3 A schematic diagram of the structure of an electronic device implementing a method for integrating multiple log modules in Embodiment 3 of the present invention is shown. DETAILED DESCRIPTION
[0049] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.
[0050] The present invention is applied to program programming. Various open source modules are used in existing program programming. Each open source module has a log module. Some modules use the glog log module, some modules use the zlog log module, and some modules use the xlog log module. These log modules are of different format types and are written into different files respectively when writing files, which is not convenient for viewing.
[0051] Moreover, the current log modules include glog log module, xlog log module, zlog log module, log4c log module, log4cxx log module, etc. Each log module includes log content of different log levels. Users can set the preset log level so that the logs that meet the preset log level can be written into files or printed out.
[0052] For example, for the glog log module, there are INFO log level, WARNING log level, ERROR log level, and FATAL log level. For general output logs, the log content of the INFO log level is output, for some logs that may have risks, the log content of the WARNING log level is output, and when there is an error code in the program, the log content of the ERROR log level is output.
[0053] For the INFO log level, use the interface LOG(INFO)<<"info message" provided by the log library, and for the error log level, use the interface LOG(ERROR)<<"error message" provided by the log library. For example, when executing a program, in order to locate the problem, the user only needs to read the log content of the ERROR log level, but not the INFO log level. In this case, the output level of the log can be specified through the interface SetLog(ERROR).
[0054] For the log4cxx log module, the interface it provides is LOG_INFO(message), so it is different from the interface provided by the above-mentioned glog log module.
[0055] For the zlog log module, first initialize a category c=zlog_get_category("category"), and print the log zlog_info(c, "hello, zlog") based on the category.
[0056] The log library provides multiple log levels and corresponding log writing interfaces. The interfaces corresponding to each log level are different, and the log level types contained in different log modules are also different. Therefore, when there are multiple log modules, their output cannot be uniformly controlled.
[0057] The present invention adds a log output interface and a log center module to the original log module. The log center module manages different log modules in a unified manner, and can uniformly control the output of log contents in different log modules. At the same time, all log contents are written into one file, which is convenient for viewing the log contents.
[0058] like Figure 1 As shown, the present invention provides a method for integrating multiple log modules, which is applied to the operation of a program including multiple different log modules, including:
[0059] S101, based on the display timing of the log during program running, the log levels of various log contents are classified, and the various log contents belong to different log modules respectively, to obtain four types of log levels, which include: INFO log level, WARNING log level, ERROR log level, and FATAL log level.
[0060] S102: marking the log levels corresponding to the various log contents, and marking the name of the log module to which each log content belongs.
[0061] S103, create a log class, define a log output interface in the log class, the log output interface is a log output interface that corresponds to a variety of different log modules, and the log output interface includes a console display interface for the log, a file writing interface for the log, and a network transmission interface for the log.
[0062] S104, creating a log center module, defining a log collection interface in the log center module, the log collection interface corresponds to the log output interface one by one, and the log collection interface includes an interface for displaying the log on a console, an interface for writing the log to a file, and an interface for transmitting the log over a network;
[0063] S105, when the preset log output function is uniformly executed for multiple different log modules, the various different log contents are forwarded to the log center module by calling the corresponding log output interface, so that the corresponding log collection interface in the log center module receives the various different log contents, and uniformly executes the preset log output function for the various different log contents.
[0064] In an optional implementation, in S101 and S102, the log levels of different log contents are summarized and classified so that different log levels belong to preset log level categories and are marked, which facilitates unified management and provides a guarantee for compatibility control.
[0065] Specifically, in the original log modules, the division of log levels corresponding to the respective log modules is different.
[0066] For example, the glog log module includes four log levels: INFO log level, WARNING log level, ERROR log level, and FATAL log level.
[0067] The zlog log module includes 3 log levels, and the log4cpp log module includes 5 log levels.
[0068] When classifying the log levels of various log contents, the classification is based on the display timing of the logs during program operation, as follows:
[0069] The log contents of INFO log level and WARNING log level are displayed by the program in debug mode. The log contents of ERROR log level and FATAL log level are displayed by the program in release mode. The log contents of FATAL log level will directly lead to the termination of the program.
[0070] Therefore, the above four log levels are divided into three categories: one is that the program is displayed in debug mode but not in release mode, one is that the program is displayed in both debug mode and release mode, and one is that it can cause the program to terminate.
[0071] According to the above rules, the zlog log module itself includes three log levels: DEBUG log level, NOTICE log level, and ERROR log level. Since the log content of the DEBUG log level and the log content of the NOTICE log level are both displayed in debug mode and will not be displayed in release mode, the log content of these two log levels is divided into the INFO log level and marked as INFO log level.
[0072] Since the ERROR log level will cause the program to terminate directly, this log level is classified as the FATAL log level and marked as the FATAL log level.
[0073] For the five log levels in the log4cpp log module: INFO log level, DEBUG log level, WARNING log level, ERROR log level, and DEAD log level, according to the above rules, the log content of the INFO log level and the log content of the DEBUG log level can be divided into the INFO log level; the log content of the WARNING log level can be divided into the WARNING log level; the log content of the ERROR log level can be divided into the ERROR log; the log content of the DEAD log level can be divided into the FATAL log level.
[0074] When marking the log level, the name of the log module corresponding to the log content of the log level is also marked.
[0075] By unifying the naming of log levels, log contents of different log levels in different log modules can be compatible.
[0076] Next, execute S103 to create a log class. The log class is created as class AdapterLog{AdapterLog(string logname)m_logname(logname), where logname is the name of the log module. The log class is used to encapsulate the relevant functional interfaces of the log. When performing functional control on each log module, the corresponding log module is found based on the name of the log module and the corresponding functional control is performed. The creation of the log class is to add the content of the log class to the original program. The created log class is used to encapsulate the log output functional interfaces of different log modules.
[0077] Then, a log output interface is defined in the log class, which is a log output interface that corresponds to a variety of different log modules. The log output interface includes: a console display interface for logs, a file writing interface for logs, and a network transmission interface for logs. The log output interface defined in the log class is also a new addition.
[0078] The console display interface of the log is defined as: Void consolelog(int level, string logmessage); the file writing interface of the log is defined as: Void filelog(int level, string logmessage); the network transmission interface of the log is defined as: Void networklog(int level, string logmessage); where level is the log level and logmessage is the log content. The console display interface of the log is used for console display of the log, the file writing interface of the log is used for file writing of the log, and the network transmission interface of the log is used for network transmission of the log.
[0079] In S104, a log center module is created, in which a log collection interface is defined. The log collection interface corresponds one-to-one with the log output interface. The log collection interface includes an interface for displaying logs on a console, an interface for writing logs to files, and an interface for transmitting logs over a network.
[0080] The log center module is used to uniformly manage the log functions of each log module, thereby achieving compatible management of multiple different log modules.
[0081] Among them, the log center module is created as: class LogCenter; the interface for displaying logs to the console is defined as: Void consolelog(string mode,int level,string logmessage); the interface for writing logs to files is defined as: Void filelog(string mode,int level,string logmessage); the interface for network transmission of logs is defined as: Void networklog(string mode,int level,stringlogmessage); mode is the log module to which the log content belongs, level is the log level, and logmessage is the log content.
[0082] Specifically, during the execution of the program, S105, when the preset log output function is uniformly executed for multiple different log modules, the various different log contents are forwarded to the log center module by calling the corresponding log output interface. The various different log contents belong to different log modules respectively, so that the corresponding log collection interface in the log center module receives the various different log contents and uniformly executes the preset log output function for the various different log contents.
[0083] For the console display interface of the log, after calling the console display interface of the log, the corresponding various log contents are forwarded to the log center module, and the corresponding log collection interface in the log center module receives the various log contents, each of which carries the log module to which it belongs.
[0084] The following code is used to realize the function of displaying the various log contents in the unified execution log console:
[0085] const HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE); Get the console handle.
[0086] SetConsoleTextAttribute(stderr_handle,old_color_attrs); Sets the color of the console.
[0087] Logmessage=mode+logmessage; add the log header to the log module to which the log content belongs, so as to know which log module the log belongs to.
[0088] fwrite(message,message.size(),1,stderr) writes the log to the console for display.
[0089] For the file writing interface of the log, there is also its corresponding execution code, which will not be described in detail here.
[0090] For the network transmission interface of the log, the log content can be written into the network socket first. The specific implementation is to create a network socket, and then write the log content into it and send it through the network, and attach the log content to the log module to which it belongs.
[0091] In an optional implementation, it also includes: a switch interface of multiple different log modules is defined in the log class, and the switch interface is defined as Bool logenable (int level), wherein level is the log level.
[0092] The switch interface is used to return the result of whether the log module is turned on. The present invention distributes the result of whether the log module is turned on to the corresponding log modules in advance. By judging whether each log module is turned on, it is avoided to call the log center module and then make a judgment in the log center module, thereby improving the overall performance of the log module and avoiding invalid writing of logs. The code for judging whether the log module is turned on by calling the interface in the log center module is as follows:
[0093] Return LogCenter::Instance()->logenable(m_logname,level).
[0094] Correspondingly, a switch instruction collection interface is defined in the log center module. The switch instruction collection interface corresponds to the switch interface and is used to execute the switch function on the specified log module, or to execute the switch function on the log content of the preset log level in the specified log module. The switch instruction collection interface is defined as Bool logenable(string mode, intlevel), where mode is the log module to which the log content belongs, and level is the log level.
[0095] For example, to disable the INFO log level in the glog log module, the implementation code is as follows:
[0096] If(mode=="glog"&&level==INFO){
[0097] Return false;}else{return true}.
[0098] In an optional implementation, it also includes: a cache interface for multiple different log modules is defined in the log class, and the cache interface is defined as Void flush(). The cache interface is used to write the cached log content corresponding to each log module to the log center module when the program exits, so that the log center module can execute the function of writing the cached log content to a file or transmitting the cached log over the network.
[0099] Specifically, the cache interface Void flush() corresponds to a log cache for each log module. When the program exits, it will write the cached log content to the log file or network transmission at one time. Therefore, by providing the cache interface, the cached log content can be written to the log center module at one time, so that the log center module executes the one-time writing of the cached log content to the log file or network transmission, thereby avoiding the technical problem of the loss of the cached log content when the program exits.
[0100] When creating the log class, the log output interface is defined in the log class, and the code that executes the corresponding function in the original program code is deleted, that is, the glog log source code is modified so that the modified glog log source code can access the newly added log output interface.
[0101] When providing its log output function, the glog log source code will splice and assemble data to call the system console for display. For example, it will add the time when the log is generated, which file the log belongs to, which function the log belongs to, which line of code, etc. Therefore, when modifying the glog log source code, the function of calling the system console for display can be commented out and replaced with the log output interface defined in the log class.
[0102] Of course, when the glog log source code provides the function of writing its log files, the file creation code of the glog log source code is commented out. Since it will first create a file and then write it, the logic of its file creation is commented out, and its subsequent file writing function is modified to close its logical judgment on whether the file exists, and replace the operation of writing log files with the file writing interface of writing to the newly added log. The specific implementation is as follows:
[0103] When the glog log source code calls the system console to display, the specific source code is as follows:
[0104] void ColoredWriteToStderr(int level,string message){
[0105] const HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE); Get the console handle.
[0106] SetConsoleTextAttribute(stderr_handle,old_color_attrs); Sets the color of the console.
[0107] Fwrite(message, message.size(), 1, stderr); writes the log content to the console for display.
[0108] The modified code is as follows:
[0109] void ColoredWriteToStderr(int level,string message){
[0110] If(!logenable(level)){return;} If the log content is not enabled, it will return directly without forwarding the log content.
[0111] adapterlog.consolelog(level,message); directly calls the console display interface of the log.
[0112] Return; and return directly without executing the original logic.}
[0113] Among them, when initializing in the glog log source code, an AdapterLog adapterlog("log"); object is created.
[0114] Then, in the ColorWriteToStderr interface of the glog log source code, the control function logic will be commented out, the log output interface will be added to the entry function, and it will be returned directly without executing its original function.
[0115] For the log file writing function and the log network transmission function, the log source code can be modified in the same implementation method, which will not be described in detail here.
[0116] In an optional implementation, since the program includes multiple log modules, when an exception occurs, multiple log modules will capture the exception. If the program encounters a problem that is difficult to solve, it will cause the program to crash. Therefore, the log module will capture these exceptions and display them in the log. The exception handling module will not use the log interface provided by each log module, but can only write the error information to the log file once, thereby avoiding recursive calls to the log and falling into an infinite loop.
[0117] When an exception occurs in the program, the program will terminate. At this time, multiple log modules will handle the exception. We cannot know which log module handles the exception, nor can we know how many log modules will handle this exception.
[0118] For this situation, two strategies can be adopted. One is to register the exception at the end of the log center module, and end the program directly when the exception is caught without throwing the exception. The other is to modify the exception capture of each log module so that each log module does not capture the exception.
[0119] The usage scenario of the above two strategies is that if any of the multiple log modules is loaded before the log center module is loaded, the strategy of registering the exception at the end of the log center module is used, and the exception is captured through the log center module, and the program is controlled to end.
[0120] If any of the multiple log modules is loaded after the log center module is loaded, that is, it cannot be guaranteed that the log center module is loaded last, the exception capture functions of the multiple different log modules will be turned off.
[0121] In an optional implementation, when performing macro definitions for multiple log modules, there may be a macro interface conflict problem.
[0122] In order to facilitate developers to output logs, existing log modules usually provide some convenient macros to facilitate the writing of log functions. Multiple log modules will write log functions by defining macros. Therefore, the macros provided by these log modules will conflict, causing log module call errors when the program is executed.
[0123] For example, the glog log code uses the LOG(INFO) macro to print logs. If the same macro is also provided in other log modules, it will lead to uncertainty about which log module's function is being called when calling.
[0124] Therefore, in the present invention, a conversion is performed on the macro definition to avoid conflicts. Assuming that the LOG macro has been defined, the solution is as follows:
[0125] #ifdef LOG first determines whether the LOG macro is defined. If it is defined
[0126] #undef LOG cancels the definition of the LOG macro.
[0127] #endif judgment ends.
[0128] #define LOG(severity)severity.stream() Then, define the LOG macro. The LOG defined at this time is the macro definition that needs to be used later, thus avoiding macro conflicts.
[0129] In a preferred implementation, since the log center module will simultaneously receive the log contents forwarded by each log module, the log center module will uniformly control the implementation functions of the log modules, that is, the log center module will perform file write operations on multiple log modules at the same time, that is, there will be multiple threads writing files at the same time. In order to ensure the security of multi-threaded file writing and take into account the performance of each log file, a mutex lock can be used to avoid multi-threading problems. However, the use of a mutex lock will cause all threads to wait for the thread lock to write files, causing a performance bottleneck.
[0130] For multi-threading problems, you can also use queues to execute and write them to the file all at once when the queue is full.
[0131] For example, in debug mode, there will be a lot of log content at the INFO log level and the WARNING log level, while there will be relatively few log content at the ERROR log level and the FATAL log level. In the release mode of the program, there will be no log content at the DEBUG log level and the INFO log level, and there will be more log content at the ERROR log level. Therefore, for each running mode of the program, a log queue is designed for each log module, and when the log queue is full, it is written to the file at one time.
[0132] The specific program code is as follows:
[0133] If (Debug) {In the current DEBUG mode, it is necessary to create a log queue for the log content of the INFO log level and the log content of the WARNING log level, and set the capacity of the log queue. When the log queue is full, write the logs to the file or send them through the socket network at one time. Because the log content of the ERROR log level is small in the DUBUG mode, there is no need to design a queue for the log module of the ERROR log level}
[0134] Else{Currently in release mode, there are only log contents with ERROR log level, so you only need to create a log queue for log contents with ERROR log level}.
[0135] When the log center module itself writes logs, it cannot use the log output function of any log module, but can only use the mode of directly writing logs to files or networks. If it uses the log output function of any log module, since it uses the log framework, it will reversely call the function of the log framework, and the log framework will call the function of the log center, which will cause recursive calls to the log, resulting in an infinite loop and a degree of deadlock.
[0136] One or more technical solutions in the embodiments of the present invention have at least the following technical effects or advantages:
[0137] The present invention provides a method for fusing multiple log modules, which is applied to the operation of a program including multiple different log modules, including: based on the display timing of the log during the operation of the program, classifying the log levels of multiple different log contents to obtain four types of log levels, the four types of log levels include: INFO log level, WARNING log level, ERROR log level and FATAL log level, and the multiple different log contents belong to different log modules respectively; marking the log levels corresponding to the multiple different log contents respectively, and marking the name of the log module to which each log content belongs; creating a log class, defining a log output interface in the log class, the log output interface is a log output interface uniformly corresponding to the multiple different log modules, the log output interface includes a console display interface for the log, a file writing interface for the log and a network transmission interface for the log; creating a log center module, in the log center module A log collection interface is defined in the block, and the log collection interface corresponds to the log output interface one by one. The log collection interface includes an interface for displaying logs on a console, an interface for writing logs to files, and an interface for transmitting logs over a network. When a preset log output function is uniformly executed for a variety of different log modules, a variety of different log contents are forwarded to the log center module by calling the corresponding log output interface. The various different log contents belong to different log modules respectively, so that the corresponding log collection interface in the log center module receives a variety of different log contents, and uniformly executes the preset log output function for the various different log contents. By making slight modifications to the original log module, unifying the format of the log level, and adding a log center module, the log center module manages the various log modules uniformly while retaining the original functions, thereby making the different log modules compatible with each other.
[0138] Furthermore, if a new log module is added to the program, the new log module can also be compatible.
[0139] Embodiment 2
[0140] Based on the same inventive concept, the present invention also provides a device for integrating multiple log modules, which is applied to the operation of a program including multiple different log modules, such as Figure 2 As shown, including:
[0141] The classification module 201 is used to classify the log levels of various log contents based on the display timing of the logs during program operation to obtain four log levels, wherein the four log levels include: INFO log level, WARNING log level, ERROR log level and FATAL log level;
[0142] The marking module 202 is used to mark the log levels corresponding to the multiple different log contents, and mark the name of the log module to which each log content belongs.
[0143] A log class creation module 203 is used to create a log class, define a log output interface in the log class, and the log output interface is a log output interface uniformly corresponding to the multiple different log modules, and the log output interface includes a console display interface of the log, a file writing interface of the log, and a network transmission interface of the log;
[0144] A log center module creation module 204 is used to create a log center module, define a log collection interface in the log center module, the log collection interface corresponds to the log output interface one by one, and the log collection interface includes an interface for displaying logs on a console, an interface for writing logs to a file, and an interface for transmitting logs over a network;
[0145] The unified execution module 205 is used to forward various different log contents to the log center module by calling the corresponding log output interface when uniformly executing the preset log output function for the various different log modules, so that the corresponding log collection interface in the log center module receives the various different log contents and uniformly executes the preset log output function for the various different log contents.
[0146] Furthermore, the log class is created as: class AdapterLog{AdapterLog(string logname)m_logname(logname), where logname is the name of the log module;
[0147] The console display interface of the log is defined as: Void consolelog (int level, string log message);
[0148] The file writing interface of the log is defined as: Void filelog (int level, string logmessage);
[0149] The network transmission interface of the log is defined as: Void networklog(int level, string logmessage);
[0150] Among them, level is the log level, and logmessage is the log content.
[0151] Furthermore, the log center module is created as: class LogCenter;
[0152] The interface for displaying logs to the console is defined as: Void consolelog(string mode, int level, string logmessage);
[0153] The interface for writing logs to files is defined as: Void filelog (string mode, int level, string logmessage);
[0154] The interface for transmitting logs over the network is defined as: Void networklog(string mode, intlevel, string logmessage);
[0155] Among them, mode is the log module to which the log content belongs, level is the log level, and logmessage is the log content.
[0156] Furthermore, the log class creation module is further used to define a plurality of switch interfaces of different log modules in the log class, and the switch interface is defined as Bool logenable (int level), wherein the level is the log level;
[0157] The log center module creation module is also used to define a switch instruction collection interface in the log center module. The switch instruction collection interface corresponds to the switch interface and is used to execute the switch function on the specified log module, or to execute the switch function on the log content of the preset log level in the specified log module. The switch instruction collection interface is defined as Bool logenable (string mode, int level), wherein the mode is the log module to which the log content belongs, and the level is the log level.
[0158] Furthermore, the log class creation module is also used to define cache interfaces of multiple different log modules in the log class, and the cache interface is defined as Void flush(). The cache interface is used to write the cached log content corresponding to each log module into the log center module when the program exits, so that the log center module can write the cached log content to a file or transmit the cached log over the network.
[0159] Furthermore, it also includes: a first exception capture module, which is used to register an exception in the log center module when any log module among the multiple different log modules is loaded before the log center module is loaded; the exception is captured by the log center module, and the program is controlled to end;
[0160] The second exception capture module is used to close the exception capture of the multiple different log modules when any one of the multiple different log modules is loaded after the log center module is loaded.
[0161] Embodiment 3
[0162] Based on the same inventive concept, a third embodiment of the present invention provides an electronic device, such as Figure 3 As shown, it includes a memory 304, a processor 302, and a computer program stored in the memory 304 and executable on the processor 302. When the processor 302 executes the program, the steps of the above-mentioned method for merging multiple log modules are implemented.
[0163] Among them, Figure 3 In the embodiment of the present invention, a bus architecture (represented by bus 300) is shown, which may include any number of interconnected buses and bridges, and bus 300 links various circuits including one or more processors represented by processor 302 and memory represented by memory 304. Bus 300 may also link various other circuits such as peripherals, voltage regulators, and power management circuits, which are well known in the art and are therefore not further described herein. Bus interface 306 provides an interface between bus 300 and receiver 301 and transmitter 303. Receiver 301 and transmitter 303 may be the same element, namely a transceiver, providing a unit for communicating with various other devices over a transmission medium. Processor 302 is responsible for managing bus 300 and general processing, while memory 304 may be used to store data used by processor 302 when performing operations.
[0164] Embodiment 4
[0165] Based on the same inventive concept, a fourth embodiment of the present invention provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the steps of the method for realizing the fusion of multiple log modules are implemented.
[0166] The algorithm and display provided herein are not inherently related to any particular computer, virtual system or other device. Various general purpose systems can also be used together with the teachings based on this. According to the above description, it is obvious that the structure required for constructing such systems. In addition, the present invention is not directed to any specific programming language either. It should be understood that various programming languages can be utilized to realize the content of the present invention described herein, and the description of the above specific languages is for disclosing the best mode of the present invention.
[0167] In the description provided herein, a large number of specific details are described. However, it is understood that embodiments of the present invention can be practiced without these specific details. In some instances, well-known methods, structures and techniques are not shown in detail so as not to obscure the understanding of this description.
[0168] Similarly, it should be understood that in order to streamline the present disclosure and aid in understanding one or more of the various inventive aspects, in the above description of exemplary embodiments of the present invention, various features of the present invention are sometimes grouped together into a single embodiment, figure, or description thereof. However, this disclosed method should not be interpreted as reflecting the intention that the claimed invention requires more features than those explicitly recited in each claim. More specifically, as reflected in the claims below, inventive aspects lie in less than all the features of the individual embodiments disclosed above. Therefore, the claims that follow the specific embodiment are hereby expressly incorporated into the specific embodiment, with each claim itself serving as a separate embodiment of the present invention.
[0169] Those skilled in the art will appreciate that the modules in the devices in the embodiments may be adaptively changed and arranged in one or more devices different from the embodiments. The modules or units or components in the embodiments may be combined into one module or unit or component, and in addition they may be divided into a plurality of submodules or subunits or subcomponents. Except that at least some of such features and / or processes or units are mutually exclusive, all features disclosed in this specification (including the accompanying claims, abstracts and drawings) and all processes or units of any method or device disclosed in this manner may be combined in any combination. Unless otherwise expressly stated, each feature disclosed in this specification (including the accompanying claims, abstracts and drawings) may be replaced by an alternative feature providing the same, equivalent or similar purpose.
[0170] In addition, those skilled in the art will appreciate that, although some embodiments herein include certain features included in other embodiments but not other features, the combination of features of different embodiments is meant to be within the scope of the present invention and form different embodiments. For example, in the claims below, any one of the claimed embodiments may be used in any combination.
[0171] The various component embodiments of the present invention may be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. It should be understood by those skilled in the art that a microprocessor or digital signal processor (DSP) may be used in practice to implement some or all functions of some or all components of a device or electronic device for fusion of multiple log modules according to an embodiment of the present invention. The present invention may also be implemented as a device or device program (e.g., a computer program and a computer program product) for executing part or all of the methods described herein. Such a program for implementing the present invention may be stored on a computer-readable medium, or may be in the form of one or more signals. Such a signal may be downloaded from an Internet website, or provided on a carrier signal, or provided in any other form.
[0172] It should be noted that the above embodiments illustrate the present invention rather than limit it, and that those skilled in the art may devise alternative embodiments without departing from the scope of the appended claims. In the claims, any reference symbol between brackets shall not be construed as a limitation on the claims. The word "comprising" does not exclude the presence of elements or steps not listed in the claims. The word "one" or "an" preceding an element does not exclude the presence of a plurality of such elements. The present invention may be implemented by means of hardware comprising a number of different elements and by means of a suitably programmed computer. In a unit claim enumerating a number of devices, several of these devices may be embodied by the same hardware item. The use of the words first, second, and third, etc., does not indicate any order. These words may be interpreted as names.
Claims
1. A method for integrating multiple log modules, applied to the operation of a program including multiple different log modules, characterized in that: include: Based on the display timing of the logs during program operation, the log levels corresponding to the various log contents in the corresponding log modules are classified to obtain four log levels, the four log levels including: INFO log level, WARNING log level, ERROR log level and FATAL log level. The various log contents belong to different log modules respectively, and different log modules include log contents of different log levels. Marking the log levels corresponding to the four log levels for each of the multiple different log contents, and marking the name of the log module to which each log content belongs; Create a log class, define the log output interface corresponding to each log module in the log class, the log output interface is the log output interface uniformly corresponding to the multiple different log modules, and the log output interface includes: a console display interface of the log, a file writing interface of the log, and a network transmission interface of the log; Create a log center module, define a log collection interface in the log center module, the log collection interface corresponds to the log output interface one by one, and the log collection interface includes an interface for displaying logs on a console, an interface for writing logs to a file, and an interface for transmitting logs over a network; When the preset log output function is uniformly executed on the multiple different log modules, the multiple different log contents are forwarded to the log center module by calling the corresponding log output interface of each log module, so that the corresponding log collection interface in the log center module receives the multiple different log contents, and uniformly executes the preset log output function on the multiple different log contents.
2. The method according to claim 1, characterized in that The log class is created as: class AdapterLog{AdapterLog(string logname)m_logname(logname), where logname is the name of the log module; The console display interface of the log is defined as: Void consolelog (int level, string log message); The file writing interface of the log is defined as: Void filelog (int level, string logmessage); The network transmission interface of the log is defined as: Void networklog(int level, string logmessage); Among them, level is the log level, and logmessage is the log content.
3. The method according to claim 2, characterized in that The log center module is created as: class LogCenter; The interface for displaying logs to the console is defined as: Void consolelog (string mode, int level, string logmessage); The interface for writing logs to files is defined as: Void filelog (string mode, int level, string logmessage); The interface for transmitting the log over the network is defined as: Void networklog(string mode, int level, string logmessage); Among them, mode is the log module to which the log content belongs, level is the log level, and logmessage is the log content.
4. The method according to claim 1, characterized in that Also includes: In the log class, a plurality of switch interfaces of different log modules are defined, and the switch interface is defined as Boollogenable(int level), wherein the level is the log level; A switch instruction collection interface is defined in the log center module. The switch instruction collection interface corresponds to the switch interface and is used to execute a switch function on a specified log module, or to execute a switch function on a log content of a preset log level in a specified log module. The switch instruction collection interface is defined as Bool logenable (string mode, intlevel), wherein the mode is the log module to which the log content belongs, and the level is the log level.
5. The method according to claim 1, characterized in that Also includes: A plurality of different log module cache interfaces are defined in the log class, and the cache interface is defined as Voidflush(). The cache interface is used to write the log content corresponding to the cache of each log module into the log center module when the program exits, so that the log center module executes the function of writing the cached log content to a file or transmitting the cached log over the network.
6. The method according to claim 1, characterized in that Also includes: When any one of the plurality of different log modules is loaded before the log center module is loaded, an exception is registered in the log center module; The log center module is used to capture exceptions and control the program termination.
7. The method according to claim 1, characterized in that Also includes: When any one of the multiple different log modules is loaded after the log center module is loaded, the exception capture of the multiple different log modules is closed.
8. A device for integrating multiple log modules, applied to the operation of a program including multiple different log modules, characterized in that: include: A classification module is used to classify the log levels corresponding to the various log contents in the corresponding log modules based on the display timing of the logs during program operation, and obtain four log levels, wherein the four log levels include: INFO log level, WARNING log level, ERROR log level and FATAL log level. The various log contents belong to different log modules respectively, and different log modules include log contents of different log levels; A marking module, used to mark the log levels corresponding to the four log levels for each of the multiple different log contents, and to mark the name of the log module to which each log content belongs; A log class creation module, used to create a log class, in which a log output interface corresponding to each log module is defined, wherein the log output interface is a log output interface uniformly corresponding to the multiple different log modules, and the log output interface includes a console display interface for the log, a file writing interface for the log, and a network transmission interface for the log; A log center module creation module, used to create a log center module, define a log collection interface in the log center module, the log collection interface corresponds to the log output interface one by one, and the log collection interface includes an interface for displaying logs on a console, an interface for writing logs to a file, and an interface for transmitting logs over a network; A unified execution module is used to forward various different log contents to the log center module by calling the corresponding log output interface of each log module when uniformly executing the preset log output function for the various different log modules, so that the corresponding log collection interface in the log center module receives the various different log contents and uniformly executes the preset log output function for the various different log contents.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the method steps according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method steps as claimed in any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Front-end log multi-channel output method and device, server and storage medium
CN109471773A