Disclosure of Invention
The invention provides a log collection method and device, and aims to solve the problems that the existing log collection method of an intelligent robot triggers log collection only after a program runs, so that the log collection is incomplete, and meanwhile, logs cannot be collected actively according to needs.
In a first aspect, an embodiment of the present invention provides a log collection method, including:
judging whether to collect corresponding logs of the terminal application service;
if the collection is needed, determining the output levels of the collected target application services corresponding to the log collection respectively;
sending a log collection instruction corresponding to a collection preset output level to a terminal;
and respectively receiving the corresponding logs of the preset output level of the target application service, which are sent by the terminal.
Preferably, the determining whether to collect a log corresponding to the terminal application service further includes:
and the server judges whether to collect the corresponding logs of the application service installed on the terminal according to the current time, the current place, the current network condition and the server memory condition.
Preferably, the determining that the target application services for collection respectively correspond to output levels of log collection further includes:
and determining output levels of log collection respectively corresponding to the target application services according to the log states and the fault repairing states.
Preferably, the receiving the logs corresponding to the preset output levels of the target application service and sent by the terminal respectively further includes:
and receiving the corresponding log of the preset output level of the target application service sent by the terminal by the fragments.
Preferably, the method further comprises:
and storing the log corresponding to the preset output level into the preset file according to the preset file address.
Preferably, according to the preset file address, the method further comprises:
and analyzing the logs stored in the preset file, and generating and displaying an analysis result.
According to the technical scheme, the log collection method provided by the embodiment of the invention judges and determines whether to collect the logs through the server, so that the active collection of the server can be realized, and the problem of single trigger of the log collection is avoided; different logs are collected through the output levels, the requirement of the user for collecting logs according to needs can be met, and meanwhile the problem of incomplete log collection is avoided.
In a second aspect, an embodiment of the present invention further provides a log collecting apparatus, including:
the log collection determining module is used for judging whether to collect logs corresponding to the terminal application service;
the output level determining module is used for determining output levels of the collected target application services corresponding to the log collection respectively if the collection is needed;
the instruction sending module is used for sending a log collection instruction corresponding to the collection preset output level to the terminal;
and the log receiving module is used for respectively receiving the logs corresponding to the preset output levels of the target application service and sent by the terminal.
Preferably, the log collection determining module is further configured to determine whether to collect a log corresponding to the application service installed in the terminal according to the current time, the current location, the current network condition, and the server memory condition.
Preferably, the output level determining module is further configured to determine, according to the log status and the failure recovery status, output levels of log collection respectively corresponding to the target application service.
Preferably, the log receiving module is further configured to receive, in a fragmented manner, logs corresponding to the preset output level of the target application service and sent by the terminal.
Preferably, the apparatus further comprises:
and the storage module is used for storing the log corresponding to the preset output level into the preset file according to a preset file address.
Preferably, the apparatus further comprises:
and the log analysis module is used for analyzing the logs stored in the preset file, generating an analysis result and displaying the analysis result.
According to the technical scheme, the log collection device provided by the embodiment of the invention judges and determines whether to collect the logs through the server, so that the active collection of the server can be realized, and the problem of single trigger of the log collection is avoided; different logs are collected through the output levels, the requirement of the user for collecting logs according to needs can be met, and meanwhile the problem of incomplete log collection is avoided.
Detailed Description
The following further describes embodiments of the invention with reference to the drawings. The following examples are only for illustrating the technical solutions of the present invention more clearly, and the protection scope of the present invention is not limited thereby.
Fig. 1 is a flowchart illustrating a log collection method according to an embodiment of the present invention, including:
s101, judging whether a corresponding log of the terminal application service is collected or not;
the server can actively collect logs corresponding to the terminal application service according to the judgment rule; the decision rule may be: according to the daily work schedule of the intelligent robot, selecting the idle time of the intelligent robot to collect logs so as not to influence the daily work efficiency of the intelligent robot; or according to the current network condition of the server, collecting logs when the network speed is high; or according to the current memory condition of the server, when the idle memory is larger than the threshold value, collecting the logs.
Or, the server may determine whether to collect logs of each application service of the terminal according to the monitoring file sent by the terminal. Specifically, the terminal side comprises a monitoring service for monitoring the service state of each application; the monitoring service acquires the state of each application service and uploads a monitoring file to the server according to a preset time interval, wherein the monitoring file comprises the running state of each application service. Specifically, on the terminal side, a plurality of application services are included, each application service corresponding to one or more log files.
By judging and determining whether to perform log collection on the corresponding log of the terminal application service, the switch for log collection can be dynamically controlled. Therefore, the output and the closing of the log can be flexibly controlled, so that complete log data can be collected for subsequent analysis while no extra performance is consumed; meanwhile, a collection instruction is initiatively initiated through the server, intelligent analysis, judgment and collection are carried out, and the traditional passive collapse collection mode is broken through, so that the collection mode is flexible and changeable, and meanwhile, the fault diagnosis of the intelligent robot is more comprehensive.
S102, if collection is needed, determining output levels of collected target application services corresponding to log collection respectively;
specifically, the output level of log collection corresponding to each application service is judged and determined according to a preset rule.
For example, the preset rule may be severity of the fault, such as light fault, moderate fault, and heavy fault; alternatively, the repair time for the failure, such as 1 minute, 10 minutes, 30 minutes, 3 hours, and 2 days; or, the setting can be preset according to the specific requirements of the user.
The output levels are different output requirements corresponding to each log file, and represent abnormal levels generated when corresponding application service runs.
For example, the output levels may be set from low to high as: i (info), v (verbose, display details), d (debug, repairable), w (war), and e (error). Wherein, i represents a log for only informing a user of an error, the abnormal level is the lowest, v represents a detailed information log for sending the error to the user, d represents an error log for informing the user that the error is repairable, w represents an error log for warning the user to pay attention to, e represents a fault log for sending the user, and the abnormal level is the highest.
Or the output level can be represented by 1-5 levels, and the output level is 1 level, which represents the logs of the important function module and the logs of the function module with faults, and the logs needing to be uploaded in real time; outputting logs with 2-level output levels representing the secondary function modules, and uploading the logs according to the instructions of the server; and the output level is 3 levels, the logs represent common functional modules, and the logs are uploaded according to the instructions of the server. When the output level of the log file M is 2 levels and the functional module corresponding to the log file M does not have a fault, the log file M is uploaded to the server when the server sends an upload 2-level log to the intelligent robot.
Alternatively, the output level may be defined according to the specific requirements of the user.
Specifically, at the intelligent robot end, the following steps need to be performed:
a1, establishing monitoring service objects installed on a terminal at an intelligent robot end, and respectively acquiring instances of the monitoring service objects according to the monitoring service objects at main entrances of the application services by each application service, so as to judge whether log collection is performed on the application services;
a2, initializing an object, and respectively calling an initialization method to initialize after each application service acquires the object instance of the monitoring service;
in step a2, each application service receives a monitoring service object instance, and controls whether the LOG recorded by itself should be output to the console of the terminal according to the LOG _ ENABLED and DEBUG flag DEBUG attribute defined in the monitoring service object instance. The collection allowing switch and the debugging mark are both bolean type data, the collection allowing switch LOG _ ENABLED represents whether the intelligent robot end allows a server to collect a switch of a LOG, and the collection allowing switch is in a closed state under the default condition so as to prevent illegal collection of malicious programs; when the server judges that log collection is needed, the collection allowing switch is changed into an open state; the debugging mark is a mark which is set for facilitating debugging access in the debugging process. And meanwhile, the detection service object instance also registers a self-defined broadcast receiver, and the broadcast receiver is responsible for responding to the command, controlling the log switch and starting the service for collecting log information for collecting and uploading the log.
A3, outputting logs, wherein each application service calls an output level method by using the initialized monitoring service object instance, and outputs log information of different levels to a console; the output level method can be customized by a user according to specific requirements.
In the step A3, a new functional package is added, so that the scheme of the native system is enhanced, the native system is more convenient to use, but the code design style of the original Google is not changed, and the program is free from obstacles when being called. In addition, the definition in step A3 has a log level of operation, which is the minimum level minLevel and the maximum level maxLevel, respectively, and a mandatory output parameter, which may break the limitation in step a 2. Specifically, when the forced output is set in step A3, the output is performed at the minimum level and the maximum level in step A3 regardless of the restriction condition of step a 2. For example, when the minimum level of forced output is 2 and the maximum level is 4, the logs with output levels of 2, 3 and 4 are uploaded to the server.
For example,
b1, the Application service acquires the instance of the monitoring service object at the main entrance through logcat. getinstant (String tag), the log collection must be placed at the entrance, the best implementation scheme is to place in the custom Application, and the instantiation of the monitoring service object runs as early as possible. Each LogCat is provided with a designated tag when being instantiated, so that filtering logs can be distinguished conveniently;
b2, after the object instance of the monitoring service is obtained, invoking an init (context) method to initialize the object instance. The initialization of the step is to make default configuration for the created LogCat object, and a control mechanism and an anti-control mechanism are arranged in the LogCat object, so that the encapsulation enables the internal complex mechanism to be simple and concise to call.
B3, calling logCat.i (StringclassName, Stringmsg) and logCat.i (StringclassName, Stringmsg, coolean isOut, StringfileName) and the like, corresponding to the corresponding log levels, according with the log level rule defined by Android, enabling the isOut parameter to break through the whole log switch mechanism, and enabling fileName to specify the local file of the saved log, wherein the log is flexible and changeable.
By controlling the grade of log collection, the problem of difficult analysis caused by huge logs can be solved; uploading is carried out in the range of the designated level, only logs concerned by the user can be analyzed, the analysis time is shortened, and faults and problems are quickly located.
S103, sending a log collection instruction corresponding to a collection preset output level to the terminal;
wherein, the preset output level can be preset according to the requirement of a user.
Specifically, the server can actively control to start the collection service through a reserved background port; and after the collection service is started, the server sends a collection instruction for collecting the logs corresponding to the preset output level to the terminal.
And the collection instruction comprises a preset output level of the log.
For example, if the server sends a log with a collection output level less than 3 to the terminal, the terminal needs to upload logs with output levels 1 and 2 to the server. Wherein, the terminal can be an intelligent robot.
S104, receiving the corresponding logs of the preset output level of the target application service sent by the terminal respectively.
Specifically, after the server sends a collection instruction for collecting the log corresponding to the preset output level to the terminal, at the terminal side, the specific steps include:
first, the collection permission switch is turned on;
then according to the output level of the log in the collection instruction, searching the log corresponding to the output level;
and finally, packaging and uploading the log corresponding to the output level to the server.
And on the side of the server, after uploading the packaged log, the terminal receives the log corresponding to the preset output level.
According to the embodiment, the server judges whether to perform log collection or not, active collection of the server can be realized, and the problem of single trigger of log collection is avoided; different logs are collected through the output levels, the requirement of the user for collecting logs according to needs can be met, and meanwhile the problem of incomplete log collection is avoided.
Further, on the basis of the above method embodiment, S101 further includes:
and the server judges whether to collect the corresponding logs of the application service installed on the terminal according to the current time, the current place, the current network condition and the server memory condition.
Specifically, whether to collect the log of the application service installed in the terminal can be determined by adopting a network long link keep-alive technology, a heartbeat sending detection technology and a comprehensive system condition analysis decision. Wherein the network long link keep-alive technique represents maintaining a long and stable connection of the network; the heartbeat sending detection technology indicates that a notice is sent to a server after a preset time interval to inform that the heartbeat sending detection technology is running; the comprehensive system condition analysis decision represents that the total sum considers a plurality of factors for analysis, for example, whether intelligent log collection needs to be started is judged according to combination of various factors such as the current time, the current location, the current network condition, the current interaction condition, the server memory condition, the overall system operation condition and the like.
For example, the server selects a trigger for collection, such as: and calling a startSave method, sending an instruction by a server to start a diagnosis function, wherein a specific collection command is served in another independent process, and the process is responsible for log collection and packaging uploading, does not participate in other services, and ensures the safety.
Whether log collection is carried out or not is judged through the current specific parameters, and the log collection can be carried out by combining the current specific conditions so as not to influence the daily work of the intelligent robot and improve the efficiency of the log collection.
Further, on the basis of the above embodiment of the method, S102 further includes:
and determining output levels of log collection respectively corresponding to the target application services according to the log states and the fault repairing states.
Specifically, the log state refers to the current operating state of the log, such as whether the log is being updated; the failure modification state refers to a state after the logged application program fails, such as whether the application program is repaired or not.
When the log is in an updating state, the output level of the log can be set to be a lower level so as to prevent the problem of incomplete data caused by uploading the log in the log updating process.
When the application program recorded by the log is repaired after the fault occurs, the log can be set to be at the next highest level so as to upload the log to the server as early as possible; when the log recorded by the application program is not repaired after the fault occurs, the log can be set to the highest level, and the log can be uploaded to the server.
Further, on the basis of the above embodiment of the method, S104 further includes:
and receiving the corresponding log of the preset output level of the target application service sent by the terminal by the fragments.
By adopting the log fragmentation technology, the success rate of log uploading can be improved, the bandwidth consumption is reduced, and the pressure of network transmission is reduced.
Specifically, the intelligent robot can compress and upload the logs in a slicing mode when uploading the logs.
Further, on the basis of the above embodiment of the method, as shown in fig. 2, the method includes:
s201, judging whether a corresponding log of the terminal application service is collected or not;
s202, if collection is needed, determining output levels of collected target application services corresponding to log collection respectively;
s203, sending a log collection instruction corresponding to a collection preset output level to the terminal;
s204, receiving the logs corresponding to the preset output levels of the target application service and sent by the terminal respectively.
S205, storing the log corresponding to the preset output level into the preset file according to the preset file address.
Wherein S201-S204 correspond to S101-S104 in the corresponding embodiment of FIG. 1.
The preset file address can be preset according to the specific requirements of the user.
Specifically, in S205, by setting the thread pool and the file security lock control, it is ensured that the file is correctly, safely, and efficiently written into the preset file.
Further, on the basis of the above embodiment of the method, as shown in fig. 3, the method includes:
s301, judging whether a corresponding log of the terminal application service is collected or not;
s402, if collection is needed, determining output levels of collected target application services corresponding to log collection respectively;
s303, sending a log collection instruction corresponding to a collection preset output level to the terminal;
s304, receiving the logs corresponding to the preset output levels of the target application service sent by the terminal respectively.
S305, storing the log corresponding to the preset output level into the preset file according to the preset file address.
And S306, analyzing the logs stored in the preset file, and generating and displaying an analysis result.
Wherein S301-S205 correspond to S201-S205 in the embodiment corresponding to FIG. 2.
Specifically, through analysis by the server in S306, different error codes are generated and identified, so that the user can clearly preview the data set in the background.
For example, the issue identification may include: the method is not solved, is irrelevant, is solved, cannot be solved at present, delays the solution and specially processes.
By analyzing the log, a complete and intuitive functional diagnosis result can be generated, and a developer can manage the response application service according to needs.
The embodiment divides the responsibility of the whole structural framework, controls the log switch in a dynamic mode, and collects the log information analysis and diagnosis function by using the independent process Service logcat command. The method comprises the steps that a terminal monitors the working state of each system service, and once the abnormal state is detected and reported to a server, the server actively starts to count abnormal logs corresponding to abnormal modules corresponding to the terminal and uniformly forwards the abnormal logs to a log server to generate error codes for developers to perform function diagnosis.
The embodiment can adopt the Logicat command set of the google Android system, and by combining a java thread pool, a multithreading technology, a multiprocess file safe writing technology, a file fragmentation technology and a synthesis technology, the program log collection and uploading form a perfect closed loop, so that the method has the advantages of complete information collection, stable program operation, low performance consumption and intelligent collection, and supports the response of a remote collection instruction, and has the advantages of high safety, strong expansibility, simple operability, technical expansion and continuous innovation and progressive significance.
Fig. 2 is a schematic structural diagram of a log collection apparatus according to an embodiment of the present invention, where the apparatus includes: a log collection determining module 21, an output level determining module 22, an instruction transmitting module 23 and a log receiving module 24; wherein,
the log collection determining module 21 is configured to determine whether to collect a log corresponding to a terminal application service;
the output level determining module 22 is configured to determine, if collection is needed, output levels of the collected target application services corresponding to log collection respectively;
the instruction sending module 23 is configured to send a log collection instruction corresponding to a collection preset output level to the terminal;
the log receiving module 24 is configured to receive logs corresponding to the preset output levels of the target application service and sent by the terminal, respectively.
Specifically, the log collection determining module 21 determines whether to collect a corresponding log of the terminal application service; if the output level determining module 22 needs to perform collection, it determines that the collected target application services respectively correspond to the output levels of log collection; the instruction sending module 23 sends a log collection instruction corresponding to a collection preset output level to the terminal; the log receiving module 24 respectively receives the logs corresponding to the preset output levels of the target application service, which are sent by the terminal. .
According to the embodiment, the server judges whether to perform log collection or not, active collection of the server can be realized, and the problem of single trigger of log collection is avoided; different logs are collected through the output levels, the requirement of the user for collecting logs according to needs can be met, and meanwhile the problem of incomplete log collection is avoided.
Further, on the basis of the above device embodiment, the log collection determining module 21 is further configured to determine whether to collect a log corresponding to an application service installed in the terminal according to the current time, the current location, the current network condition, and the server memory condition.
Further, on the basis of the above apparatus embodiment, the output level determining module 22 is further configured to determine, according to the log status and the failure recovery status, the output levels of the log collections respectively corresponding to the target application services.
Further, on the basis of the above apparatus embodiment, the log receiving module 24 is further configured to receive, in a fragmented manner, the logs corresponding to the preset output level of the target application service and sent by the terminal.
Further, on the basis of the above embodiment of the apparatus, the apparatus further comprises:
and the storage module 25 is configured to store the log corresponding to the output level into the preset file according to a preset file address.
Further, on the basis of the above embodiment of the apparatus, the apparatus further comprises:
and the log analysis module 26 is configured to analyze the logs stored in the preset file, generate an analysis result, and display the analysis result.
The log collection device described in this embodiment may be used to implement the above method embodiments, and the principle and technical effect are similar, which are not described herein again.
The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.
Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware. With this understanding in mind, the above-described technical solutions may be embodied in the form of a software product, which can be stored in a computer-readable storage medium such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the methods described in the embodiments or some parts of the embodiments.
It should be noted that: the above examples are only intended to illustrate the technical solution of the present invention, but not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.