All Products
Search
Document Center

Simple Log Service:Query and analyze JSON logs

Last Updated:Apr 22, 2024

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

  1. Log on to the Simple Log Service console.

  2. In the Projects section, click the project that you want to manage.

    image

  3. In the left-side navigation pane, click Log Storage. In the Logstores list, click the Logstore that you want to manage.

    image

  4. In the upper-right corner of the query and analysis page, choose Index Attributes > Attributes. 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, see Create indexes.

    Note

    If 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.

  5. 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, and upstream_addr fields exclude leaf nodes. You can create an index for the content field.

    • The request and time fields include leaf nodes, and the leaf nodes are not JSON arrays.

      • You cannot create indexes for the request or time field. You cannot query or analyze the two fields.

      • You can create indexes for the leaf nodes of the request and time fields. When you create the indexes, you must specify the complete names of the leaf nodes. Format: KEY1.KEY2.KEY3. Examples: time.request_time and time.upstream_response_time. After the indexes are created, you can query the time.request_time and time.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 the body_bytes_sent field or the leaf nodes of the body_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"

    PV

  • 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"

    平均请求时长

References