A log printing control method and apparatus
By configuring convergence strategies and using log identifiers, the system stability problem of the logging framework during traffic surges was solved, achieving stable system operation and rapid fault location under high load.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING WODONG TIANJUN INFORMATION TECH CO LTD
- Filing Date
- 2022-04-07
- Publication Date
- 2026-05-19
AI Technical Summary
Existing logging frameworks cause frequent disk I/O and a rapid increase in CPU utilization when traffic surges, increasing the risk of server downtime and potentially triggering a system avalanche effect.
The log output can be controlled by configuring convergence strategies, including convergence control based on dimensions such as user identifier, number of user requests per unit time, log volume per unit time, and classpath. Log identifiers are generated and added during printing to ensure consistent log format and facilitate problem localization.
Maintaining stable system operation during traffic surges reduces resource consumption, quickly locates system faults, and lowers the difficulty of troubleshooting.
Smart Images

Figure CN114780333B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a log printing control method and apparatus. Background Technology
[0002] Log printing is a data recording method used for various system operation monitoring, troubleshooting and locating problems, and tracking data changes. Currently, log printing-related functional modules are designed into the system, such as the Log4j framework or Logback framework, for log output. These logging frameworks can control the printing of logs by level.
[0003] In implementing this invention, the prior art has at least the following problems:
[0004] If the system traffic suddenly increases, the logs will also increase. If the existing logging framework is used to print logs in different levels, there will be frequent disk input / output (I / O), a rapid increase in the CPU utilization, and a rapid increase in memory utilization. This will greatly increase the risk of server crashes and cause a cascading failure in the entire system. Summary of the Invention
[0005] In view of this, embodiments of the present invention provide a log printing control method and apparatus. Before printing logs, the method uses a configured convergence strategy to control the output of logs and adds a log identifier when printing logs. This not only ensures the stable operation of the system, but also enables rapid problem localization when a system failure occurs.
[0006] To achieve the above objectives, according to one aspect of the present invention, a log printing control method is provided.
[0007] An embodiment of the present invention provides a log printing control method, comprising: responding to a user request, executing corresponding business logic code; when the business logic code executes to the first log printing function, generating a corresponding log identifier for the user request; obtaining a pre-configured convergence strategy; performing convergence control on the printing task of the executed log printing function according to the convergence dimension specified by the convergence strategy to obtain a log to be printed; formatting the log to be printed according to the log identifier and a set log format to obtain log information; and starting a log printing thread to print the log information.
[0008] Optionally, the convergence dimension includes any one or more of the following: user identifier of the user request, number of user requests per unit time, number of logs per unit time, and classpath; the convergence control of the logging task of the executed log printing function includes: if the convergence dimension includes the user identifier, then perform a hash operation on the user identifier and take the remainder with a set cutoff value to obtain the remainder, and determine that the remainder is within the set cutoff range, allowing the execution of the log printing function; if the convergence dimension includes the number of user requests per unit time, then according to a set sampling ratio, sample the logs from the user request to the classpath. The system samples user requests received within a given time period to determine if the user request matches the sample, thus allowing the execution of the log printing function. If the convergence dimension includes the amount of logs within the given time period, the log length corresponding to the log printing function is accumulated into the log length variable. If the accumulated log length variable is less than or equal to a set length threshold, the execution of the log printing function is allowed. The log length variable is used to record the currently accumulated log length. If the convergence dimension includes the classpath, the classpath of the log printing function is determined not to belong to the set target path, thus allowing the execution of the log printing function.
[0009] Optionally, the business logic code includes multiple log printing functions; the convergence control of the printing tasks of the executed log printing functions includes: when the business logic code executes the first log printing function, generating a convergence identifier based on the convergence result determined using the user identifier and / or the number of user requests per unit time; when the business logic code executes a non-first log printing function, obtaining the convergence identifier, and determining whether to allow or disallow the execution of the log printing function based on the convergence identifier.
[0010] Optionally, the convergence dimension further includes a global degradation switch, and the priority of the global degradation switch is higher than the priority of other convergence dimensions; the convergence control of the logging task of the executed log printing function includes: based on the state of the global degradation switch, if logging is allowed, performing convergence control of the logging task of the executed log printing function using the other convergence dimensions.
[0011] Optionally, generating a corresponding log identifier for the user request includes: concatenating the IP address, thread number, and current timestamp of the server currently processing the user request and performing a hash operation to obtain the log identifier for the user request.
[0012] Optionally, the log format is used to add identifiers before and after the log body, the identifiers being used to indicate the composition of the log body; the formatting of the log to be printed includes: splitting the log to be printed into multiple split logs, and setting a split number for each of the multiple split logs; using the split logs as the log body, and adding the identifiers to the log body; wherein, the identifier of the first log body includes the concatenation result of the log identifier and the corresponding split number, the class path and method, and the user identifier, and the identifiers of non-first log bodies include the concatenation result of the log identifier and the corresponding split number.
[0013] Optionally, the segmentation number includes the log number and segmentation sequence number of the log to be printed; after the step of obtaining the log to be printed, the method further includes: numbering the log to be printed according to the execution order of the log printing function in the business logic code to obtain the log number of the log to be printed.
[0014] To achieve the above objectives, according to another aspect of the present invention, a log printing control device is provided.
[0015] An embodiment of the present invention provides a log printing control device, comprising: an identifier generation module, configured to execute corresponding business logic code in response to a user request, and generate a corresponding log identifier for the user request when the business logic code executes the first log printing function;
[0016] The convergence control module is used to obtain a pre-configured convergence strategy, and perform convergence control on the printing tasks of the executed log printing function according to the convergence dimension specified by the convergence strategy to obtain the log to be printed; the log printing module is used to format the log to be printed according to the log identifier and the set log format to obtain log information, and start the log printing thread to print the log information.
[0017] Optionally, the convergence dimension includes any one or more of the following: user identifier of the user request, number of user requests per unit time, number of logs per unit time, and classpath. The convergence control module is further configured to: if the convergence dimension includes the user identifier, perform a hash operation on the user identifier and then take the remainder with a set cutoff value to obtain a remainder; determine if the remainder is within the set cutoff range and allow execution of the log printing function; if the convergence dimension includes the number of user requests per unit time, sample the user requests received per unit time according to a set sampling ratio; determine if the user requests hit the sample and allow execution of the log printing function; if the convergence dimension includes the number of logs per unit time, accumulate the log length corresponding to the log printing function to a log length variable; determine if the accumulated log length variable is less than or equal to a set length threshold and allow execution of the log printing function; wherein, the log length variable is used to record the currently accumulated log length; if the convergence dimension includes the classpath, determine if the classpath of the log printing function does not belong to a set target path and allow execution of the log printing function.
[0018] Optionally, the business logic code includes multiple log printing functions; the convergence control module is further configured to generate a convergence identifier when the business logic code executes the first log printing function, based on the convergence result determined by using the user identifier and / or the number of user requests per unit time; when the business logic code executes a non-first log printing function, obtain the convergence identifier, and determine whether to allow or disallow the execution of the log printing function based on the convergence identifier.
[0019] Optionally, the convergence dimension further includes a global degradation switch, and the priority of the global degradation switch is higher than the priority of other convergence dimensions; the convergence control module is further configured to perform convergence control of the other convergence dimensions on the logging task of the executed log printing function when it is determined that logging is allowed based on the state of the global degradation switch.
[0020] Optionally, the identifier generation module is further configured to concatenate the IP address, thread number, and current timestamp of the server currently processing the user request and perform a hash operation to obtain the log identifier of the user request.
[0021] Optionally, the log format is used to add identifiers before and after the log body, the identifiers being used to indicate the composition of the log body; the log printing module is further used to segment the log to be printed to obtain multiple segmented logs, and to set segmentation numbers for each of the multiple segmented logs; the segmented logs are used as log bodies, and the identifiers are added to the log bodies; wherein, the identifier of the first log body includes the concatenation result of the log identifier and the corresponding segmentation number, the class path and method, and the user identifier, and the identifiers of non-first log bodies include the concatenation result of the log identifier and the corresponding segmentation number.
[0022] Optionally, the segmentation number includes the log number and segmentation sequence number of the log to be printed; the device further includes: a log numbering module, used to number the log to be printed according to the execution order of the log printing function in the business logic code, so as to obtain the log number of the log to be printed.
[0023] To achieve the above objectives, according to another aspect of the present invention, an electronic device is provided.
[0024] An electronic device according to an embodiment of the present invention includes: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement a log printing control method according to an embodiment of the present invention.
[0025] To achieve the above objectives, according to another aspect of the present invention, a computer-readable medium is provided.
[0026] An embodiment of the present invention provides a computer-readable medium having a computer program stored thereon, wherein the program, when executed by a processor, implements a log printing control method according to an embodiment of the present invention.
[0027] One embodiment of the above invention has the following advantages or beneficial effects: By using a configured convergence strategy to control the log output before printing the logs, and adding a log identifier when printing the logs, it is possible to ensure stable system operation and quickly locate problems when system failures occur. By controlling the log output from multiple dimensions, it is possible to ensure that the log volume during traffic surges is roughly the same as the log volume under normal conditions, minimizing the resource consumption required for system business logic execution during log printing, further ensuring stable system operation and good flexibility.
[0028] The first log printing function for each user request performs convergence checks on the user identifier dimension and / or the number of user requests per unit time dimension, and generates a convergence flag based on the convergence result. This ensures that subsequent log printing positions for that user request only need to obtain the convergence flag, without repeating these convergence checks, thus minimizing redundant convergence checks in each log printing process. A global degradation switch controls whether to log user requests. When logging is allowed, further convergence checks on other dimensions are performed to avoid invalid convergence checks.
[0029] By generating a unique log identifier for the user request at the first log printing function, subsequent log printing functions use this identifier as the request-level identifier, ensuring rapid fault location in case of system failure. Segmenting and formatting the logs to be printed ensures a uniform output format, facilitating subsequent problem localization. Numbering the logs to be printed and adding them to the log body improves the accuracy of problem localization.
[0030] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description
[0031] The accompanying drawings are provided to better understand the invention and are not intended to unduly limit the scope of the invention. Wherein:
[0032] Figure 1 This is a schematic diagram of the main steps of the log printing control method according to an embodiment of the present invention;
[0033] Figure 2 This is a schematic diagram of the implementation architecture of the log printing control method according to an embodiment of the present invention;
[0034] Figure 3 This is a schematic diagram of the main flow of the log printing control method according to an embodiment of the present invention;
[0035] Figure 4 This is a schematic diagram of the convergence strategy in the log printing control method of this invention.
[0036] Figure 5 This is a schematic diagram of the main modules of the log printing control device according to an embodiment of the present invention;
[0037] Figure 6 This is an exemplary system architecture diagram in which embodiments of the present invention can be applied;
[0038] Figure 7 This is a schematic diagram of the structure of a computer device suitable for implementing electronic devices according to embodiments of the present invention. Detailed Implementation
[0039] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of the present invention, including various details to aid understanding. These details should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.
[0040] Figure 1 This is a schematic diagram illustrating the main steps of a log printing control method according to an embodiment of the present invention. Figure 1 As shown, the log printing control method of this embodiment of the invention is executed by a log printing control device and mainly includes the following steps:
[0041] Step S101: In response to a user request, execute the corresponding business logic code. When the business logic code executes the first log printing function, generate a corresponding log identifier for the user request. The user sends a user request to the log printing control device through the client. The log printing control device obtains and executes the business logic code corresponding to the user request. The business logic code may include one or more log printing functions. When the first log printing function is executed, a unique log identifier is generated for the user request. Other log printing functions of the user request use this log identifier as a unique identifier at the request level.
[0042] Step S102: Obtain the pre-configured convergence strategy, and perform convergence control on the printing tasks of the executed log printing function according to the convergence dimension specified by the convergence strategy to obtain the log to be printed. This convergence strategy is used to control the convergence of log output. In this embodiment, the convergence dimension may include any one or more of the following: user identifier of the user request (e.g., user account), number of user requests per unit time, number of logs per unit time, and classpath.
[0043] Each convergence dimension corresponds to a specific convergence control implementation method. For the user identifier convergence dimension, the user identifier is hashed and then moduloed by a set cutoff value. The remainder is then determined, and if it falls within the set cutoff range, the log printing function is allowed to execute. For the user request volume per unit time convergence dimension, user requests received per unit time are sampled according to a set sampling ratio. If a user request matches the sample, the log printing function is allowed to execute.
[0044] For the convergence dimension of log volume per unit time, the log length corresponding to the log printing function is accumulated into the log length variable. If the accumulated log length variable is less than or equal to a set length threshold, the log printing function is allowed to execute. The log length variable records the currently accumulated log length. For the convergence dimension of classpath, if the classpath of the log printing function does not belong to the set target path, the log printing function is allowed to execute.
[0045] After the above processing, we can obtain all the log printing functions that are allowed to be executed in the business logic code. The logs corresponding to these log printing functions constitute the logs to be printed.
[0046] Step S103: Format the log to be printed according to the log identifier and the set log format to obtain log information, and start the log printing thread to print the log information. This step is used to standardize the log output format. Specifically, the log to be printed can be formatted according to the set log format. To facilitate problem localization, a log identifier needs to be added during the formatting process.
[0047] In this embodiment, to avoid excessively long printed logs, the log to be printed needs to be split into multiple split logs. These split logs are then formatted to obtain the log information. Finally, the log printing thread is started to print the log information. This process ensures stable system operation and enables rapid problem localization in the event of a system failure.
[0048] Figure 2 This is a schematic diagram of the implementation architecture of the log printing control method according to an embodiment of the present invention. Figure 2 As shown, the log printing control method of this invention can be applied to scenarios with sudden traffic surges. In such scenarios, a large number of users access the same business function at the same time, resulting in a sudden surge in traffic. If only log output level control is performed during log printing without traffic control, it will cause the server to crash.
[0049] To address the aforementioned issues, this embodiment controls log printing based on user traffic. Taking N users simultaneously accessing the settlement and order placement service as an example, the implementation of the settlement and order placement service requires the execution of multiple logic blocks. Each logic block can implement some of the functions of the settlement and order placement service, and some or all of the logic blocks include a log printing function.
[0050] When executing logic blocks sequentially, if a logic block contains a logging function, multi-dimensional convergence control is applied to logging. This includes convergence control based on dimensions such as global degradation switch, user identifier of user requests, number of user requests per unit time, log volume per unit time, and classpath, to determine the logs to be printed. Then, a logging framework (such as Log4j) is used for logging. The final printed logs can include multiple log levels, such as error, warning, info, and debug.
[0051] Figure 3 This is a schematic diagram of the main flow of the log printing control method according to an embodiment of the present invention. Figure 3 As shown, the log printing control method of this embodiment of the invention is executed by a log printing control device and mainly includes the following steps:
[0052] Step S301: In response to multiple user requests received within a unit of time, execute the corresponding business logic code. Multiple users send user requests to the log printing control device through the client within a unit of time. These user requests are used to request the same business function, such as the address switching function on the settlement page. In the embodiment, the unit of time is, for example, 500 milliseconds, 1 second, 2 seconds, etc.
[0053] The log printing control device obtains and executes the business logic code corresponding to these user requests. Since multiple user requests are used to request the same business function, the executed business logic code is the same piece of code. Taking the address switching function as an example, when executing the business logic code corresponding to this function, each call to the log printing function is a log printing request.
[0054] Step S302: When the business logic code executes the first log printing function, a corresponding log identifier is generated for each of the multiple user requests. In this embodiment, a log identifier (TraceId) can be generated when the first log printing function for each user request is executed. This log identifier is a unique identifier for a single log record of a user request. The remaining log printing functions for that user request can then use the getTraceId() method to obtain the TraceId, which can be used as a unique identifier at the request level, allowing for rapid problem location during troubleshooting.
[0055] The generation of the log identifier can be done in several ways. For example, it can be achieved by concatenating the IP address of the server currently processing the user request, the thread number, and the current timestamp, and then performing a hash operation to obtain the log identifier for that user request. Another possible method is to concatenate the user identifier, the IP address of the server currently processing the user request, the thread number, and the current timestamp, and then perform a hash operation to obtain the log identifier for that user request. The thread number is the ID of the thread used to process the user request.
[0056] In this embodiment, the Java container's built-in ThreadLocal (thread-local variable) can be used to create a TraceId variable and store it in ThreadLocal, ensuring that the TraceId obtained by the remaining log printing function of the user request is unique. The lifecycle of TraceId is request-level; after the user request ends, the TraceId-related information in ThreadLocal can be cleared.
[0057] Step S303: Obtain the pre-configured convergence strategy. Based on the convergence dimensions specified by the convergence strategy, perform convergence control on the printing tasks of the executed log printing function to obtain the log to be printed. This step is used to perform convergence control on log printing from multiple dimensions. In this embodiment, by dynamically configuring the convergence strategy, the immediacy of convergence control and the flexibility of the code are achieved.
[0058] Figure 4 This is a schematic diagram of the convergence strategy in the log printing control method of this invention. For example... Figure 4 As shown, the convergence strategy of this embodiment includes five convergence dimensions: global degradation switch, user identifier of user request, number of user requests per unit time, number of logs per unit time, and classpath.
[0059] For the global degradation switch as a convergence dimension, log printing is controlled by reading its configured status. If the global degradation switch is enabled, no logs are printed for any user requests; if it is disabled, logging is allowed, allowing further convergence control for other convergence dimensions. In other words, the global degradation switch has higher priority than other convergence dimensions; convergence control for other dimensions only occurs when the global degradation switch is disabled. Furthermore, the global degradation switch configuration can be adjusted at any time during peak traffic periods.
[0060] For the convergence dimension of user request user identifier, percentage-based sharding control is performed based on the user request user identifier for each user. Specifically, after performing a hash operation on the user identifier, the remainder is calculated by taking the remainder modulo the set sharding value. It is then determined whether the remainder is within the set sharding range. If the remainder is within the set sharding range, the log printing function is allowed to be executed; otherwise, the log printing function is not allowed to be executed.
[0061] In this embodiment, the cutoff value is the normal log volume (i.e., the log volume during non-peak hours), which can be customized based on experience, for example, the default is 10000. The cutoff range is calculated based on the estimated peak traffic. The maximum value of the cutoff range can be the ratio of peak traffic to normal traffic (e.g., 2000), and the minimum value can be 0. The cutoff range is set before the peak begins to ensure that the log volume during the peak is approximately the same as the normal log volume, ensuring that the system resources consumed by log printing preempt the resources needed for executing business logic as little as possible, thus ensuring system stability. The cutoff value and cutoff range are also dynamically configurable and can be adjusted at any time during peak traffic periods.
[0062] For the convergence dimension of user requests per unit time, the number of user requests received per unit time is sampled according to a set sampling ratio. It is then determined whether a user request matches the sample. If it matches, the log printing function is allowed to execute; otherwise, it is not. In this embodiment, the TraceId of user requests that need to be logged per unit time is recorded, and these TraceIds are sampled at a percentage; the sampling ratio can be dynamically configured. If a sample matches, the log printing function is allowed to execute; otherwise, it is discarded.
[0063] For the convergence dimension of log volume per unit time, before executing the log printing function, the log length corresponding to the log printing function is accumulated into a log length variable. It is then checked whether the accumulated log length variable is less than or equal to a set length threshold. If the accumulated log length variable is less than or equal to the length threshold, the log printing function is allowed to execute; otherwise, it is not allowed. The log length variable records the currently accumulated log length. The length threshold is also dynamically configured and can be adjusted at any time during peak traffic periods.
[0064] In this embodiment, the character size of the log entries to be printed within the same second is recorded, and this character size is compared with a length threshold in a dynamically configured setting. If the character size is less than or equal to the length threshold, the log printing function is allowed to be executed; otherwise, the log printing function is not allowed to be executed.
[0065] For the classpath convergence dimension, it determines whether the classpath of the logging function belongs to the defined target path. If the classpath of the logging function does not belong to the defined target path, the logging function is allowed to execute; otherwise, it is not allowed to execute. The target path is the path of the classes that do not need to be logged (including the class name and package path). In the embodiment, the package path of the objects in the current logging object is obtained. If it matches the dynamically configured package path, all logs within that class are discarded.
[0066] Business logic code typically includes multiple log printing functions. In a preferred embodiment, when the business logic code of each user request reaches the first log printing function, a convergence identifier can be generated based on the convergence result determined by using the user identifier and / or the number of user requests per unit time. Subsequent log printing functions of the user request only need to obtain this convergence identifier and do not need to repeat the convergence judgment, so as to minimize the repeated convergence judgment in each log printing process.
[0067] Specifically, in this step, when the business logic code executes the first log printing function, a convergence flag is generated based on the convergence result determined by the user identifier and / or the number of user requests per unit time. When the business logic code executes a function other than the first log printing function, the convergence flag is obtained, and based on the convergence flag, it is determined whether the execution of the log printing function is allowed or not.
[0068] Taking the user identifier as a convergence dimension as an example, if the remainder is within the cutoff range, the convergence identifier can be set to true. When other log printing functions obtain this convergence identifier, they can directly execute the log printing function. If the remainder is not within the cutoff range, the convergence identifier can be set to false. When other log printing functions obtain this convergence identifier, they can directly refuse to execute the log printing function without repeated judgment.
[0069] After the above processing, we can obtain all the log printing functions that are allowed to be executed in the business logic code. The logs corresponding to these log printing functions constitute the logs to be printed.
[0070] Step S304: Based on the execution order of each log printing function in the business logic code, number the corresponding logs to be printed to obtain the log number of the log to be printed. In this embodiment, a log counter can be used to sequentially number all logs to be printed, and these logs can be linked together by numbering to facilitate accurate problem localization.
[0071] Specifically, define a log counter in ThreadLocal, create a TraceId and set it in the current thread. Initialize the number of the log counter to 0. Then, for each obtained TraceId, increment the number of the log counter by 1. Add the number calculated by the log counter to each log to be printed and print them together in the log.
[0072] Step S305: Format the log to be printed according to the log identifier, log number, and set log format to obtain log information. This step is used to provide a unified format standard for subsequent log analysis and log positioning. The log format can be used to add identifiers before and after the log body, and the identifier is used to represent the composition of the log body.
[0073] To avoid the length of the printed log being too long, it is necessary to limit the log length. In the embodiment, the maximum log size can be set. Judge the size of each log to be printed. If the log to be printed is larger than the maximum log size, after splitting the log to be printed, add an identifier to the splitting result; if the log to be printed is less than or equal to the maximum log size, directly add an identifier.
[0074] The formatting process for the log to be printed that needs to be split is as follows: Split the log to be printed according to the set maximum log size to obtain multiple split logs, and set split numbers for the multiple split logs respectively; then use the split logs as the log body and add an identifier to the log body.
[0075] Among them, the identifier of the first log body includes the concatenation result of the log identifier and the corresponding split number, the class path and method, and the user identifier; the identifier of the non-first log body includes the concatenation result of the log identifier and the corresponding split number. In the embodiment, the split number includes the log number of the log to be printed and the split sequence number, ensuring the log sequence and integrity.
[0076] The formatting process for the log to be printed that does not need to be split is as follows: Use the log to be printed as the log body and add an identifier to the log body. Among them, the identifier of the log body includes the concatenation result of the log identifier and the corresponding log number, the class path and method, and the user identifier.
[0077] Taking the log to be printed at the info level as an example, assume that the maximum log size is 3000 and the log length of the log to be printed is 6500. Then it will be divided into 3 segments when printed. The first two segments are 3000 characters each, and the last segment is 500 characters. Then the formatting result of the log to be printed is:
[0078] [<TraceId~split number 1>]|"class path|method"|“pin”|description|in / out|log body p->|
[0079] [<TraceId~Split ID 2>]|p->|Log body p->|
[0080] [<TraceId~Split ID 3>]|p->|Log body
[0081] In the above formatting result, "pin" is the user identifier; the description is used to explain the class and method to be printed; "in" and "out" are the input and output parameters respectively. Among them, the description and the in / out parts are not mandatory. They are used during rpc remote calls. For other situations, such as only needing to print a sentence, these two parts are not required.
[0082] For the logs to be printed at the error level, warn level, and debug level, the formatting process is similar to that of the logs to be printed at the info level, except that the content of the log body is different. For example, the log body at the info level is the parameters and the return result; the log body at the error level is the error code. Assuming that the length of the log to be printed at the error level is less than the maximum log size, its formatting result can be:
[0083] [<TraceId~Log ID>]|"Class path|Method"|"pin"|Description|in / out|Log body stack
[0084] Among them, the stack indicates the location of the error code.
[0085] The above formatting result can elaborate on the logs to be printed, facilitating problem location.
[0086] Step S306: Start the log printing thread to print log information. In the embodiment, a dynamic thread pool for log printing can be defined, and an asynchronous switch can be set to control the asynchronous execution of the thread, realizing asynchronous printing of log information. This operation can maximize the utilization of resources, and at the same time, the asynchronous thread will not affect the main logic during execution.
[0087] In an optional embodiment, the log printing level can also be set to control the output log level. Taking the Log4j framework as an example, it uses four log printing levels, with the priorities from high to low being error, warn, info, and debug. The log printing level can be controlled to be info, so that the logs at and above this level are output, and the logs below this level are not output. That is, all the log information at the debug level will not be printed.
[0088] In another preferred embodiment, before executing each log printing function, data合法性验证 can be performed respectively, that is, to judge whether the data related to the input parameters of each log printing function is empty. If it is empty, the execution of the log printing function is not allowed; if it is not empty, the execution of the log printing function is allowed.
[0089] As can be seen from the log printing control method of this invention, by converging the log output during traffic surges, the preconditions and printing methods of log printing can be reasonably controlled to ensure that the log volume during traffic peaks is roughly the same as the normal volume. This minimizes the resources required for system execution of business logic by log printing, thereby ensuring system stability during traffic surges. At the same time, in the event of system failure, it enables quick and accurate problem location, reducing the difficulty of troubleshooting and improving troubleshooting efficiency.
[0090] Figure 5 This is a schematic diagram of the main modules of a log printing control device according to an embodiment of the present invention. Figure 5 As shown, the log printing control device 500 of this embodiment mainly includes:
[0091] The identifier generation module 501 is used to respond to user requests and execute corresponding business logic code. When the business logic code executes the first log printing function, it generates a corresponding log identifier for the user request. The user sends a user request to the identifier generation module through a client. The identifier generation module obtains and executes the business logic code corresponding to the user request. The business logic code may include one or more log printing functions. When the first log printing function is executed, a unique log identifier is generated for the user request. All other log printing functions of the user request use this log identifier as a unique identifier at the request level.
[0092] The convergence control module 502 is used to obtain a pre-configured convergence strategy and, according to the convergence dimension specified by the convergence strategy, to perform convergence control on the printing tasks of the executed log printing function to obtain the log to be printed. This convergence strategy is used to control the convergence of log output. In this embodiment, the convergence dimension may include any one or more of the following: user identifier of the user request (e.g., user account), number of user requests per unit time, number of logs per unit time, and classpath.
[0093] Each convergence dimension corresponds to a specific convergence control implementation method. For the user identifier convergence dimension, the user identifier is hashed and then moduloed by a set cutoff value. The remainder is then determined, and if it falls within the set cutoff range, the log printing function is allowed to execute. For the user request volume per unit time convergence dimension, user requests received per unit time are sampled according to a set sampling ratio. If a user request matches the sample, the log printing function is allowed to execute.
[0094] For the convergence dimension of log volume per unit time, the log length corresponding to the log printing function is accumulated into the log length variable. If the accumulated log length variable is less than or equal to a set length threshold, the log printing function is allowed to execute. The log length variable records the currently accumulated log length. For the convergence dimension of classpath, if the classpath of the log printing function does not belong to the set target path, the log printing function is allowed to execute.
[0095] After the above processing, we can obtain all the log printing functions that are allowed to be executed in the business logic code. The logs corresponding to these log printing functions constitute the logs to be printed.
[0096] The log printing module 503 is used to format the log to be printed according to the log identifier and the set log format, obtain log information, and start the log printing thread to print the log information. This module is used to standardize the format of log output. Specifically, the log to be printed can be formatted according to the set log format. To facilitate problem localization, a log identifier needs to be added during the formatting process.
[0097] Additionally, the log printing control device 500 of this embodiment may further include: a log numbering module ( Figure 5 (Not shown in the image). This module is used to number the logs to be printed according to the execution order of the log printing function in the business logic code, thereby obtaining the log number of the log to be printed.
[0098] As can be seen from the above description, by using the configured convergence strategy to control the output of logs before printing them, and by adding log identifiers when printing logs, we can ensure the stable operation of the system and quickly locate problems when system failures occur.
[0099] Figure 6 An exemplary system architecture 600 is shown that can be applied to the log printing control method or log printing control device of the present invention.
[0100] like Figure 6 As shown, system architecture 600 may include terminal devices 601, 602, and 603, a network 604, and a server 605. Network 604 serves as the medium for providing communication links between terminal devices 601, 602, and 603 and server 605. Network 604 may include various connection types, such as wired or wireless communication links or fiber optic cables, etc.
[0101] Users can use terminal devices 601, 602, and 603 to interact with server 605 via network 604 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 601, 602, and 603, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social media platform software, etc.
[0102] Terminal devices 601, 602, and 603 can be various electronic devices with displays and web browsing capabilities, including but not limited to smartphones, tablets, laptops, and desktop computers.
[0103] Server 605 can be a server that provides various services, such as a backend management server that processes user requests sent by users through terminal devices 601, 602, and 603. The backend management server can execute business code logic, generate log identifiers, control the convergence of log printing, print formatted log information, and feed back the processing results (such as the printed log information) to the terminal devices.
[0104] It should be noted that the log printing control method provided in this application embodiment is generally executed by server 605, and correspondingly, the log printing control device is generally set in server 605.
[0105] It should be understood that Figure 6 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0106] According to embodiments of the present invention, the present invention also provides an electronic device and a computer-readable medium.
[0107] The electronic device of the present invention includes: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement a log printing control method according to an embodiment of the present invention.
[0108] The present invention provides a computer-readable medium having a computer program stored thereon, which, when executed by a processor, implements a log printing control method according to an embodiment of the present invention.
[0109] The following is for reference. Figure 7 It shows a schematic diagram of the structure of a computer system 700 suitable for implementing an electronic device according to embodiments of the present invention. Figure 7 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of use of the embodiments of the present invention.
[0110] like Figure 7As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 702 or programs loaded from storage section 708 into random access memory (RAM) 703. The RAM 703 also stores various programs and data required for the operation of the computer system 700. The CPU 701, ROM 702, and RAM 703 are interconnected via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.
[0111] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, mouse, etc.; an output section 707 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 710 as needed so that computer programs read from it can be installed into the storage section 708 as needed.
[0112] In particular, according to embodiments disclosed in this invention, the processes described in the above main step diagrams can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the main step diagrams. In such embodiments, the computer program can be downloaded and installed from a network via communication section 709, and / or installed from removable medium 711. When the computer program is executed by central processing unit (CPU) 701, it performs the functions defined above in the system of this invention.
[0113] It should be noted that the computer-readable medium shown in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. Computer-readable signal media can also be any computer-readable medium other than computer-readable storage media, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.
[0114] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0115] The modules described in the embodiments of the present invention can be implemented in software or hardware. The described modules can also be housed in a processor; for example, a processor can be described as including an identifier generation module, a convergence control module, and a log printing module. The names of these modules do not necessarily limit the module itself; for example, the identifier generation module can also be described as "a module that, in response to a user request, executes corresponding business logic code, and when the business logic code executes the first log printing function, generates a corresponding log identifier for the user request."
[0116] In another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device. The computer-readable medium carries one or more programs, which, when executed by the device, cause the device to include: responding to a user request, executing corresponding business logic code; when the business logic code executes the first log printing function, generating a corresponding log identifier for the user request; obtaining a pre-configured convergence strategy; performing convergence control on the printing task of the executed log printing function according to the convergence dimension specified by the convergence strategy to obtain a log to be printed; formatting the log to be printed according to the log identifier and a set log format to obtain log information; and starting a log printing thread to print the log information.
[0117] According to the technical solution of the present invention, by using a configured convergence strategy to control the output of logs before printing logs, and adding log identifiers when printing logs, the system can not only ensure stable operation, but also quickly locate problems when system failures occur.
[0118] The above-described product can execute the method provided in the embodiments of the present invention, and has the corresponding functional modules and beneficial effects for executing the method. Technical details not described in detail in this embodiment can be found in the method provided in the embodiments of the present invention.
[0119] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.
Claims
1. A log printing control method, characterized in that, include: In response to a user request, the corresponding business logic code is executed. When the business logic code executes the first log printing function, a corresponding log identifier is generated for the user request. Obtain a pre-configured convergence strategy, and perform convergence control on the logging tasks of the executed log printing function according to the convergence dimension specified by the convergence strategy to obtain the logs to be printed and generate a convergence identifier based on the convergence result; the convergence dimension includes any one or more of the user identifier of the user request, the number of user requests per unit time, the number of logs per unit time, and the classpath; the convergence identifier is used to determine whether the execution of the log printing function is allowed or not. Based on the log identifier and the set log format, the log to be printed is formatted to obtain log information, and the log printing thread is started to print the log information.
2. The method according to claim 1, characterized in that, The convergence control of the logging tasks executed by the log printing function includes: If the convergence dimension includes the user identifier, then after performing a hash operation on the user identifier, the remainder is obtained by taking the remainder modulo the set cutoff value. If the remainder is within the set cutoff range, the log printing function is allowed to be executed. If the convergence dimension includes the number of user requests per unit time, then the user requests received per unit time are sampled according to the set sampling ratio to determine if the user request hits the sample and allow the execution of the log printing function. If the convergence dimension includes the amount of logs per unit time, then the log length corresponding to the log printing function is accumulated into the log length variable. If the accumulated log length variable is less than or equal to the set length threshold, the execution of the log printing function is allowed. The log length variable is used to record the currently accumulated log length. If the convergence dimension includes the classpath, then it is determined that the classpath of the log printing function does not belong to the set target path, and the execution of the log printing function is allowed.
3. The method according to claim 2, characterized in that, The business logic code includes multiple log printing functions; The convergence control of the logging tasks executed by the log printing function includes: When the business logic code executes to the first log printing function, a convergence identifier is generated based on the convergence result determined by using the user identifier and / or the number of user requests per unit time. When the business logic code executes to a point other than the first log printing function, the convergence flag is obtained, and based on the convergence flag, it is determined whether the execution of the log printing function is allowed or not.
4. The method according to claim 2 or 3, characterized in that, The convergence dimension also includes a global degradation switch, and the priority of the global degradation switch is higher than the priority of other convergence dimensions; The convergence control of the logging tasks executed by the log printing function includes: Based on the state of the global degradation switch, if logging is allowed, the logging tasks of the executed logging functions are subject to convergence control in the other convergence dimensions.
5. The method according to claim 1, characterized in that, The step of generating a corresponding log identifier for the user request includes: The log identifier of the user request is obtained by concatenating the IP address, thread number, and current timestamp of the server currently processing the user request and performing a hash operation.
6. The method according to claim 1, characterized in that, The log format is used to add identifiers before and after the log body, and the identifiers are used to indicate the composition of the log body; The formatting of the log to be printed includes: The log to be printed is segmented to obtain multiple segmented logs, and a segmentation number is assigned to each of the multiple segmented logs; The segmented log is used as the log body, and the identifier is added to the log body; wherein, the identifier of the first log body includes the concatenation result of the log identifier and the corresponding segment number, the class path and method, and the user identifier, and the identifier of the non-first log body includes the concatenation result of the log identifier and the corresponding segment number.
7. The method according to claim 6, characterized in that, The segmentation number includes the log number and segmentation sequence number of the log to be printed; After obtaining the log to be printed, the method further includes: Based on the execution order of the log printing function in the business logic code, the logs to be printed are numbered to obtain the log number of the log to be printed.
8. A log printing control device, characterized in that, include: The identifier generation module is used to respond to user requests and execute corresponding business logic code. When the business logic code executes the first log printing function, it generates a corresponding log identifier for the user request. The convergence control module is used to obtain a pre-configured convergence strategy, and perform convergence control on the logging tasks of the executed log printing function according to the convergence dimension specified by the convergence strategy, to obtain the logs to be printed and generate a convergence identifier based on the convergence result; the convergence dimension includes any one or more of the user identifier of the user request, the number of user requests per unit time, the number of logs per unit time, and the classpath; the convergence identifier is used to determine whether the execution of the log printing function is allowed or not. The log printing module is used to format the log to be printed according to the log identifier and the set log format, obtain log information, and start the log printing thread to print the log information.
9. An electronic device, characterized in that, include: One or more processors; Storage device for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1-7.
10. A computer-readable medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-7.