Disclosure of Invention
An object of the embodiments of the present invention is to provide a script-based log collection method, so as to avoid intrusion of log collection on a service executed by a service program, thereby improving performance of the service program when executing the service.
In order to achieve the above object, an embodiment of the present invention discloses a log collection method, where the method includes:
the method comprises the steps that a main process obtains task parameters of a target task to be subjected to log file collection; sending the task parameters to a shell process, and starting the shell process, wherein the main process is as follows: a process for executing the target task;
the shell process starts a log collection thread, sends the task parameters to the log collection thread, and obtains a log file generated by the target task from output data of the target task according to the task parameters;
and the log collection thread monitors the target task according to the task parameters, and when the generation of a log file is monitored, the log file obtained by the shell process is persisted.
Preferably, the method further comprises:
the log collection thread monitors whether the target task is executed completely, and if the target task is executed completely, notification information is sent to the main process;
and the main process finishes the log collection thread and the shell process in sequence according to the received notification information sent by the log collection thread and exits.
Preferably, after the log collection thread monitors that the target task is executed completely, before the main process exits, the method further includes:
the main process acquires an exit code of the target task;
the main process detects whether the target task is abnormally exited or not according to the exit code;
if the main process detects that the target task is abnormally exited, determining the reason for the abnormal exit of the target task, and persisting the exit code and the reason for the abnormal exit;
and if the main process detects that the target task is not abnormal exit, directly persisting the exit code.
Preferably, the persisting the log file obtained by the shell process includes:
and the log collection thread stores the log file obtained by the shell process to a preset third-party database.
Preferably, the task parameters include: a name of the target task.
In order to achieve the above object, an embodiment of the present invention discloses a log collecting device, including:
the process starting module is used for acquiring task parameters of a target task to be subjected to log file collection by a main process; sending the task parameters to a shell process, and starting the shell process, wherein the main process is as follows: a process for executing the target task;
the log acquisition module is used for starting a log collection thread by the shell process, sending the task parameters to the log collection thread and acquiring a log file generated by the target task from output data of the target task according to the task parameters;
and the log storage module is used for monitoring the target task by the log collection thread according to the task parameters and persisting the log file obtained by the shell process when the generation of the log file is monitored.
Preferably, the apparatus further comprises:
the state monitoring module is used for monitoring whether the target task is executed or not by the log collection thread, and sending notification information to the main process if the target task is executed;
and the process quitting module is used for the main process to sequentially end the log collecting thread and the shell process according to the received notification information sent by the log collecting thread and quit.
Preferably, after the state monitoring module monitors that the target task is executed completely, and before the process exit module exits from the main process, the apparatus further includes:
a reason analysis module, configured to acquire, by the host process, an exit code of the target task; detecting whether the target task is abnormally exited or not according to the exit code; if the target task is detected to be abnormally exited, determining the reason for the abnormally exiting of the target task, and persisting the exit code and the reason for the abnormally exiting; and if the target task is detected not to be abnormally exited, directly saving the exit code.
Preferably, the log storage module is specifically configured to monitor the target task by the log collection thread according to the task parameter, and store a log file obtained by the shell process in a preset third-party database when it is monitored that the log file is generated.
In order to achieve the above object, an embodiment of the present invention discloses an electronic device, which includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface, and the memory complete communication with each other through the communication bus;
a memory for storing a computer program;
the processor, when executing the program stored in the memory, implements the method steps of any of the embodiments described above. In yet another aspect of the present invention, there is also provided a computer-readable storage medium having stored therein instructions, which when run on a computer, cause the computer to perform any one of the log collection methods described above.
In yet another aspect of the present invention, the present invention further provides a computer program product containing instructions, which when run on a computer, causes the computer to execute any one of the log collection methods described above.
According to the script-based log collection scheme provided by the embodiment of the invention, a log file generated by a target task is obtained from output data of the target task through a shell process; meanwhile, the target task is monitored through a log collection thread, and when the generation of a log file is monitored, the log file obtained by the shell process is persisted, so that the mutual independence of log collection and service program service business service logic is realized, the invasion of the log collection to the service is avoided, the energy of a service developer is saved, and the performance of executing the service by the script is improved. Of course, it is not necessary for any product or method of practicing the invention to achieve all of the above-described advantages at the same time.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
In order to solve the problem that service execution is affected by overhigh service intrusion of log file collection on a service program in the prior art, the invention provides a log collection method and a log collection device.
The following generally describes a log collection method provided by an embodiment of the present invention.
As shown in fig. 1, which is a system block diagram of the log collection method provided in the embodiment of the present invention, it can be seen from the system block diagram shown in fig. 1 that the log collection process of the log collection method provided in the embodiment of the present invention is completely independent from the business logic inside the target task itself.
The shell process is a child process of the host process and the log collection thread is a child process of the shell process. The shell process is used for collecting the log files generated by the target task, and the log collection thread is used for monitoring the target task and also used for persisting data needing to be stored. Specifically, the data persisted by the log collection thread includes log files collected in the shell thread, and may further include information such as exit codes and abnormal exit reasons of the target task. The persistence process is a process of storing data in a database, for example, the database that can be used for storing data in the embodiment of the present invention includes a MySQL database, a Mongo database, and the like.
In one implementation, the log collection method includes:
the method comprises the steps that a main process obtains task parameters of a target task to be subjected to log file collection; sending the task parameters to a shell process, and starting the shell process, wherein the main process is as follows: a process for executing the target task;
the shell process starts a log collection thread, sends the task parameters to the log collection thread, and obtains a log file generated by the target task from output data of the target task according to the task parameters;
and the log collection thread monitors the target task according to the task parameters, and when the generation of a log file is monitored, the log file obtained by the shell process is persisted.
By the log collection method provided by the embodiment of the invention, mutual independence of log collection and service logic of service program service is realized, invasion of log collection to service is avoided, the energy of service developers is saved, and the performance of script execution service is improved.
The following describes in detail a log collection method provided by an embodiment of the present invention with reference to specific embodiments.
As shown in fig. 2, a schematic flow chart of a log collection method provided in an embodiment of the present invention includes the following steps:
step S201: the method comprises the steps that a main process obtains task parameters of a target task to be subjected to log file collection; and sending the task parameters to a shell process, and starting the shell process.
Wherein, the main process is as follows: a process for executing the target task.
Specifically, the target task may be a task executed by a service program in the electronic device as an execution subject, and the task parameter of the target task may be a name of the target task or information such as an execution time of the target task.
In a specific application, the shell process may be written using python or other computer languages, and may or may not be consistent with the writing language of the target task, which is not limited in the present invention.
Because the same computer language is not needed to be used between the shell process and the target task, the developer is not limited to write the target task and does not need to care whether the used computer language supports the third-party database.
Step S202: and the shell process starts a log collection thread, sends the task parameters to the log collection thread, and obtains a log file generated by the target task from the output data of the target task according to the task parameters.
Specifically, after acquiring task parameters of the target task, the shell process may determine the target task according to the parameters, and may further collect all input and output data of the target task, where the output data of the target task includes a log file generated by the target task in the execution process. Of course, the shell process may also collect only the output data of the target task, or may also collect only the log file generated by the target task. Because the shell process and the target task are two mutually independent processes, the collection process of the shell process does not influence the operation of the target task.
In addition, the shell process can further start a log collection thread and send task parameters of the target task to the log collection thread, so that the log collection thread can identify the target task and perform the next monitoring on the target task.
Step S203: and the log collection thread monitors the target task according to the task parameters, and when the generation of a log file is monitored, the log file obtained by the shell process is persisted.
Specifically, the log collection thread monitors the target task in real time, and if the target task is monitored to generate a log file, the log file collected in the shell process is persisted. Because the log collection thread monitors the target task in real time, the log collection thread collects log files in real time, and the real-time performance of log collection is improved.
In this embodiment, for example, the log file collected in the shell process may be stored in a third-party database. Specifically, the third-party database includes Redis, MySQL, Mongo, and the like.
As can be seen from the above, in the scheme provided in this embodiment, by starting the shell process and the log collection thread, the log collection process and the execution process of the target task are independent from each other, so that intrusion of log collection on the target task is avoided, and stable operation of the target task is facilitated.
In another embodiment of the invention, the host process finishes the shell process and the log collection thread at the same time after the target task is executed. As shown in fig. 3, which is a schematic flow chart of this embodiment, compared with the embodiment shown in fig. 1, step S304 and step S305 are added to this embodiment.
Step S301: the method comprises the steps that a main process obtains task parameters of a target task to be subjected to log file collection; and sending the task parameters to a shell process, and starting the shell process.
Wherein, the main process is as follows: a process for executing the target task.
Step S302: and the shell process starts a log collection thread, sends the task parameters to the log collection thread, and obtains a log file generated by the target task from the output data of the target task according to the task parameters.
Step S303: and the log collection thread monitors the target task according to the task parameters, and when the generation of a log file is monitored, the log file obtained by the shell process is persisted.
Step S304: and the log collection thread monitors whether the target task is executed completely, and if the target task is executed completely, notification information is sent to the main process.
Specifically, when the log collection thread monitors the target task, the monitoring includes monitoring the running state of the target task, where the running state includes that the target task is running or the target task is finished running. And when the log collection thread monitors that the running state of the target task is running, sending notification information to the main process, and notifying the main process of the running state of the target task.
Step S305: and the main process finishes the log collection thread and the shell process in sequence according to the received notification information sent by the log collection thread and exits.
The main process can receive the notification information of the log collection thread, if the notification that the target task is completely operated and sent by the log collection thread is received, the log collection thread and the shell process are sequentially ended, and after the log collection thread and the shell process are quitted, the log collection thread and the shell process are automatically quitted, and the operation state is ended.
Steps S301 to S303 are the same as steps S201 to S203 in the embodiment of the invention shown in fig. 2, and are not described again.
As can be seen from the above, in the solution provided in this embodiment, the main process can learn the running state of the target task according to the monitoring result of the log collection thread on the target task, and when the running state of the target task is running, the log collection thread and the shell process are ended, thereby avoiding waste of resources.
In another embodiment of the invention, the main process detects the exit reason of the target task before exiting. As shown in fig. 4, which is a schematic flow chart of this embodiment, step S405 is added to this embodiment as compared with the embodiment shown in fig. 3.
Step S401: the method comprises the steps that a main process obtains task parameters of a target task to be subjected to log file collection; and sending the task parameters to a shell process, and starting the shell process.
Wherein, the main process is as follows: a process for executing the target task.
Step S402: and the shell process starts a log collection thread, sends the task parameters to the log collection thread, and obtains a log file generated by the target task from the output data of the target task according to the task parameters.
Step S403: and the log collection thread monitors the target task according to the task parameters, and when the generation of a log file is monitored, the log file obtained by the shell process is persisted.
Step S404: and the log collection thread monitors whether the target task is executed completely, and if the target task is executed completely, notification information is sent to the main process.
Step S405: the main process acquires an exit code of the target task; detecting whether the target task is abnormally exited or not according to the exit code; if the target task is detected to be abnormally exited, determining the reason for the abnormally exiting of the target task, and persisting the exit code and the reason for the abnormally exiting; and if the target task is detected not to be abnormal exit, directly saving the exit code.
Each process generates an exit code when exiting, the exit code is generated by a preset rule, and different exit codes can directly reflect different exit reasons of the process, such as: the process normally exits or the process abnormally exits, wherein the process abnormally exits possibly because the process is forcibly stopped, the process is automatically exited after being blocked, and the like.
Specifically, after the main process obtains the information that the target task is executed, the exit code of the target task is further obtained, the exit code is analyzed according to a preset exit code generation rule, the exit code is matched with the exit reason of the process, and whether the target task is abnormally exited or not is judged. If the log file is abnormal exit, the exit code and the abnormal exit reason corresponding to the exit code are saved in the database together with the log file; if the exit is not abnormal, only the exit code is saved.
Step S406: and the main process finishes the log collection thread and the shell process in sequence according to the received notification information sent by the log collection thread and exits.
Steps S401 to S404 are the same as steps S301 to S304 of the embodiment of the present invention shown in fig. 2, and are not repeated here.
As can be seen from the above, in the scheme provided in this embodiment, the host process acquires and analyzes the exit code of the target task, stores the abnormal exit reason of the target task in the database, and supplements the collected log file, so that the running state and the result of the target task are recorded in more detail, which is beneficial for the service developer to subsequently detect and improve the target task.
Corresponding to the log collection method, the embodiment of the application also provides a log collection device.
Fig. 5 is a schematic structural diagram of a log collection device according to an embodiment of the present invention, where the log collection device includes:
a process starting module 510, configured to obtain a task parameter of a target task to be subjected to log file collection by a host process; and sending the task parameters to a shell process, and starting the shell process.
Wherein, the main process is as follows: a process for executing the target task.
And the log obtaining module 520 is configured to start a log collecting thread by the shell process, send the task parameter to the log collecting thread, and obtain a log file generated by the target task from output data of the target task according to the task parameter.
And the log storage module 530 is configured to monitor the target task by the log collection thread according to the task parameters, and when it is monitored that a log file is generated, persist the log file obtained by the shell process.
Specifically, the log storage module 530 is configured to monitor the target task by the log collection thread according to the task parameters, and store the log file obtained by the shell process in a preset third-party database when it is monitored that the log file is generated.
As can be seen from the above, in the present solution, the process starting module 510 starts the shell process and the log collection thread, and the log obtaining module 520 and the log storage module 530 mutually independent the log collection process and the execution process of the target task, so as to avoid the invasion of the log collection to the target task, and facilitate the stable operation of the target task.
Fig. 6 is a schematic structural diagram of a log collection device in another embodiment of the present invention, where the log collection device includes:
the process starting module 610 is used for the main process to obtain task parameters of a target task to be subjected to log file collection; sending the task parameters to a shell process, and starting the shell process, wherein the main process is as follows: a process for executing the target task.
And the log obtaining module 620 is configured to start a log collection thread by the shell process, send the task parameter to the log collection thread, and obtain a log file generated by the target task from output data of the target task according to the task parameter.
And the log storage module 630 is configured to monitor the target task by the log collection thread according to the task parameters, and when it is monitored that a log file is generated, persist the log file obtained by the shell process.
And the state monitoring module 640 is configured to monitor, by the log collection thread, whether the target task is executed completely, and send notification information to the host process if the target task is executed completely.
And a process exit module 650, configured to, by the host process, sequentially end the log collection thread and the shell process according to the received notification information sent by the log collection thread, and exit.
As can be seen from the above, in the present solution, in the state monitoring module 640, the main process monitors the target task in real time according to the log collection thread, knows that the target task is executed in real time, and immediately ends the log collection thread and the shell process, thereby avoiding the waste of resources.
Fig. 7 is a schematic structural diagram of a log collection device in another embodiment of the present invention, which includes:
the process starting module 710 is used for the main process to obtain task parameters of a target task to be subjected to log file collection; sending the task parameters to a shell process, and starting the shell process, wherein the main process is as follows: a process for executing the target task.
And the log obtaining module 720 is configured to start a log collection thread by the shell process, send the task parameter to the log collection thread, and obtain a log file generated by the target task from output data of the target task according to the task parameter.
And the log storage module 730 is used for monitoring the target task by the log collection thread according to the task parameters, and persisting the log file obtained by the shell process when the generation of the log file is monitored.
And a state monitoring module 740, configured to monitor, by the log collection thread, whether the target task is executed completely, and send notification information to the host process if it is detected that the target task is executed completely.
A reason analysis module 750, configured to acquire, by the host process, an exit code of the target task; detecting whether the target task is abnormally exited or not according to the exit code; if the target task is detected to be abnormally exited, determining the reason for the abnormally exiting of the target task, and persisting the exit code and the reason for the abnormally exiting; and if the target task is detected not to be abnormally exited, directly saving the exit code.
And the process exit module 760 is configured to, by the host process, sequentially end the log collection thread and the shell process according to the received notification information sent by the log collection thread, and exit.
As can be seen from the above, in the present solution, the reason analysis module 750 stores the abnormal exit reason of the target task in the database through the acquisition and analysis of the exit code of the target task by the host process, and supplements the collected log file, so that the running state and the result record of the target task are more detailed, which is beneficial to the subsequent detection and improvement of the target task by the service developer.
An embodiment of the present invention further provides an electronic device, as shown in fig. 8, which includes a processor 801, a communication interface 802, a memory 803, and a communication bus 804, where the processor 801, the communication interface 802, and the memory 803 complete mutual communication through the communication bus 804,
a memory 803 for storing a computer program;
the processor 801 is configured to implement the log collection method according to the embodiment of the present invention when executing the program stored in the memory 803.
In an embodiment of the present invention, the log collecting method includes:
the method comprises the steps that a main process obtains task parameters of a target task to be subjected to log file collection; sending the task parameters to a shell process, and starting the shell process, wherein the main process is as follows: a process for executing the target task.
And the shell process starts a log collection thread, sends the task parameters to the log collection thread, and obtains a log file generated by the target task from the output data of the target task according to the task parameters.
And the log collection thread monitors the target task according to the task parameters, and when the generation of a log file is monitored, the log file obtained by the shell process is persisted.
It should be noted that, other embodiments of the log collection method implemented by the processor 801 executing the program stored in the memory 803 are the same as the embodiments mentioned in the previous embodiment, and are not described herein again.
The communication bus mentioned in the electronic device may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The communication bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is shown, but this does not mean that there is only one bus or one type of bus.
The communication interface is used for communication between the electronic equipment and other equipment.
The Memory may include a Random Access Memory (RAM) or a Non-Volatile Memory (NVM), such as at least one disk Memory. Optionally, the memory may also be at least one memory device located remotely from the processor.
The Processor may be a general-purpose Processor, including a Central Processing Unit (CPU), a Network Processor (NP), and the like; but also Digital Signal Processors (DSPs), Application Specific Integrated Circuits (ASICs), Field Programmable Gate Arrays (FPGAs) or other Programmable logic devices, discrete Gate or transistor logic devices, discrete hardware components.
Therefore, according to the scheme, the shell process and the log collection thread are started, the log collection process and the execution process of the target task are mutually independent, the invasion of the log collection to the target task is avoided, and the stable operation of the target task is facilitated; the same computer language is not needed to be used between the shell process and the target task, so that the writing of the target task by developers is not limited, and whether the used computer language supports a third-party database is not needed to be concerned; meanwhile, the real-time performance of log collection is improved due to the fact that the log collection thread monitors the target task in real time and stores the log file in real time.
In yet another embodiment of the present invention, a computer-readable storage medium is further provided, which has instructions stored therein, and when the instructions are executed on a computer, the instructions cause the computer to execute the log collection method described in any one of the above embodiments.
In yet another embodiment, there is also provided a computer program product containing instructions which, when run on a computer, cause the computer to perform the log collection method of any of the above embodiments.
In the above embodiments, the implementation may be wholly or partially realized by software, hardware, firmware, or any combination thereof. When implemented in software, may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When loaded and executed on a computer, cause the processes or functions described in accordance with the embodiments of the invention to occur, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a network of computers, or other programmable device. The computer instructions may be stored in a computer readable storage medium or transmitted from one computer readable storage medium to another, for example, from one website site, computer, server, or data center to another website site, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, Digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device, such as a server, a data center, etc., that incorporates one or more of the available media. The usable medium may be a magnetic medium (e.g., floppy Disk, hard Disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., Solid State Disk (SSD)), among others.
It is noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on the differences from the other embodiments.
In particular, as for the apparatus embodiment, since it is substantially similar to the method embodiment, the description is relatively simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The above description is only for the preferred embodiment of the present invention, and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention shall fall within the protection scope of the present invention.