CN113778350A - Log printing method and device - Google Patents

Log printing method and device Download PDF

Info

Publication number
CN113778350A
CN113778350A CN202110025697.8A CN202110025697A CN113778350A CN 113778350 A CN113778350 A CN 113778350A CN 202110025697 A CN202110025697 A CN 202110025697A CN 113778350 A CN113778350 A CN 113778350A
Authority
CN
China
Prior art keywords
log
printing
target scene
scene
configuration data
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202110025697.8A
Other languages
Chinese (zh)
Inventor
贾龙
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Century Trading Co Ltd
Beijing Wodong Tianjun Information Technology Co Ltd
Original Assignee
Beijing Jingdong Century Trading Co Ltd
Beijing Wodong Tianjun Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Jingdong Century Trading Co Ltd, Beijing Wodong Tianjun Information Technology Co Ltd filed Critical Beijing Jingdong Century Trading Co Ltd
Priority to CN202110025697.8A priority Critical patent/CN113778350A/en
Publication of CN113778350A publication Critical patent/CN113778350A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F3/00Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
    • G06F3/12Digital output to print unit, e.g. line printer, chain printer
    • G06F3/1201Dedicated interfaces to print systems
    • G06F3/1202Dedicated interfaces to print systems specifically adapted to achieve a particular effect
    • G06F3/1211Improving printing performance
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F3/00Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
    • G06F3/12Digital output to print unit, e.g. line printer, chain printer
    • G06F3/1201Dedicated interfaces to print systems
    • G06F3/1223Dedicated interfaces to print systems specifically adapted to use a particular technique
    • G06F3/1237Print job management
    • G06F3/1253Configuration of print job parameters, e.g. using UI at the client
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F3/00Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
    • G06F3/12Digital output to print unit, e.g. line printer, chain printer
    • G06F3/1201Dedicated interfaces to print systems
    • G06F3/1223Dedicated interfaces to print systems specifically adapted to use a particular technique
    • G06F3/1237Print job management
    • G06F3/1273Print job history, e.g. logging, accounting, tracking

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Human Computer Interaction (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)
  • Accessory Devices And Overall Control Thereof (AREA)

Abstract

The invention discloses a log printing method and device, and relates to the technical field of computers. One embodiment of the method comprises: receiving a log printing request, acquiring a target scene according to the log printing request, and inquiring log configuration data corresponding to the target scene according to a scene identifier of the target scene, wherein the log configuration data comprises: log output mode and log level; judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene; if yes, determining that the log does not need to be printed; if not, acquiring a log printing instance corresponding to the target scene according to the log configuration data corresponding to the target scene, and then printing the log by using the log printing instance. According to the embodiment, the log configuration data corresponding to the scene is used, the output mode and the printing level of the log are dynamically controlled, different scene requirements can be met, the practicability is high, and the user experience is good.

Description

Log printing method and device
Technical Field
The invention relates to the technical field of computers, in particular to a log printing method and device.
Background
For an application program, logging is an essential part, and online problem tracking, log-based business logic statistical analysis and the like can not be separated from logging. Currently, Log frames commonly used include Log4j2, Slf4j, Log back, and the like. However, the existing log framework has the following disadvantages: logs can be printed in all scenes, and whether the logs are output or not can not be controlled in different scenes; the logs with different log levels cannot be printed aiming at different scenes; different output modes can not be used in different scenes.
Disclosure of Invention
In view of this, embodiments of the present invention provide a log printing method and apparatus, so as to implement dynamic control of an output mode and a printing level of a log according to log configuration data corresponding to a scene, and meet different scene requirements, and have strong practicability and good user experience.
To achieve the above object, according to an aspect of an embodiment of the present invention, there is provided a log printing method.
The log printing method of the embodiment of the invention comprises the following steps: receiving a log printing request, acquiring a target scene according to the log printing request, and inquiring log configuration data corresponding to the target scene according to a scene identifier of the target scene, wherein the log configuration data comprises: log output mode and log level; judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene; if yes, determining that the log does not need to be printed; if not, acquiring a log printing instance corresponding to the target scene according to the log configuration data corresponding to the target scene, and then printing a log by using the log printing instance.
Optionally, the log configuration data includes: log printing state; and after querying log configuration data corresponding to the target scene, the method further comprises: judging whether the log printing state corresponding to the target scene is required to be printed or not; if yes, determining that the target scene needs to print the log, and then printing the log according to the log output mode and the log level corresponding to the target scene; if not, determining that the target scene does not need to print the log.
Optionally, the method further comprises: determining a plurality of scenes; acquiring a scene identifier of each scene, configuring a log printing state, a log output mode and a log level corresponding to each scene, and acquiring log configuration data corresponding to each scene to obtain the log configuration data corresponding to the plurality of scenes; storing the log configuration data corresponding to the scenes into a memory or a database; wherein the target scene is a scene of the plurality of scenes.
Optionally, the obtaining, according to the log configuration data corresponding to the target scene, a log print instance corresponding to the target scene, and then performing log printing by using the log print instance includes: screening a log printing instance corresponding to the target scene from the Map according to a log output mode corresponding to the target scene; and calling a log level printing method corresponding to the target scene in the proxy class according to the log level corresponding to the target scene, and printing the log corresponding to the target scene according to the log printing instance corresponding to the target scene.
Optionally, the method further comprises: adding one or more log configurations in a configuration file of a log framework; setting a log output mode corresponding to the log configuration for each log configuration, setting the log level corresponding to the log configuration as trace, generating a log printing instance corresponding to the log configuration, and storing the generated log printing instance by using Map; wherein, Key in Map is log output mode, Value is log printing example.
Optionally, the method further comprises: defining the agent class, and defining a method for implementing log-level printing in the agent class.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided a log printing apparatus.
An apparatus for printing a log according to an embodiment of the present invention includes: the query module is configured to receive a log printing request, obtain a target scene according to the log printing request, and query log configuration data corresponding to the target scene according to a scene identifier of the target scene, where the log configuration data includes: log output mode and log level; the judging module is used for judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene; and the printing module is used for determining that the log does not need to be printed if the log does not need to be printed, acquiring a log printing instance corresponding to the target scene according to the log configuration data corresponding to the target scene if the log does not need to be printed, and then printing the log by using the log printing instance.
Optionally, the log configuration data includes: log printing state; and the judging module is further used for: judging whether the log printing state corresponding to the target scene is required to be printed or not; if yes, determining that the target scene needs to print the log, and then printing the log according to the log output mode and the log level corresponding to the target scene; if not, determining that the target scene does not need to print the log.
Optionally, the apparatus further comprises a configuration module, the configuration module further configured to: determining a plurality of scenes; acquiring a scene identifier of each scene, configuring a log printing state, a log output mode and a log level corresponding to each scene, and acquiring log configuration data corresponding to each scene to obtain the log configuration data corresponding to the plurality of scenes; storing the log configuration data corresponding to the scenes into a memory or a database; wherein the target scene is a scene of the plurality of scenes.
Optionally, the printing module is further configured to: screening a log printing instance corresponding to the target scene from the Map according to a log output mode corresponding to the target scene; and calling a log level printing method corresponding to the target scene in the proxy class according to the log level corresponding to the target scene, and printing the log corresponding to the target scene according to the log printing instance corresponding to the target scene.
Optionally, the configuration module is further configured to: adding one or more log configurations in a configuration file of a log framework; setting a log output mode corresponding to the log configuration for each log configuration, setting the log level corresponding to the log configuration as trace, generating a log printing instance corresponding to the log configuration, and storing the generated log printing instance by using Map; wherein, Key in Map is log output mode, Value is log printing example.
Optionally, the configuration module is further configured to: defining the agent class, and defining a method for implementing log-level printing in the agent class.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided an electronic apparatus.
An electronic device of an embodiment of the present invention includes: one or more processors; the storage device is used for storing one or more programs, and when the one or more programs are executed by one or more processors, the one or more processors realize the log printing method of the embodiment of the invention.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided a computer-readable medium.
A computer-readable medium of an embodiment of the present invention has stored thereon a computer program that, when executed by a processor, implements a log printing method of an embodiment of the present invention.
One embodiment of the above invention has the following advantages or benefits: the method has the advantages that the log output mode and the log grade corresponding to the target scene can be inquired according to the scene identification of the target scene, then the log grade in the code is compared with the inquired log grade, the log is printed by combining the inquired log output mode, the problems that logs of different log grades cannot be printed according to different scenes and different output modes cannot be used in different scenes in the conventional log framework are solved, log configuration data corresponding to the scenes are achieved, the output mode and the printing grade of the log are dynamically controlled, requirements of different scenes can be met, the practicability is high, and user experience is good.
Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
fig. 1 is a schematic diagram of main steps of a log printing method according to an embodiment of the present invention;
fig. 2 is a schematic diagram of a main flow of a log printing method according to an embodiment of the present invention;
fig. 3 is a schematic diagram of a main flow of a log printing method according to another embodiment of the present invention;
fig. 4 is a schematic diagram of main modules of a log printing apparatus according to an embodiment of the present invention;
FIG. 5 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;
fig. 6 is a schematic block diagram of a computer system suitable for use in implementing a terminal device or server of an embodiment of the invention.
Detailed Description
Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
Fig. 1 is a schematic diagram of main steps of a log printing method according to an embodiment of the present invention. As an embodiment of the present invention, as shown in fig. 1, the main steps of the log printing method may include:
step S101, receiving a log printing request, acquiring a target scene according to the log printing request, and inquiring log configuration data corresponding to the target scene according to a scene identifier of the target scene;
step S102, judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene;
step S103, if yes, determining that the log does not need to be printed;
and step S104, if not, acquiring a log printing instance corresponding to the target scene according to the log configuration data corresponding to the target scene, and then printing the log by using the log printing instance.
When a scene needs to be analyzed, a log of the scene may be printed so that the scene may be analyzed through the log of the scene. The target scene refers to a scene in which a log needs to be printed. After the target scene is acquired, the log configuration data corresponding to the target scene can be queried according to the scene identifier of the target scene. In addition, the scene identifier of the target scene may be placed into a transitibleth ready local, TTL for short, which is a component for solving the problem of context transfer during asynchronous execution, and the thread variable transfer function in the thread reuse scene is realized on the basis of the Inheritable thread local. The scene identification is put into the TTL, then the scene identification is obtained from the TTL, and then the log configuration data corresponding to the target scene is inquired according to the obtained scene identification, so that the problem of mixed dishes of the scene identification can be avoided, and the log can be printed according to the log configuration data corresponding to the target scene.
In step S101, log configuration data corresponding to the target scene may be queried according to the scene identifier of the target scene. Therefore, as an embodiment of the present invention, the log printing method may further include a process of configuring log configuration data corresponding to the scene for the scene, and the specific implementation method includes:
(1) a plurality of scenes is determined. If the detailed page of the commodity needs to display the freight rate, the scenario of calling the freight rate calculation interface is called, and then, the scenario of calling the freight rate calculation interface is called as an order system.
(2) And aiming at each scene, acquiring a scene identifier of each scene, configuring a log printing state, a log output mode and a log level corresponding to each scene, and acquiring log configuration data corresponding to each scene to obtain the log configuration data corresponding to a plurality of scenes. After a plurality of scenes are determined, for each scene, configuring log configuration data, specifically, acquiring a scene identifier of the scene, and configuring a log printing state, a log output mode and a log level for the scene according to actual requirements, so that the log configuration data corresponding to the scene can be obtained, that is, the log configuration data corresponding to the plurality of scenes can be obtained.
(3) And storing the log configuration data corresponding to the plurality of scenes into a memory or a database, wherein the target scene is a scene in the plurality of scenes. After the log configuration data is obtained, the log configuration data can be stored in a memory or a database, so that after a log printing request is received, the log configuration data corresponding to the target scene can be quickly inquired from the memory or the database according to the scene identifier of the target scene. In addition, if the log configuration data corresponding to a certain scenario needs to be changed, all application instances need to update the log configuration data in the memory or the database, for example, whether the log configuration data has a change can be queried through a timing task.
In another aspect, the log configuration data may include: log output mode and log level. The log output mode refers to outputting the log to a file, Kafka (i.e., an open source stream processing platform), Elasticsearch (i.e., a search server), or the like. The log level refers to the level of the log, for example, the log level is, from high to low: error, war, info, debug, trace. Where error indicates that while an error event occurs, it still does not affect the continued operation of the system, such as printing error and exception information, and this level may be used to avoid outputting too many logs; wan indicates that a potential error situation occurs, and some information is not error information but also gives some prompts; the info represents that the message emphasizes the running process of the application program on a coarse granularity level, such as printing some interesting or important information, which can be used for outputting some important information of the running of the program in a production environment, but can not be abused to avoid printing too many logs; debug points out that fine-grained information events are very helpful for debugging application programs and are mainly used for printing some running information in the development process; trace is a lower log level and is not generally used. Note that if the log level corresponding to the target scene is a certain level, it means that the log at the certain level and above is required to be printed. If the log level corresponding to the target scene is debug, the logs of error, war, info and debug levels need to be printed corresponding to the target scene.
The print log code refers to a code that needs to perform a print operation, for example, a certain system has 1000 codes, and 100 codes in the 1000 codes are codes that need to perform a log print operation. In the log printing method according to the embodiment of the present invention, the level of the log level corresponding to the target scene and the level of the log level of the print log code need to be analyzed. Specifically, after a scene code corresponding to a target scene is acquired, each print log code in the scene code needs to be analyzed according to the following method: judging whether the log level corresponding to the target scene is higher than the log level of the printing log code; if yes, skipping the printing log code, namely, not needing to print the log; if not, acquiring a log printing instance corresponding to the target scene according to the log configuration data corresponding to the target scene, and then printing the log by using the log printing instance. For example, the log level corresponding to the target scene is war, that is, the logs with the log levels of war and error in the target scene need to be printed, and if the log level of a certain printing log code is info, that is, the log level printed by the certain printing log code is info, the log corresponding to the certain printing log code does not need to be printed, so that the certain printing log code is directly skipped.
According to the log printing method, the log output mode and the log grade corresponding to the target scene can be inquired according to the scene identification of the target scene, then the log grade in the code is compared with the inquired log grade, and the log is printed by combining the inquired log output mode, so that the problems that the logs with different log grades cannot be printed according to different scenes and different output modes cannot be used in different scenes in the conventional log framework are solved, the log configuration data corresponding to the scene is used, the output mode and the printing grade of the log are dynamically controlled, different scene requirements can be met, the practicability is high, and the user experience is good.
The existing log framework has the following problems: logs can be printed in all scenes, and whether the logs are output or not can not be controlled in different scenes. For example, when the freight cost needs to be displayed on the commodity detail page, the freight cost calculation interface is called, and in the scene, the calling amount of the freight cost calculation interface is large and the recorded log significance is not large; and the log of the order system calling the freight calculation interface can be used as an important basis for subsequent freight calculation problem examination or data analysis. In order to solve the above problem, it is necessary to determine whether a scene has a requirement for printing a log for different scenes. Therefore, the log configuration data may include: log print status. And after querying log configuration data corresponding to the target scene, the log printing method may further include: judging whether the log printing state corresponding to the target scene is required to be printed or not; if yes, determining that the target scene needs to print the log, and then printing the log according to the log output mode and the log level corresponding to the target scene; if not, determining that the target scene does not need to print the log.
Fig. 2 is a schematic diagram of a main flow of a log printing method according to an embodiment of the present invention. As shown in fig. 2, the main flow of the log printing method may include:
step S201, receiving a log printing request, obtaining a target scene according to the log printing request, and querying log configuration data corresponding to the target scene according to a scene identifier of the target scene, where the log configuration data includes: log printing state, log output mode and log level;
step S202, judging whether the log printing state corresponding to the target scene is required to be printed, if so, executing step S203, otherwise, executing step S205;
step S203, judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene, if so, executing step S206, otherwise, executing step S204;
step S204, acquiring a log printing instance corresponding to the target scene according to the log configuration data corresponding to the target scene, and then printing a log by using the log printing instance;
step S205, determining that the target scene does not need to print the log;
in step S206, it is determined that the print log code does not require a print log.
It should be noted that, in step S203, the log level corresponding to the target scene is compared with the log level of each print log code corresponding to the target scene, that is, whether each print log code needs to be printed is sequentially determined. For each printing log code, if the log needs to be printed, performing log printing according to step S204; if the log is not required to be printed, the log is not required to be printed according to the log printing code obtained in step S206, and then the next log printing code is skipped until all log printing codes are analyzed.
The log printing method provided by the embodiment of the invention can inquire the log printing state, the log output mode and the log grade corresponding to the target scene according to the scene identifier of the target scene, and then perform log printing according to the inquired log printing state, log output mode and log grade, so that the problems that whether logs are output or not can be controlled in different scenes, logs of different log grades can not be printed aiming at different scenes and different output modes can not be used in different scenes in the conventional log frame are solved, the situation that whether logs are printed or not can be dynamically controlled through log configuration data corresponding to the scenes, the output mode and the print grade of the logs can be dynamically controlled, different scene requirements can be met, the practicability is strong, and the user experience is good.
The existing log framework fixedly uses one or more output modes, so that different output modes used in different scenes cannot be realized. The log printing method provided by the embodiment of the invention can dynamically select the log output mode corresponding to the scene, so that log printing instances corresponding to different log output modes need to be generated, and the log printing instance corresponding to the log output mode can be called according to the log output mode corresponding to the target scene. Therefore, the log printing method of the embodiment of the invention can also comprise a process of generating a log printing instance, and the specific implementation method comprises the following steps:
(1) one or more log configurations are added to the configuration file of the log framework. For example, adding log configuration to the configuration file log4j2.xml of the log frame log4j2, and for example, adding log configuration to the configuration file log back. xml of the log frame log back.
(2) Setting a log output mode corresponding to log configuration for each log configuration, setting the log level corresponding to the log configuration as trace, generating a log printing instance corresponding to the log configuration, and storing the generated log printing instance by using Map; wherein, Key in Map is log output mode, Value is log printing example. That is to say, a logger configuration is newly added in a configuration file of the log frame, a log output mode corresponding to the logger configuration is set, and a log level corresponding to the logger configuration is set as trace, that is, the log level corresponding to the log configuration is set as the lowest level, so that a log print instance corresponding to the logger configuration can be generated. And, the log level corresponding to the log configuration is set to the lowest level, so that the generated log print instance can be used for printing logs of various log levels. And finally, storing the generated log printing instance into the Map, namely storing the generated log printing instance in a Key-Value data structure, wherein Key is a log output mode, and Value is a log printing instance, so that the log printing instance corresponding to the target scene can be inquired from the Map according to the log output mode corresponding to the target scene.
The existing log framework has the problem that logs with different log levels cannot be printed aiming at different scenes, and in order to solve the problem, the log printing method provided by the embodiment of the invention can print logs with different log levels through proxy classes. Therefore, the log printing method may further include: an agent class is defined, and a method for implementing log-level printing in the agent class is defined. That is, a proxy class is defined, and then for each log level, a method that can print the log at the log level in the proxy class is defined, such as a method that can print the log at the log level in the proxy class, such as error, war, info, debug, trace, and the like.
As an embodiment of the present invention, acquiring a log print instance corresponding to a target scene according to log configuration data corresponding to the target scene, and then performing log printing by using the log print instance, may include: screening a log printing example corresponding to the target scene from the Map according to a log output mode corresponding to the target scene; and calling a log level printing method corresponding to the target scene in the proxy class according to the log level corresponding to the target scene, and printing a log corresponding to the target scene according to a log printing instance corresponding to the target scene.
Specifically, the method for printing the log comprises the following steps: and screening out the log printing instance corresponding to the target scene from the Map for storing the log printing instance according to the log output mode corresponding to the target scene, calling a method for printing the log at the log level corresponding to the target scene in the proxy class according to the log level corresponding to the target scene, and printing the log corresponding to the scene according to the log printing instance corresponding to the target scene. That is, the journaling instance is executed in the proxy class.
It should be noted that, the log is printed to determine the actual code position, and if the log is printed corresponding to the 100 th code corresponding to the target scene, the actual code position is the 100 th code corresponding to the target scene. The specific code position determining process comprises the following steps: acquiring a current thread call stack, and traversing call stack elements; if the character string obtained by the getClassName method of the current stack element is the same as the name of the proxy class, and the character string obtained by the getClassName method of the next stack element in the stack is not the same as the name of the proxy class, the file name where the actual code is located and the number of lines where the actual code is located can be obtained from the getFileName method and the getLineNumber method of the next stack element.
Fig. 3 is a schematic diagram of a main flow of a log printing method according to another embodiment of the present invention. As shown in fig. 3, the main flow of the log printing method may include:
step S301, receiving a log printing request, acquiring a target scene according to the log printing request, and acquiring a scene identifier of the target scene from TTL;
step S302, according to the scene identification of the target scene, inquiring log configuration data corresponding to the target scene, wherein the log configuration data comprises: log printing state, log output mode and log level;
step S303, judging whether the log printing state corresponding to the target scene is required to be printed, if so, executing step S304, and if not, executing step S307;
step S304, judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene, if so, executing step S308, otherwise, executing step S305;
s305, screening a log printing instance corresponding to the target scene from the Map according to a log output mode corresponding to the target scene;
step S306, calling a log level printing method corresponding to the target scene in the proxy class according to the log level corresponding to the target scene, and printing the log corresponding to the target scene according to the log printing instance corresponding to the target scene;
step S307, determining that the target scene does not need to print logs;
in step S308, it is determined that the print log code does not require a print log.
In addition, the log printing method according to the embodiment of the present invention further includes: (1) before step S301, configuring log configuration data corresponding to a target scene for the target scene, and then storing the log configuration data corresponding to the target scene in a memory or a database, so that the log configuration data corresponding to the target scene can be queried; (2) before step S305, generating log print instances corresponding to different log output modes, so as to call the log print instance corresponding to the log output mode according to the log output mode corresponding to the target scene; (3) before step S306, an agent class and a method for implementing log-level printing in the agent class are defined, so that a log-level printing method corresponding to a target scene in the agent class can be called according to a log level corresponding to the target scene.
It should be further noted that, in step S304, the log level corresponding to the target scene is compared with the log level of each print log code corresponding to the target scene, that is, whether each print log code needs to be printed is sequentially determined. For each print log code, if the log needs to be printed, performing log printing according to step S305 and step S306; if the log is not required to be printed, the log is not required to be printed according to the log printing code obtained in step S308, and then the next log printing code is skipped until all log printing codes are analyzed.
The log printing method provided by the embodiment of the invention can inquire the log printing state, the log output mode and the log grade corresponding to the target scene according to the scene identifier of the target scene, and then perform log printing according to the inquired log printing state, log output mode and log grade, so that the problems that whether logs are output or not can be controlled in different scenes, logs of different log grades can not be printed aiming at different scenes and different output modes can not be used in different scenes in the conventional log frame are solved, the situation that whether logs are printed or not can be dynamically controlled through log configuration data corresponding to the scenes, the output mode and the print grade of the logs can be dynamically controlled, different scene requirements can be met, the practicability is strong, and the user experience is good.
Fig. 4 is a schematic diagram of main modules of a log printing apparatus according to an embodiment of the present invention. As shown in fig. 4, the main modules of the journal printing apparatus 400 may include: a query module 401, a determination module 402, and a print module 403.
Wherein, the query module 401 may be configured to: receiving a log printing request, acquiring a target scene according to the log printing request, and inquiring log configuration data corresponding to the target scene according to a scene identifier of the target scene; the determining module 402 may be configured to: judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene; the print module 403 may be used to: if so, determining that the log does not need to be printed, otherwise, acquiring a log printing instance corresponding to the target scene according to log configuration data corresponding to the target scene, and then printing the log by using the log printing instance. Wherein the log configuration data may include: log output mode and log level
As an embodiment of the present invention, the log configuration data may further include: log print status. And, the determining module 402 may be further configured to: judging whether the log printing state corresponding to the target scene is required to be printed or not; if yes, determining that the target scene needs to print the log, and then printing the log according to the log output mode and the log level corresponding to the target scene; if not, determining that the target scene does not need to print the log.
As shown in fig. 4, the log printing apparatus 400 may further include a configuration module 404 as an embodiment of the present invention. The configuration module 404 may also be configured to: determining a plurality of scenes; acquiring a scene identifier of each scene, configuring a log printing state, a log output mode and a log level corresponding to each scene, and acquiring log configuration data corresponding to each scene to obtain log configuration data corresponding to a plurality of scenes; storing log configuration data corresponding to a plurality of scenes into a memory or a database; wherein the target scene is a scene of the plurality of scenes.
As an embodiment of the present invention, the printing module 403 may also be used to: screening a log printing example corresponding to the target scene from the Map according to a log output mode corresponding to the target scene; and calling a log level printing method corresponding to the target scene in the proxy class according to the log level corresponding to the target scene, and printing a log corresponding to the target scene according to a log printing instance corresponding to the target scene.
As an embodiment of the present invention, the configuration module 404 may further be configured to: adding one or more log configurations in a configuration file of a log framework; setting a log output mode corresponding to log configuration for each log configuration, setting the log level corresponding to the log configuration as trace, generating a log printing instance corresponding to the log configuration, and storing the generated log printing instance by using Map; wherein, Key in Map is log output mode, Value is log printing example.
As an embodiment of the present invention, the configuration module 404 may further be configured to: an agent class is defined, and a method for implementing log-level printing in the agent class is defined.
According to the log printing device provided by the embodiment of the invention, the log output mode and the log grade corresponding to the target scene can be inquired according to the scene identifier of the target scene, then the log grade in the code is compared with the inquired log grade, and the log is printed by combining the inquired log output mode, so that the problems that the logs with different log grades can not be printed according to different scenes and different output modes can not be used in different scenes in the conventional log frame are solved, the log configuration data corresponding to the scene is used, the output mode and the printing grade of the log are dynamically controlled, the requirements of different scenes can be met, the practicability is high, and the user experience is good.
Fig. 5 illustrates an exemplary system architecture 500 to which the log printing method or the log printing apparatus of the embodiment of the present invention can be applied.
As shown in fig. 5, the system architecture 500 may include terminal devices 501, 502, 503, a network 504, and a server 505. The network 504 serves to provide a medium for communication links between the terminal devices 501, 502, 503 and the server 505. Network 504 may include various connection types, such as wired, wireless communication links, or fiber optic cables, to name a few.
The user may use the terminal devices 501, 502, 503 to interact with a server 505 over a network 504 to receive or send messages or the like. The terminal devices 501, 502, 503 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and the like.
The server 505 may be a server providing various services, for example, a background management server (for example only) providing support during log printing by the user using the terminal devices 501, 502, 503; for another example, the server 505 may perform log printing according to an embodiment of the present invention.
It should be noted that the log printing method provided by the embodiment of the present invention is generally executed by the server 505, and accordingly, the log printing apparatus is generally disposed in the server 505.
It should be understood that the number of terminal devices, networks, and servers in fig. 5 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 6, a block diagram of a computer system 600 suitable for use with a terminal device implementing an embodiment of the invention is shown. The terminal device shown in fig. 6 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present invention.
As shown in fig. 6, the computer system 600 includes a Central Processing Unit (CPU)601 that can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM)602 or a program loaded from a storage section 608 into a Random Access Memory (RAM) 603. In the RAM 603, various programs and data necessary for the operation of the system 600 are also stored. The CPU 601, ROM 602, and RAM 603 are connected to each other via a bus 604. An input/output (I/O) interface 605 is also connected to bus 604.
The following components are connected to the I/O interface 605: an input portion 606 including a keyboard, a mouse, and the like; an output portion 607 including a display such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage section 608 including a hard disk and the like; and a communication section 609 including a network interface card such as a LAN card, a modem, or the like. The communication section 609 performs communication processing via a network such as the internet. The driver 610 is also connected to the I/O interface 605 as needed. A removable medium 611 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 610 as necessary, so that a computer program read out therefrom is mounted in the storage section 608 as necessary.
In particular, according to the embodiments of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method illustrated in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network through the communication section 609, and/or installed from the removable medium 611. The computer program performs the above-described functions defined in the system of the present invention when executed by the Central Processing Unit (CPU) 601.
It should be noted that the computer readable medium shown in the present invention can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present invention, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules described in the embodiments of the present invention may be implemented by software or hardware. The described modules may also be provided in a processor, which may be described as: a processor includes an inquiry module, a judgment module, and a printing module. For example, the query module may be further described as a module that receives a log print request, obtains a target scene according to the log print request, and queries log configuration data corresponding to the target scene according to a scene identifier of the target scene.
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to comprise: receiving a log printing request, acquiring a target scene according to the log printing request, and inquiring log configuration data corresponding to the target scene according to a scene identifier of the target scene, wherein the log configuration data comprises: log output mode and log level; judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene; if yes, determining that the log does not need to be printed; if not, acquiring a log printing instance corresponding to the target scene according to the log configuration data corresponding to the target scene, and then printing the log by using the log printing instance.
According to the technical scheme of the embodiment of the invention, the log output mode and the log grade corresponding to the target scene can be inquired according to the scene identifier of the target scene, then the log grade in the code is compared with the inquired log grade, and the log is printed by combining the inquired log output mode, so that the problems that the logs with different log grades can not be printed according to different scenes and different output modes can not be used in different scenes in the conventional log framework are solved, the data is configured through the logs corresponding to the scenes, the output mode and the printing grade of the logs are dynamically controlled, the requirements of different scenes can be met, the practicability is high, and the user experience is good.
The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (10)

1. A method of printing a journal, comprising:
receiving a log printing request, acquiring a target scene according to the log printing request, and inquiring log configuration data corresponding to the target scene according to a scene identifier of the target scene, wherein the log configuration data comprises: log output mode and log level;
judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene;
if yes, determining that the log does not need to be printed;
if not, acquiring a log printing instance corresponding to the target scene according to the log configuration data corresponding to the target scene, and then printing a log by using the log printing instance.
2. The method of claim 1, wherein the log configuration data comprises: log printing state; and the number of the first and second groups,
after querying log configuration data corresponding to the target scenario, the method further includes:
judging whether the log printing state corresponding to the target scene is required to be printed or not;
if yes, determining that the target scene needs to print the log, and then printing the log according to the log output mode and the log level corresponding to the target scene;
if not, determining that the target scene does not need to print the log.
3. The method of claim 2, further comprising:
determining a plurality of scenes;
acquiring a scene identifier of each scene, configuring a log printing state, a log output mode and a log level corresponding to each scene, and acquiring log configuration data corresponding to each scene to obtain the log configuration data corresponding to the plurality of scenes;
storing the log configuration data corresponding to the scenes into a memory or a database; wherein the content of the first and second substances,
the target scene is a scene of the plurality of scenes.
4. The method according to claim 1, wherein the obtaining a log print instance corresponding to the target scene according to the log configuration data corresponding to the target scene and then performing log printing by using the log print instance comprises:
screening a log printing instance corresponding to the target scene from the Map according to a log output mode corresponding to the target scene;
and calling a log level printing method corresponding to the target scene in the proxy class according to the log level corresponding to the target scene, and printing the log corresponding to the target scene according to the log printing instance corresponding to the target scene.
5. The method of claim 4, further comprising:
adding one or more log configurations in a configuration file of a log framework;
setting a log output mode corresponding to the log configuration for each log configuration, setting the log level corresponding to the log configuration as trace, generating a log printing instance corresponding to the log configuration, and storing the generated log printing instance by using Map; wherein the content of the first and second substances,
key in Map is log output mode, Value is log printing example.
6. The method of claim 4, further comprising:
defining the agent class, and defining a method for implementing log-level printing in the agent class.
7. A log printing apparatus, characterized by comprising:
the query module is configured to receive a log printing request, obtain a target scene according to the log printing request, and query log configuration data corresponding to the target scene according to a scene identifier of the target scene, where the log configuration data includes: log output mode and log level;
the judging module is used for judging whether the log level corresponding to the target scene is higher than the log level of the printing log code corresponding to the target scene;
and the printing module is used for determining that the log does not need to be printed if the log does not need to be printed, acquiring a log printing instance corresponding to the target scene according to the log configuration data corresponding to the target scene if the log does not need to be printed, and then printing the log by using the log printing instance.
8. The apparatus of claim 7, wherein the print module is further configured to:
screening a log printing instance corresponding to the target scene from the Map according to a log output mode corresponding to the target scene;
and calling a log level printing method corresponding to the target scene in the proxy class according to the log level corresponding to the target scene, and printing the log corresponding to the target scene according to the log printing instance corresponding to the target scene.
9. An electronic device, comprising:
one or more processors;
a storage device for storing one or more programs,
when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-6.
10. A computer-readable medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-6.
CN202110025697.8A 2021-01-08 2021-01-08 Log printing method and device Pending CN113778350A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110025697.8A CN113778350A (en) 2021-01-08 2021-01-08 Log printing method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110025697.8A CN113778350A (en) 2021-01-08 2021-01-08 Log printing method and device

Publications (1)

Publication Number Publication Date
CN113778350A true CN113778350A (en) 2021-12-10

Family

ID=78835369

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110025697.8A Pending CN113778350A (en) 2021-01-08 2021-01-08 Log printing method and device

Country Status (1)

Country Link
CN (1) CN113778350A (en)

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110231921A (en) * 2019-06-06 2019-09-13 深圳前海微众银行股份有限公司 Log Method of printing, device, equipment and computer readable storage medium
CN111400128A (en) * 2020-03-04 2020-07-10 平安科技(深圳)有限公司 Log management method and device, computer equipment and storage medium
CN112130781A (en) * 2020-09-22 2020-12-25 平安科技(深圳)有限公司 Log printing method and device, electronic equipment and storage medium

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110231921A (en) * 2019-06-06 2019-09-13 深圳前海微众银行股份有限公司 Log Method of printing, device, equipment and computer readable storage medium
CN111400128A (en) * 2020-03-04 2020-07-10 平安科技(深圳)有限公司 Log management method and device, computer equipment and storage medium
CN112130781A (en) * 2020-09-22 2020-12-25 平安科技(深圳)有限公司 Log printing method and device, electronic equipment and storage medium

Similar Documents

Publication Publication Date Title
CN111177112A (en) Database blocking method and device based on operation and maintenance management system and electronic equipment
CN107644075B (en) Method and device for collecting page information
CN111427701A (en) Workflow engine system and business processing method
CN113794909A (en) Video streaming system, method, server, device, and storage medium
US20220138074A1 (en) Method, electronic device and computer program product for processing data
CN113191889A (en) Wind control configuration method, configuration system, electronic device and readable storage medium
CN111414154A (en) Method and device for front-end development, electronic equipment and storage medium
CN111241048A (en) Web terminal log management method, device, medium and electronic equipment
CN112948138A (en) Method and device for processing message
CN116032614A (en) Container network micro-isolation method, device, equipment and medium
CN113722007B (en) Configuration method, device and system of VPN branch equipment
CN113515271B (en) Service code generation method and device, electronic equipment and readable storage medium
US11675683B2 (en) Method, electronic device, and computer program product for monitoring storage system
CN114168607A (en) Global serial number generation method, device, equipment, medium and product
CN113778350A (en) Log printing method and device
CN114218313A (en) Data management method, device, electronic equipment, storage medium and product
CN113312900A (en) Data verification method and device
CN113448578A (en) Page data processing method, processing system, electronic device and readable storage medium
CN113204425A (en) Method and device for process management internal thread, electronic equipment and storage medium
CN112799863A (en) Method and apparatus for outputting information
CN112559001A (en) Method and device for updating application
CN115484149B (en) Network switching method, network switching device, electronic equipment and storage medium
CN110262756B (en) Method and device for caching data
CN115309612B (en) Method and device for monitoring data
CN110554942A (en) method and device for monitoring code execution

Legal Events

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