This topic describes how to query and analyze JSON website logs by using query statements.
Prerequisites
JSON logs are collected. For more information, see Collect logs in simple mode.
Step 1: Create indexes
Log on to the Simple Log Service console.
In the Projects section, click the project that you want to manage.
In the left-side navigation pane, click Log Storage. In the Logstores list, click the Logstore that you want to manage.
In the upper-right corner of the query and analysis page, choose Create indexes.
. If no indexes are created, click Enable on the page. For more information about full-text indexes and field indexes and how to create an index, seeNoteIf you want to query all fields in logs, we recommend that you use full-text indexes. If you want to query only specific fields, we recommend that you use field indexes. This helps reduce index traffic. If you want to analyze fields, you must create field indexes. You must include a SELECT statement in your query statement for analysis.
Create field indexes. The following figures show a sample JSON log and an example on how to create indexes for fields.
The
__topic__
,__source__
, and__tag__
fields are reserved fields of Simple Log Service. For more information, see Reserved fields.The
@timestamp
,remote_addr
,remote_user
,http_referer
,http_user_agent
,status
,server_protocal
,http_x_forward_for
, andupstream_addr
fields exclude leaf nodes. You can create an index for thecontent
field.The
request
andtime
fields include leaf nodes, and the leaf nodes are not JSON arrays.You cannot create indexes for the
request
ortime
field. You cannot query or analyze the two fields.You can create indexes for the leaf nodes of the
request
andtime
fields. When you create the indexes, you must specify the complete names of the leaf nodes. Format:KEY1.KEY2.KEY3
. Examples:time.request_time
andtime.upstream_response_time
. After the indexes are created, you can query thetime.request_time
andtime.upstream_response_time
fields.
The value of the
body_bytes_sent
field is a JSON array. You cannot create indexes for the field or the leaf nodes of the field You cannot query or analyze thebody_bytes_sent
field or the leaf nodes of thebody_bytes_sent
field.
Step 2: Reindex data
The new indexes take effect only on data that is collected after the indexes are created. If you want to query historical data, you must use the reindexing feature. For more information, see Reindex logs for a Logstore.
Step 3: Query and analyze logs
On the query and analysis page of the Logstore, enter a query statement, specify a query time range, and then execute the statement. When you write an analytic statement, you must use double quotation marks (") to enclose field names and single quotation marks (') to enclose strings. You must include a SELECT statement in the analytic statement. For more information about how to query and analyze logs, see Query and analyze logs. For more information about common questions on how to query and analyze JSON logs, see FAQ about the query and analysis of JSON logs.
Query the logs of requests for which status code 200 is returned.
content.status:200
Query the logs of requests whose length is greater than 70.
content.request.request_length > 70
Query the logs of GET requests.
content.request.request_method:GET
Calculate the number of logs of requests by status code.
* | SELECT "content.status", COUNT(*) AS PV GROUP BY "content.status"
Calculate the number of requests by request duration and sort the results in ascending order of request duration.
* | SELECT "content.time.request_time", COUNT(*) AS count GROUP BY "content.time.request_time" ORDER BY "content.time.request_time"
Calculate the average request duration by request method.
* | SELECT avg("content.time.request_time") AS avg_time,"content.request.request_method" GROUP BY "content.request.request_method"