Log Structuring
Log data is classified into structured data and unstructured data. Structured data can be described using digits or unified data models and has a fixed length and format. Unstructured data has an irregular or incomplete structure and does no have a predefined data model. It is inconvenient to use two-dimensional logic tables of databases to display such data.
Logs are structured by log stream. Based on extraction rules, logs in a fixed format or with a high similarity are extracted from log streams and called structured logs. Irrelevant logs are filtered out so that structured logs can be queried and analyzed based on the SQL syntax.
Currently, log structuring is available only in CN South-Guangzhou and AP-Singapore.
Note
- Logs are structured by log stream. Before structuring logs, create a log stream first.
- Most logs in log streams must follow certain rules. Otherwise, logs cannot be structured.
Structuring of Original Logs
You can extract original logs from log streams by adding extraction rules and combine the extracted logs for SQL query and analysis.
The following shows how to structure original logs in a log stream.
- Log in to the LTS console and choose Log Management in the navigation pane on the left.
- Logs are structured by log stream. Select the target log group and log stream on the Log Management page.
- On the log stream details page, click Structured Configuration in the navigation pane on the left. On the page that is displayed, select a log extraction method, as shown in the following figure.
- Regular expression: Use regular expression to extract fields.
- Special characters (unsupported currently): Use delimiters such as commas (,), spaces, or other special characters to extract fields.
Figure 1 Log structuring
- On the Extract Field page, select an example log. Specifically, query and analyze original logs in the log stream by time and keyword to preliminarily find out the log contents to be structured, and then select an example log.
As shown in the following figure, the fields to be structured include IP address, date, method, URL, version, return code, and request size. Then, select the first log in the list as an example log and click Next.
Figure 2 Extracting fields
- Select fields.
In the example log, select the log content to be structured. In the dialog box that is displayed, set a name for the selected content and click Add. As shown in the following figure:
Figure 3 Selecting fields
LTS enables you to divide the log content into multiple words for search by configuring delimiters. Default delimiters include , '";=()[]{}@&<>/:\n\t\r. During log search or log structuring, you can select a word between two adjacent words.
- Click Next to structure logs.
Structured log data can be considered as two-dimensional tables of databases. You can use SQL statements to query and analyze the extracted fields.
SQL Query and Analysis
After log structuring, wait about 1-2 minutes before implementing SQL query and analysis.
The following uses structured logs in a log stream as an example to describe how to locate faults in the server through SQL query and analysis.
- In the navigation pane on the left, choose Log Management.
- SQL query is performed by log stream. Select the target log group and log stream on the Log Management page.
- On the log stream details page, click the Structured logs tab page to implement SQL query and analysis.
SQL query and analysis: Set a time range and filter criterion. Enter an SQL statement in the search box. For details about SQL statements supported by LTS, see SQL Query Syntax.
For example, to query the number of requests whose HTTP return code is greater than or equal to 500 in the last hour, perform the following operations:
Select Last 1 hour in the upper right corner and enter an SQL statement, such as select count(*) where code >= 500 in the search box.
Figure 4 Structured query
SQL Query Syntax
LTS allows you to query and analyze structured logs using SQL statements, including aggregate and mathematical functions. For example, the MIN function is used to calculate the minimum value in the num column.
- SQL Statements Table 1 lists the SQL statements supported by LTS.
Table 1 SQL statement description Statement
Description
Example
GROUP BY
Group results of one or more columns (generally used with aggregate functions).
select * group by (year),(month)
LIKE
Search for the specified pattern in a column in the WHERE clause.
select * where name like 'zhang%'
WHERE
Specify the selection condition.
select * where num > 11 or num <= 10
- Aggregate Functions
An aggregate function is used to collect statistics on a specified column of structured logs. The aggregate function returns a single value, and is often used with the SELECT statement and the GROUP BY statement. Table 2 lists the aggregate functions supported by LTS.
Table 2 Aggregate function description Function
Description
Example
count(*)
Calculate the number of tuples.
select count(*)
count (<column name>)
Calculate the number of values in a column.
select count(num)
min (<column name>)
Calculate the minimum value in a column.
select min(num)
max (<column name>)
Calculate the maximum value in a column.
select max(num)
avg (<column name>)
Calculate the average value in a column.
select avg(num)
sum (<column name>)
Calculate the sum of values in a column.
select sum(num)
Last Article: Log Management
Next Article: Log Clustering
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.