Abnormal log detection method and device
Through the method of TF-IDF weight and dynamic threshold adjustment, the problem of low efficiency of manual analysis is solved, automatic abnormal log detection is realized, efficiency and accuracy are improved, and missed detections and false detections are avoided.
Patent Information
- Application Number
- CN202410934900.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-11
- Publication Date
- 2024-10-18
AI Technical Summary
In the existing technology, the efficiency of detecting abnormal logs based on manual log file analysis is low, which can easily lead to missed detections and false detections, and cannot effectively respond to unknown abnormal situations.
The TF-IDF weight is used to determine the target score of log data, and by adjusting the initial anomaly threshold multiple times, the anomaly detection threshold is dynamically adjusted to automatically detect anomalies in logs, eliminate the impact of differences in score distribution, and improve detection accuracy.
It realizes automatic abnormal log detection without manual analysis, improves detection efficiency and accuracy, avoids missed detections and false detections, and dynamically adjusts the threshold to make abnormal log detection more comparable and accurate.
Smart Images

Figure CN118798167A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of data processing technology, and in particular to a method and device for detecting abnormal logs. Background Art
[0002] In modern large-scale software systems, log files are an important means of recording system operation status and user operations. By analyzing log files and determining abnormal log data in log files, abnormal behaviors, errors, and potential problems in the system can be discovered, thereby improving system stability.
[0003] In the related art, abnormal log data in the log files is detected based on manual review and analysis of the log files. However, since log files usually include a large amount of log data, the method of detecting abnormal log data in the log files based on manual log file analysis may lead to reduced efficiency in determining abnormal logs, and may also lead to missed detections and false detections of abnormal logs. Summary of the invention
[0004] The present disclosure provides a method and device for detecting abnormal logs, which can at least solve one of the technical problems in the related art to a certain extent. The technical solution of the present disclosure is as follows:
[0005] According to a first aspect of an embodiment of the present disclosure, a method for detecting abnormal logs is provided, comprising: obtaining multiple log data from a log file within a specified time period; for any log data from the multiple log data, determining a target score for the any log data based on a term frequency-inverse document frequency (TF-IDF) weight of any word segment in the any log data, wherein the target score is used to indicate a degree of abnormality of the any log data; based on the target scores of the multiple log data, adjusting a set initial abnormal threshold multiple times to obtain a target abnormal threshold; comparing the target score of the any log data with the target abnormal threshold to determine whether the any log data is abnormal.
[0006] According to a second aspect of an embodiment of the present disclosure, a device for detecting abnormal logs is provided, comprising: an acquisition module, used to acquire multiple log data in a log file within a specified time period; a first determination module, used to determine, for any one of the multiple log data, a target score of the any one of the log data according to the TF-IDF weight of any word in the any one of the log data, wherein the target score is used to indicate the degree of abnormality of the any one of the log data; an adjustment module, used to adjust a set initial abnormal threshold multiple times based on the target scores of the multiple log data to obtain a target abnormal threshold; and a second determination module, used to compare the target score of the any one of the log data with the target abnormal threshold to determine whether the any one of the log data is abnormal.
[0007] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the method for detecting abnormal logs as described in the embodiment of the first aspect of the present disclosure.
[0008] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the method for detecting abnormal logs as described in the embodiment of the first aspect of the present disclosure.
[0009] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, including: a computer program, which, when executed by a processor, implements the method for detecting abnormal logs as described in the embodiment of the first aspect of the present disclosure.
[0010] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:
[0011] In the technical scheme, for any log data in a log file within a specified period of time, the target score of any log data is determined according to the TF-IDF weight of any word in the log data, wherein the target is used to indicate the degree of abnormality of any log data; further, based on the degree of abnormality of multiple log data, the set initial abnormal threshold is adjusted multiple times to obtain a target abnormal threshold, and the target score of any log data is compared with the target abnormal threshold obtained through the multiple adjustment processes to determine whether any log data is abnormal, thereby dynamically adjusting the threshold of log data anomaly detection based on the degree of abnormality of multiple log data, avoiding missed detection or false detection of abnormal logs due to excessively high or low target abnormal thresholds, and realizing automatic detection of abnormal logs without manual analysis of log files, thereby improving the detection efficiency of abnormal logs; wherein, based on the target scores of multiple log data, the multiple log data are determined. According to the corresponding initial abnormal log data ratio, and then based on the abnormal ratio of multiple log data, the initial abnormal threshold is adjusted to obtain the target abnormal threshold, which improves the accuracy of the target abnormal threshold. The adjusted target abnormal threshold can make the abnormal log data ratio within an appropriate range, effectively avoiding missed detection or false detection of abnormal logs; in addition, when determining the target score of any log data, it is calculated based on the TF-IDF weight of any word in any log data, the TF-IDF average score of multiple log data and the TF-IDF standard deviation, which eliminates the influence of the distribution difference of the target score between different log data, makes the target scores between different log data more comparable, and improves the accuracy of determining the target score of any log data. Therefore, the target score of any log data is compared with the target abnormal threshold to determine whether any log data is abnormal, thereby improving the accuracy of abnormal log detection.
[0012] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.
[0014] Figure 1 It is a flowchart of a method for detecting abnormal logs shown in the first embodiment of the present disclosure;
[0015] Figure 2 is a flow chart of a method for detecting abnormal logs according to a second embodiment of the present disclosure;
[0016] Figure 3is a flowchart of a method for detecting abnormal logs according to the third embodiment of the present disclosure;
[0017] Figure 4 is a flowchart of a method for detecting abnormal logs according to a fourth embodiment of the present disclosure;
[0018] Figure 5 It is a schematic diagram of the principle of the abnormal log detection method shown in the embodiment of the present disclosure;
[0019] Figure 6 is a structural schematic diagram of an abnormal log detection device shown in the fifth embodiment of the present disclosure;
[0020] Figure 7 It is a schematic structural diagram of an electronic device shown in an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION
[0021] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.
[0022] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0023] It should be noted that in the technical solution of the present disclosure, the collection, storage, use, processing, transmission, provision and disclosure of user personal information are all carried out with the user's consent, and are in compliance with relevant laws and regulations and do not violate public order and good morals.
[0024] At present, the detection method of abnormal logs still relies on manual review of log files and analysis, which is inefficient and error-prone. In the related art, automated anomaly detection tools based on rules or pattern matching are used to detect abnormal logs, but these methods can usually only detect predefined abnormal patterns and cannot deal with unknown abnormal situations.
[0025] Therefore, in order to solve the above problems, the present disclosure proposes a method and device for detecting abnormal logs.
[0026] The following describes the abnormal log detection method and device according to the embodiments of the present disclosure with reference to the accompanying drawings.
[0027] Figure 1 It is a flowchart of the abnormal log detection method shown in the first embodiment of the present disclosure.
[0028] like Figure 1 As shown, the abnormal log detection method includes the following steps:
[0029] Step 101: Acquire multiple log data in a log file within a specified time period.
[0030] As a possible implementation manner, a log file within a specified period is obtained from a server, wherein the format of the log file is a text format.
[0031] As an example, all log data in the log file are directly used as multiple log data.
[0032] In order to improve the detection efficiency of abnormal logs, as another example, the log data in the log file is cleaned to remove some unnecessary information to obtain multiple log data, wherein the unnecessary information includes: timestamp, Internet Protocol Address (IP) address and other information.
[0033] Step 102 : for any piece of log data among the multiple pieces of log data, determine a target score for any piece of log data according to the TF-IDF weight of any word in any piece of log data.
[0034] The target score is used to indicate the abnormality of any log data.
[0035] In order to improve the detection efficiency of abnormal logs, as a possible implementation method, for any log data among multiple log data, the TF-IDF weight of any word in any log data is determined, that is, the importance of any word in each log data is determined, and the target score of any log data is automatically determined according to the importance of any word in each log data; wherein the target score is used to indicate the abnormality of any log data, so as to detect abnormal logs based on the abnormality of any log data, thereby improving the accuracy of log anomaly detection.
[0036] Step 103 : Based on the target scores of the plurality of log data, the set initial abnormality threshold is adjusted multiple times to obtain a target abnormality threshold.
[0037] In order to avoid missed detection or false detection of abnormal logs due to the target abnormal threshold being too high or too low, as a possible implementation method, the abnormal log detection threshold is dynamically adjusted based on the abnormality degree of multiple log data, that is, the initial abnormal threshold is adjusted multiple times to obtain the target abnormal threshold.
[0038] Step 104 : Compare the target score of any log data with the target abnormality threshold to determine whether any log data is abnormal.
[0039] As a possible implementation method, the target score of any log data is compared with the target abnormality threshold to obtain a comparison result, and whether any log data is abnormal is determined based on the comparison result.
[0040] As an example, in response to the comparison result indicating that the target score of any log data is greater than the target abnormality threshold, it is determined that any log data is abnormal.
[0041] For example, if the target score of any log data is 3.01 and the target abnormality threshold is 3, and if the target score of any log data is greater than the target abnormality threshold, the log data is determined to be abnormal.
[0042] As another example, in response to the comparison result indicating that the target score of any log data is less than or equal to the target abnormality threshold, it is determined that any log data is not abnormal.
[0043] For example, if the target score of any log data is 2.91 and the target abnormality threshold is 3, and the target score of any log data is less than the target abnormality threshold, then the log data is determined to be non-abnormal.
[0044] In order to display the abnormal log data more intuitively, as an example, the abnormal log data is marked, such as adding tags or adding the abnormal log data to a specified file, and then the abnormal log data is displayed in the form of a chart or table, and the abnormal log data is parsed and summarized to generate an analysis report, and the analysis report is sent to relevant personnel so that the relevant personnel can promptly understand the relevant situation of the abnormal log data.
[0045] In summary, according to the TF-IDF weight of any word in any log data, the target score of any log data is automatically determined, wherein the target score is used to indicate the degree of abnormality of any log data; further, based on the degree of abnormality of multiple log data, the set initial abnormal threshold is adjusted multiple times to obtain the target abnormal threshold, and the target score of any log data is compared with the target abnormal threshold obtained through the multiple adjustment processes to determine whether any log data is abnormal, thereby realizing dynamic adjustment of the threshold of log data anomaly detection based on the degree of abnormality of multiple log data, avoiding missed detection or false detection of abnormal logs due to excessively high or low target abnormal threshold, and realizing the detection of abnormal logs without manual analysis of log files, thereby improving the detection efficiency of abnormal logs.
[0046] In order to clearly illustrate how the set initial abnormality threshold is adjusted multiple times based on the target scores of multiple log data in the above embodiment to obtain the target abnormality threshold, the present disclosure proposes another abnormal log detection method.
[0047] Figure 2 It is a flowchart of a method for detecting abnormal logs shown in the second embodiment of the present disclosure.
[0048] like Figure 2 As shown, the abnormal log detection method includes the following steps:
[0049] Step 201: Acquire multiple log data in a log file within a specified time period.
[0050] Step 202 : for any piece of log data among the multiple pieces of log data, determine a target score for any piece of log data according to the TF-IDF weight of any word in any piece of log data.
[0051] The target score is used to indicate the abnormality of any log data.
[0052] Step 203 : Compare the target score of each of the multiple log data with the initial abnormal threshold value to determine the initial abnormal log data ratio corresponding to the multiple log data.
[0053] As a possible implementation method, the target score of each log data in the multiple log data is compared with the initial abnormality threshold, and whether each log data is suspected of having an abnormality is determined based on the comparison result. For example, log data with a target score greater than the initial abnormality threshold is determined as log data suspected of having an abnormality. Then, based on the comparison of the number of log data suspected of having an abnormality with the number of multiple log data, the initial abnormal log data ratio is obtained.
[0054] Step 204 , according to the abnormal log data ratio, the initial abnormal threshold is adjusted multiple times to obtain a target abnormal threshold.
[0055] In order to avoid missed detection or false detection of abnormal logs due to the target abnormality threshold being too high or too low, as a possible implementation method, based on the initial abnormal log data ratio, the initial abnormality threshold is adjusted multiple times to obtain the target abnormality threshold. Based on the target abnormality threshold, the abnormal log data ratio is made within an appropriate range to avoid missed detection or false detection of abnormal logs.
[0056] Among them, the first adjustment process in the multiple adjustment processes includes: in response to the abnormal log data ratio being greater than a first ratio threshold value (e.g., 0.05), using a first set step size (e.g., 0.1) to increase the initial abnormal threshold value to obtain the abnormal threshold value obtained in the first adjustment process; in response to the abnormal log data ratio being less than a second ratio threshold value (e.g., 0.01), using a second set step size (e.g., 0.15) to lower the initial abnormal threshold value to obtain the abnormal threshold value obtained in the first adjustment process.
[0057] Among them, the non-first adjustment process in the multiple adjustment processes (i.e., the i-th adjustment process, i is a positive integer greater than 1) includes the following steps:
[0058] 1. Determine whether i is greater than the set adjustment times; that is, determine whether the adjustment times are greater than the set adjustment times (e.g., 100 times);
[0059] 2. If it is greater than, the adjustment process ends, and the abnormal threshold obtained in the (i-1)th adjustment process is used as the target abnormal threshold;
[0060] 3. If not, then calculate the abnormal log data ratio of the i-1th adjustment process according to the abnormal threshold value obtained in the i-1th adjustment process, and determine whether the abnormal log data ratio of the i-1th adjustment process is greater than or equal to the second ratio threshold value and less than or equal to the first ratio threshold value;
[0061] 4. If yes, the adjustment process ends and the abnormal threshold obtained in the (i-1)th adjustment process is used as the target abnormal threshold;
[0062] 5. If the proportion of abnormal log data in the i-1th adjustment process is greater than the first proportion threshold, the abnormal threshold obtained in the i-1th adjustment process is increased by using the first set step size to obtain the abnormal threshold obtained in the i-th adjustment process;
[0063] 6. If the abnormal log data ratio of the i-1th adjustment process is less than the second ratio threshold, the abnormal threshold obtained in the i-1th adjustment process is lowered by using the second set step size to obtain the abnormal threshold obtained in the i-th adjustment process.
[0064] Step 205 : Compare the target score of any log data with the target abnormality threshold to determine whether any log data is abnormal.
[0065] It should be noted that the execution process of steps 201 to 202 and step 205 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0066] In summary, the target score of each log data in the multiple log data is compared with the initial abnormality threshold to determine the initial abnormal log data ratio corresponding to the multiple log data; according to the abnormal log data ratio, the initial abnormality threshold is adjusted multiple times to obtain the target abnormality threshold. Thus, based on the abnormal ratio of the multiple log data, the target abnormality threshold for abnormal log detection is adjusted, which improves the accuracy of the target abnormality threshold, thereby avoiding missed detection or false detection of abnormal logs due to excessively high or low target abnormality threshold.
[0067] In order to clearly explain how in the above embodiment, for any one of the multiple log data, the target score of any one of the log data is determined according to the TF-IDF weight of any word in any one of the log data, the present disclosure proposes another abnormal log detection method.
[0068] Figure 3 It is a flowchart of a method for detecting abnormal logs shown in the third embodiment of the present disclosure.
[0069] like Figure 3 As shown, the abnormal log detection method includes the following steps:
[0070] Step 301: Acquire multiple log data in a log file within a specified time period.
[0071] Step 302 : for any piece of log data among the multiple pieces of log data, determine the TF-IDF score of any piece of log data according to the TF-IDF weight of any word in any piece of log data.
[0072] As a possible implementation method, multiple word segments in any log data are obtained; and the TF-IDF score of any log data is determined according to the sum of the TF-IDF weights of the multiple word segments.
[0073] That is to say, after each log data is segmented, a segmentation sequence will be obtained, the TF-IDF weights of multiple segmentations in the segmentation sequence are added, and the result of the addition is used as the TF-IDF score of the corresponding log data.
[0074] Step 303: Determine a target score for any piece of log data according to the TF-IDF score of any piece of log data.
[0075] In order to eliminate the impact of the distribution difference of scores between different log data and make the scores between different log data more comparable, as a possible implementation method, the TF-IDF average score and TF-IDF standard deviation of multiple log data are determined according to the TF-IDF scores of multiple log data; for any log data, the difference between the TF-IDF score of any log data and the TF-IDF standard deviation is determined; according to the ratio of the difference to the TF-IDF average score, the target score of any log data is determined.
[0076] That is to say, the TF-IDF scores of multiple log data are averaged to obtain the average TF-IDF score of multiple log data, and the standard deviation of the TF-IDF scores of multiple log data is calculated. Then, for any log data, the target score is determined using the following formula:
[0077]
[0078] Among them, Z i represents the target score of the i-th log data among multiple log data, x i represents the TF-IDF score of the i-th log data, μ represents the average TF-IDF score of multiple log data, and σ represents the standard deviation of the TF-IDF scores of multiple log data.
[0079] Step 304 : Based on the target scores of the plurality of log data, the set initial abnormality threshold is adjusted multiple times to obtain a target abnormality threshold.
[0080] Step 305 : Compare the target score of any log data with the target abnormality threshold to determine whether any log data is abnormal.
[0081] It should be noted that the execution process of step 301 and steps 304 to 305 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0082] In summary, for any log data among multiple log data, the TF-IDF score of any log data is determined according to the TF-IDF weight of any word in any log data; the target score of any log data is determined according to the TF-IDF score of any log data, thereby automatically determining the target score of any log data according to the importance of any word in each log data, thereby improving the accuracy of determining the target score of any log data, thereby detecting abnormal logs based on the target score of any log data, and improving the detection efficiency and accuracy of abnormal logs.
[0083] Based on any of the above embodiments, according to the TF-IDF weight of any word in any log data, before determining the target score of any log data, the TF-IDF weight of any word needs to be determined. Figure 4 The embodiments are described in detail.
[0084] Figure 4 It is a flowchart of a method for detecting abnormal logs shown in the fourth embodiment of the present disclosure.
[0085] like Figure 4 As shown, the detection of abnormal logs includes the following steps:
[0086] Step 401: Acquire multiple log data in a log file within a specified time period.
[0087] Step 402 : for any piece of log data among the plurality of log data, for any word in any piece of log data, determine the number of occurrences of any word in each piece of log data.
[0088] As a possible implementation, any log data in the multiple log data is segmented to obtain a segmentation sequence of each log data, and for any segmentation in the segmentation sequence of each log data, the number of occurrences of the any segmentation in each log is determined.
[0089] For example, there are two log data, log data 1: [INFO] User 123456 logged in; log data 2: [ERROR] Failed to connect to database; then, the two log data are segmented to obtain the following segmentation sequences: the segmentation sequence of log data 1: [INFO, User, 123456, logged, in]; the segmentation sequence of log data 2: [ERROR, Failed, to, connect, to, database]; count the number of times each word appears in each log, for example, "INFO" appears 1 time in log data 1 and 0 times in log data 2, "User" appears 1 time in log data 1 and 0 times in log data 2; "123456" appears 1 time in log data 1 and 0 times in log data 2, and so on, "database" appears 0 times in log data 1 and 1 time in log data 2.
[0090] Step 403: Generate an A×B word frequency matrix according to the number of occurrences of A word segments in B log data.
[0091] Among them, the element in the mth row and nth column in the word frequency matrix is used to indicate the number of occurrences of the mth word segment in the nth log data, m is a positive integer not greater than A, n is a positive integer not greater than B, A is the total number of word segments in the multiple log data, and B is the total number of the multiple log data.
[0092] That is to say, based on the number of occurrences of each word segment in the multiple log data, an A×B word frequency matrix is generated.
[0093] Step 404: Determine the TF and IDF of any word segment according to the word frequency matrix.
[0094] As a possible implementation method, the term frequency (TF) of word i in log data j can be expressed as tf i,j , the inverse document frequency (IDF) of word i can be expressed as df i represents the number of log data containing word i, and N represents the total number of log data.
[0095] Step 405: Determine the TF-IDF weight of any word segmentation according to the TF and IDF of any word segmentation.
[0096] As a possible implementation method, the term frequency TF and the inverse document frequency IDF of any word segment are multiplied to obtain the TF-IDF weight of any word segment, which is specifically expressed by the following formula:
[0097]
[0098] Among them, tfidf i,j represents the TF-IDF weight of word i in log data j, N represents the total number of log data, tf i,j represents the TF of word i in log data j, Represents the IDF of word i.
[0099] Step 406: Determine a target score for any piece of log data according to the TF-IDF weight of any word in any piece of log data.
[0100] The target score is used to indicate the abnormality of any log data.
[0101] Step 407 : Based on the target scores of the plurality of log data, the set initial abnormality threshold is adjusted multiple times to obtain a target abnormality threshold.
[0102] Step 408 : Compare the target score of any log data with the target abnormality threshold to determine whether any log data is abnormal.
[0103] It should be noted that the execution process of step 401 and steps 407 to 408 can be implemented in any manner in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0104] In summary, for any log data in multiple log data, for any word in any log data, the number of occurrences of any word in each log data is determined; according to the number of occurrences of each word in the multiple log data, an A×B word frequency matrix is generated; according to the word frequency matrix, the TF and IDF of any word are determined; according to the TF and IDF of any word, the TF-IDF weight of any word is determined, thereby effectively determining the TF and IDF of each word, and thus, based on the TF and IDF of each word, accurately generating the TF-IDF weight of each word.
[0105] Based on any embodiment of the present disclosure, Figure 5 As shown, the abnormal log detection method of the embodiment of the present disclosure can also be implemented based on the following steps:
[0106] 1. Collection and preprocessing of log files
[0107] 1.1. Log file collection
[0108] Log files are logs that record information such as user access and system events. Assume that log files are obtained from the server of the application over a period of time and stored in text format;
[0109] 1.2. Log file preprocessing
[0110] In the log file preprocessing stage, the log data in the collected log files must first be cleaned to remove some unnecessary information, such as timestamps, IP addresses, etc., which do not affect the detection results of abnormal logs.
[0111] For example, taking the log data "2024-04-30 14:30:45 [INFO] User 123456 logged in from IP address 192.168.1.100" as an example, you need to remove the timestamp and IP address to get "[INFO] User123456 logged in".
[0112] 2. Text feature extraction and weight calculation
[0113] 2.1. Word segmentation
[0114] Each log data is segmented to obtain a sequence of words, which can be single words, phrases, or words defined based on domain knowledge.
[0115] 2.2. Constructing word frequency matrix (document-word frequency matrix)
[0116] In the process of constructing the document-term frequency matrix, each log data is represented as a vector, in which each element represents the frequency of occurrence of the word in the corresponding log. This process can be expressed by the following formula:
[0117]
[0118] Among them, tf i,j represents the frequency of word i in log data j, n i,j is the number of times word i appears in log data j; ∑ k n k,j is the total number of words in log data j; k represents the index or identifier of all words in the log file;
[0119] For example, there are two logs: Log data 1: [INFO] User 123456 logged in; Log data 2: [ERROR] Failed to connect to database; First, perform word segmentation on the two logs to obtain the following word sequences: Log data 1: [INFO, User, 123456, logged, in]; Log data 2: [ERROR, Failed, to, connect, to, database];
[0120] Next, as shown in Table 1, the number of occurrences of each word in each log data is counted to obtain a document-word frequency matrix;
[0121] Table 1 The number of occurrences of each word in each log data
[0122] Words Log data 1 Log data 2 [INFO] 1 0 User 1 0 123456 1 0 logged 1 0 in 1 0 [ERROR] 0 1 Failed 0 1 to 0 2 connect 0 1 database 0 1
[0123] 2.3. Calculate TF-IDF weight
[0124] In the process of calculating TF-IDF weights, the document-word frequency matrix is weighted to reduce the weight of common words and highlight the importance of rare words. This process can be expressed by the following formula:
[0125]
[0126] Among them, tfidf i,j is the TF-IDF weight of word i in log data j, N is the total number of log data, df i is the number of logs containing word i;
[0127] Based on the above formula, the TF-IDF weight of each word is obtained, and the TF-IDF weight is used to indicate the importance of a word.
[0128] 3. Anomaly detection and abnormal log marking
[0129] 3.1. Defining anomaly detection metrics
[0130] In the anomaly detection stage, anomaly detection indicators need to be defined in order to identify abnormal logs. Common indicators include the TF-IDF weight of a word, the frequency of word occurrence, the relevance of a word, etc. In the embodiment of the present disclosure, the anomaly detection indicator is exemplified as the TF-IDF weight of a word.
[0131] 3.2 Anomaly Detection Algorithm
[0132] The Z-score anomaly detection algorithm is based on the properties of the standard normal distribution. It calculates the deviation between the TF-IDF value of the log data and the mean, and then normalizes the deviation to a multiple of the standard deviation to determine whether it is an outlier. The specific expression is as follows:
[0133]
[0134] Among them, x i represents the TF-IDF value of a log data (the sum of the TF-IDF weights of all the words in the log data), μ represents the average TF-IDF value of all log data, and σ represents the standard deviation of the TF-IDF value of all log data. i ∣ is greater than the target abnormal threshold, then x is considered i It is abnormal log data.
[0135] The target abnormal threshold is obtained by dynamically adjusting the initial abnormal threshold. The specific steps are as follows:
[0136] (1) When the number of iterations is less than the set number of iterations, calculate the Z of each log data. i ;
[0137] (2) By comparing the Z of each log data i and the initial abnormal threshold to determine whether the log data is suspected to be abnormal;
[0138] (3) Based on the suspected abnormal log data, calculate the abnormal log data ratio, that is, the ratio of log data exceeding the threshold to the total log data;
[0139] (4) Adjust the initial abnormal threshold according to the abnormal log data ratio so that the abnormal log data ratio is within a certain range (between 0.01 and 0.05);
[0140] (5) Stop condition: When the difference between the adjusted threshold and the previous threshold is less than 0.1, or the number of iterations is greater than or equal to the set number of iterations, the iteration is stopped.
[0141] 3.3. Mark abnormal logs
[0142] According to the results of anomaly detection, the abnormal log data is marked and subsequently processed or analyzed. For example, special tags can be added to the abnormal log data or the abnormal log data can be recorded in a special file.
[0143] 4. Abnormal log visualization and analysis report
[0144] 4.1 Visualization
[0145] In the abnormal log data visualization stage, the abnormal log data is displayed in the form of charts, tables, etc., so that users can intuitively understand the statistical information of the abnormal log data. Among them, commonly used visualization tools include matplotlib, Seaborn, etc.;
[0146] 4.2. Generate analysis report
[0147] In the analysis report generation stage, the abnormal log data is sorted and summarized for trend analysis, abnormal cause analysis, and abnormal solution, forming a detailed analysis report, which is then sent to relevant personnel. The analysis report may include text descriptions, charts, tables, etc.
[0148] Corresponding to the abnormal log detection method provided in the above-mentioned embodiment, the present disclosure also provides an abnormal log detection device. Since the abnormal log detection device provided in the embodiment of the present disclosure corresponds to the abnormal log detection method provided in the above-mentioned embodiment, the implementation method of the abnormal log detection method is also applicable to the abnormal log detection device provided in the embodiment of the present disclosure, and will not be described in detail in the embodiment of the present disclosure.
[0149] Figure 6 It is a schematic diagram of the structure of an abnormal log detection device shown in the fifth embodiment of the present disclosure.
[0150] like Figure 6 As shown, the abnormal log detection device 600 includes: an acquisition module 610, a first determination module 620, an adjustment module 630 and a second determination module 640.
[0151] Among them, the acquisition module 610 is used to obtain multiple log data from the log file within a specified time period; the first determination module 620 is used to determine the target score of any log data among the multiple log data according to the term frequency-inverse document frequency TF-IDF weight of any word in any log data, wherein the target score is used to indicate the degree of abnormality of any log data; the adjustment module 630 is used to adjust the set initial abnormality threshold multiple times based on the target scores of the multiple log data to obtain the target abnormality threshold; the second determination module 640 is used to compare the target score of any log data with the target abnormality threshold to determine whether any log data is abnormal.
[0152] As a possible implementation method of the embodiment of the present disclosure, the adjustment module 630 is used to compare the target score of each log data in the multiple log data with the initial abnormality threshold to determine the initial abnormal log data ratio corresponding to the multiple log data; according to the abnormal log data ratio, the initial abnormality threshold is adjusted multiple times to obtain the target abnormality threshold.
[0153] As a possible implementation method of the embodiment of the present disclosure, the adjustment module 630 is used to, in response to the abnormal log data ratio being greater than the first ratio threshold, use a first set step size to increase the initial abnormal threshold to obtain the abnormal threshold obtained in the first adjustment process; in response to the abnormal log data ratio being less than the second ratio threshold, use a second set step size to lower the initial abnormal threshold to obtain the abnormal threshold obtained in the first adjustment process.
[0154] As a possible implementation of the embodiment of the present disclosure, the adjustment module 630 is used to determine whether i is greater than the set adjustment times. If not, the abnormal log data ratio of the i-1th adjustment process is calculated according to the abnormal threshold value obtained in the i-1th adjustment process, and it is determined whether the abnormal log data ratio of the i-1th adjustment process is greater than or equal to the second ratio threshold value and less than or equal to the first ratio threshold value; if it is greater than the first ratio threshold value, the abnormal threshold value obtained in the i-1th adjustment process is increased by using the first set step size to obtain the abnormal threshold value obtained in the i-th adjustment process; if it is less than the second ratio threshold value, the abnormal threshold value obtained in the i-1th adjustment process is lowered by using the second set step size to obtain the abnormal threshold value obtained in the i-1th adjustment process; if it is greater than or equal to the second ratio threshold value and less than or equal to the first ratio threshold value, or if i is greater than the set adjustment times, the adjustment process is terminated, and the abnormal threshold value obtained in the i-1th adjustment process is used as the target abnormal threshold value.
[0155] As a possible implementation method of the embodiment of the present disclosure, the second determination module 640 is used to compare the target score of any log data with the target abnormality threshold to obtain a comparison result; in response to the comparison result indicating that the target score of any log data is greater than the target abnormality threshold, determine that any log data is abnormal; in response to the comparison result indicating that the target score of any log data is less than or equal to the target abnormality threshold, determine that any log data is not abnormal.
[0156] As a possible implementation method of an embodiment of the present disclosure, the first determination module 620 is used to determine, for any log data among multiple log data, a TF-IDF score of any log data according to the TF-IDF weight of any word in any log data; and determine a target score of any log data according to the TF-IDF score of any log data.
[0157] As a possible implementation method of an embodiment of the present disclosure, the first determination module 620 is used to determine the TF-IDF average score and the TF-IDF standard deviation of multiple log data according to the TF-IDF scores of multiple log data; for any log data, determine the difference between the TF-IDF score of any log data and the TF-IDF standard deviation; and determine the target score of any log data according to the ratio of the difference to the TF-IDF average score.
[0158] As a possible implementation manner of the embodiment of the present disclosure, the first determination module 620 is used to obtain multiple word segments in the any log data; and determine the TF-IDF score of the any log data according to the sum of the TF-IDF weights of the multiple word segments.
[0159] As a possible implementation manner of the embodiment of the present disclosure, the total number of segmented words in the plurality of log data is A, the total number of the plurality of log data is B, and the abnormal log detection device 600 further includes: a third determination module.
[0160] Among them, the third determination module is used to determine the number of occurrences of any participle in any log data in each log data; generate an A×B word frequency matrix according to the number of occurrences of A participles in B log data; wherein the element in the mth row and nth column of the word frequency matrix is used to indicate the number of occurrences of the mth participle in the nth log data; determine the TF and IDF of any participle according to the word frequency matrix; determine the TF-IDF weight of any participle according to the TF and IDF of any participle.
[0161] The abnormal log detection device of the embodiment of the present disclosure automatically determines the target score of any log data according to the TF-IDF weight of any word in the any log data, wherein the target is used to indicate the abnormal degree of any log data; further, based on the abnormal degrees of multiple log data, the set initial abnormal threshold is adjusted multiple times to obtain the target abnormal threshold, and the target score of any log data is compared with the target abnormal threshold obtained through the multiple adjustment processes to determine whether any log data is abnormal, thereby realizing dynamic adjustment of the threshold of log data abnormality detection based on the abnormal degrees of multiple log data, avoiding missed detection or false detection of abnormal logs due to excessively high or low target abnormal thresholds, and realizing detection of abnormal logs without manual analysis of log files, thereby improving the detection efficiency of abnormal logs.
[0162] In an exemplary embodiment, an electronic device is also provided.
[0163] Among them, electronic equipment includes:
[0164] processor;
[0165] a memory for storing processor-executable instructions;
[0166] The processor is configured to execute instructions to implement the abnormal log detection method proposed in any of the aforementioned embodiments.
[0167] As an example, Figure 7 is a schematic diagram of the structure of an electronic device 700 shown in an exemplary embodiment of the present disclosure, such as Figure 7 As shown, the electronic device 700 may further include:
[0168] The memory 710 and the processor 720, a bus 730 connecting different components (including the memory 710 and the processor 720), the memory 710 stores a computer program, and when the processor 720 executes the program, the method for detecting abnormal logs described in the embodiment of the present disclosure is implemented.
[0169] Bus 730 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor or a local bus using any of a variety of bus architectures. For example, these architectures include but are not limited to Industry Standard Architecture (ISA) bus, Micro Channel Architecture (MAC) bus, Enhanced ISA bus, Video Electronics Standards Association (VESA) local bus and Peripheral Component Interconnect (PCI) bus.
[0170] The electronic device 700 typically includes a variety of electronic device readable media. These media can be any available media that can be accessed by the electronic device 700, including volatile and non-volatile media, removable and non-removable media.
[0171] The memory 710 may also include computer system readable media in the form of volatile memory, such as random access memory (RAM) 740 and / or cache memory 750. The server 700 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 760 may be used to read and write non-removable, non-volatile magnetic media ( Figure 7 not shown, usually called a "hard drive"). Although Figure 7Not shown, a disk drive for reading and writing to a removable non-volatile disk (e.g., a "floppy disk"), and an optical disk drive for reading and writing to a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical media) may be provided. In these cases, each drive may be connected to the bus 730 via one or more data medium interfaces. The memory 710 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present disclosure.
[0172] A program / utility 780 having a set (at least one) of program modules 770 may be stored, for example, in the memory 710, such program modules 770 including but not limited to an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. The program modules 770 generally perform the functions and / or methods of the embodiments described in the present disclosure.
[0173] The electronic device 700 may also communicate with one or more external devices 790 (e.g., keyboard, pointing device, display 791, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 700, and / or communicate with any device that enables the electronic device 700 to communicate with one or more other computing devices (e.g., network card, modem, etc.). Such communication may be performed through an input / output (I / O) interface 792. Furthermore, the electronic device 700 may also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN) and / or public network, such as the Internet) through a network adapter 793. As shown, the network adapter 793 communicates with other modules of the electronic device 700 through a bus 730. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 700, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0174] The processor 720 executes various functional applications and data processing by running the programs stored in the memory 710 .
[0175] It should be noted that the implementation process and technical principles of the electronic device of this embodiment refer to the aforementioned explanation of the abnormal log detection method of the embodiment of the present disclosure, and will not be repeated here.
[0176] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory including instructions, and the above instructions can be executed by a processor of an electronic device to complete the detection method of abnormal logs proposed in any of the above embodiments. Optionally, the computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0177] In an exemplary embodiment, a computer program product is further provided, including a computer program / instruction, wherein when the computer program / instruction is executed by a processor, the abnormal log detection method proposed in any of the above embodiments is implemented.
[0178] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure is intended to cover any variations, uses or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The description and examples are to be considered exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0179] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A method for detecting abnormal logs, characterized in that: include: Get multiple log data from the log file within the specified time period; For any one of the multiple log data, determine a target score for the any one of the log data according to a term frequency-inverse document frequency TF-IDF weight of any word segment in the any one of the log data, wherein the target score is used to indicate the abnormality degree of the any one of the log data; Based on the target scores of the plurality of log data, adjusting the set initial abnormality threshold multiple times to obtain a target abnormality threshold; The target score of any of the log data is compared with the target abnormality threshold to determine whether any of the log data is abnormal.
2. The method according to claim 1, characterized in that The step of adjusting the set initial abnormality threshold multiple times based on the target scores of the plurality of log data to obtain the target abnormality threshold includes: Comparing the target score of each of the plurality of log data with the initial abnormal threshold value respectively, so as to determine the initial abnormal log data ratio corresponding to the plurality of log data; According to the abnormal log data ratio, the initial abnormal threshold is adjusted multiple times to obtain the target abnormal threshold.
3. The method according to claim 2, characterized in that The first adjustment process in the multiple adjustment processes includes: In response to the abnormal log data ratio being greater than a first ratio threshold, the initial abnormal threshold is adjusted higher using a first set step length to obtain an abnormal threshold obtained in a first adjustment process; In response to the abnormal log data ratio being less than a second ratio threshold, the initial abnormal threshold is adjusted down using a second set step length to obtain the abnormal threshold obtained in the first adjustment process.
4. The method according to claim 3, characterized in that The i-th (i is a positive integer greater than 1) adjustment process of the multiple adjustment processes includes: Determine whether i is greater than the set number of adjustment times. If not, calculate the abnormal log data ratio of the i-1th adjustment process according to the abnormal threshold obtained in the i-1th adjustment process, and determine whether the abnormal log data ratio of the i-1th adjustment process is greater than or equal to the second ratio threshold and less than or equal to the first ratio threshold; If it is greater than the first ratio threshold, the first set step size is used to increase the abnormal threshold obtained in the (i-1)th adjustment process to obtain the abnormal threshold obtained in the (i)th adjustment process; If it is less than the second ratio threshold, the second set step size is used to lower the abnormal threshold obtained in the (i-1)th adjustment process to obtain the abnormal threshold obtained in the (i)th adjustment process; If it is greater than or equal to the second ratio threshold and less than or equal to the first ratio threshold, or if i is greater than the set number of adjustments, the adjustment process is terminated, and the abnormal threshold obtained in the i-1th adjustment process is used as the target abnormal threshold.
5. The method according to claim 1, characterized in that The comparing the target score of any piece of log data with the target abnormality threshold to determine whether any piece of log data is abnormal includes: Compare the target score of any log data with the target abnormality threshold to obtain a comparison result; In response to the comparison result indicating that the target score of any one of the log data is greater than the target abnormality threshold, determining that any one of the log data is abnormal; In response to the comparison result indicating that the target score of any one of the log data is less than or equal to the target abnormality threshold, it is determined that any one of the log data is not abnormal.
6. The method according to claim 1, characterized in that The step of determining, for any one of the plurality of log data, a target score of the any one of the log data according to the TF-IDF weight of any word in the any one of the log data, includes: For any one of the multiple log data, determine a TF-IDF score of the any one of the log data according to the TF-IDF weight of any word in the any one of the log data; A target score for any piece of log data is determined according to the TF-IDF score of any piece of log data.
7. The method according to claim 6, characterized in that The determining, according to the TF-IDF score of any one of the log data, a target score of the any one of the log data includes: Determine, according to the TF-IDF scores of the plurality of log data, an average TF-IDF score and a TF-IDF standard deviation of the plurality of log data; For any of the log data, determine a difference between the TF-IDF score of the any of the log data and the TF-IDF standard deviation; A target score for any one of the log data is determined according to a ratio of the difference to the TF-IDF average score.
8. The method according to claim 6, characterized in that The determining, for any one of the plurality of log data, a TF-IDF score of the any one of the log data according to the TF-IDF weight of any word in the any one of the log data, includes: Obtain multiple word segments in any one of the log data; The TF-IDF score of any one of the log data is determined according to the sum of the TF-IDF weights of the multiple word segments.
9. The method according to any one of claims 1 to 8, characterized in that The total number of segmented words in the plurality of log data is A, the total number of log data is B, Before determining the target score of any piece of log data according to the TF-IDF weight of any word segment in any piece of log data, the method further includes: For any participle in any piece of log data, determine the number of occurrences of the participle in each piece of log data; Generate an A×B word frequency matrix according to the number of occurrences of A word segments in B log data; wherein the element in the mth row and nth column of the word frequency matrix is used to indicate the number of occurrences of the mth word segment in the nth log data; According to the word frequency matrix, determine the word frequency TF and the inverse document frequency IDF of any word segmentation; The TF-IDF weight of any word segment is determined according to the word frequency TF and the inverse document frequency IDF of any word segment.
10. A device for detecting abnormal logs, characterized in that: include: An acquisition module is used to acquire multiple log data from a log file within a specified period of time; A first determination module is used to determine, for any one of the multiple log data, a target score of the any one of the log data according to a term frequency-inverse document frequency TF-IDF weight of any word segment in the any one of the log data, wherein the target score is used to indicate the abnormality degree of the any one of the log data; An adjustment module, configured to adjust the set initial abnormality threshold multiple times based on the target scores of the plurality of log data to obtain a target abnormality threshold; The second determination module is used to compare the target score of any log data with the target abnormality threshold to determine whether any log data is abnormal.
Citation Information
Cited By
Health evaluation method, electronic equipment, storage medium and product
CN120104456A
