百度智能云

All Product Document

          Log Service

          LogRecord Operations

          Push log PushLogRecord

          Support bulk pushing of log records to the BLS platform. The log record format can be either TEXT or JSON. For TEXT log records, logs remain unprocessed; for JSON log records, JSON fields are automatically identified (only first-level fields are currently supported, nested fields are not yet supported).

          To upload both specific fields extracted from processing and the raw log text, you can use JSON format, including the raw log text as part of JSON (@raw as the key and the raw log text as the value). When processing @raw, BLS will interpret its content as the raw log text.

          Use the following code to bulk push JSON log records to the specified log stream within the specified LogStore.

          import com.baidubce.auth.DefaultBceCredentials;
          import com.baidubce.services.bls.BlsClient;
          import com.baidubce.services.bls.BlsClientConfiguration;
          import com.baidubce.services.bls.model.logrecord.LogRecord;
          import com.baidubce.services.bls.model.logrecord.LogType;
          import com.baidubce.services.bls.model.logrecord.PushLogRecordRequest;
          import java.util.ArrayList;
          import java.util.List;
          public class ExamplePushLogRecord {
              public static void main(String[] args) {
                  String ak = "Your Ak";
                  String sk = "Your Sk";
                  String endpoint = "bls-log.bj.baidubce.com";
                  BlsClientConfiguration config = new BlsClientConfiguration();
                  config.setEndpoint(endpoint);
                  config.setCredentials(new DefaultBceCredentials(ak, sk));
           // Create BLS client
                  BlsClient client = new BlsClient(config);
                  PushLogRecordRequest request = new PushLogRecordRequest();
                  List<LogRecord> logRecords = new ArrayList<>();
                  logRecords.add(new LogRecord(1742281309000L, "{\"key1\":\"value\", \"key2\": \"value2\" }"));
           // Set target project group name
                  request.setProject("default");
           // Set target logstore name
                  request.setLogStoreName("logstorename");
           // Set log format to JSON
                  request.setType(LogType.JSON);
           // Set log
                  request.setLogRecords(logRecords);
           // Execute log pushing
                  client.PushLogRecord(request);
              }
          }

          Obtain logrecord PullLogRecord

          With following codes, view the logrecords in the specified logstream, and obtain the recent logrecord contents or the time range of use for filtering.

          package com.baidubce.examples.bls;
          import com.baidubce.auth.DefaultBceCredentials;
          import com.baidubce.services.bls.BlsClient;
          import com.baidubce.services.bls.BlsClientConfiguration;
          import com.baidubce.services.bls.model.logrecord.*;
          public class ExamplePullLogRecord {
              public static void main(String[] args) {
                  String ak = "Your Ak";
                  String sk = "Your Sk";
                  String endpoint = "bls-log.bj.baidubce.com";
                  BlsClientConfiguration config = new BlsClientConfiguration();
                  config.setEndpoint(endpoint);
                  config.setCredentials(new DefaultBceCredentials(ak, sk));
           // Create BLS client
                  BlsClient client = new BlsClient(config);
                  PullLogRecordRequest request = new PullLogRecordRequest();
           // Set target project group name
                  request.setProject("default");
           // Set target logstore name
                  request.setLogStoreName("logstorename");
           // Set start time of log query
                  request.setStartDateTime("2025-03-17T02:04:05Z");
           // Set end time of log query
                  request.setEndDateTime("2025-03-17T15:04:05Z");
           // Set maximum number of returned items
                  request.setLimit(10);
           // Execute log query
                  PullLogRecordResponse resp = client.pullLogRecord(request);
                  System.out.println(resp);
              }
          }

          Search analysis log QueryLogRecord

          Users can search or analyze data in a specified logstore by submitting a query. Only one logstore can be queried at a time.

          Query statements support three formats, such as:

          • match search statement: it is required to enable the full-text index or field-level index and search the log contents according to the conditions, for example:match method:GET and status >= 400
          • SQL statement: execute SQL statements, for example: select * limit 10
          • match search statement| SQL statement: it is required to enable the full-text index or field-level index and execute SQL statements on the result set that meets the search conditions, with the search statement separated from the SQL statement by a vertical line, for example: match method:GET and status >= 400 | select host, count(*) group by host

          Query related restrictions are as follows:

          • The maximum number of query concurrency supported by each account is 15
          • Limit the size of the returned result set to a maximum of 1 MB or 1,000 records.

          For search syntax, please refer to Search Syntax

          SQL statements may not contain from clauses, and syntax details may refer to SQL Syntax

          Use the following code to query log records that satisfy specified conditions within the desired LogStore.

          import com.baidubce.auth.DefaultBceCredentials;
          import com.baidubce.services.bls.BlsClient;
          import com.baidubce.services.bls.BlsClientConfiguration;
          import com.baidubce.services.bls.model.logrecord.QueryLogRecordRequest;
          import com.baidubce.services.bls.model.logrecord.QueryLogRecordResponse;
          public class ExampleQueryLogRecord {
              public static void main(String[] args) {
                  String ak = "Your Ak";
                  String sk = "Your Sk";
                  String endpoint = "bls-log.bj.baidubce.com";
                  BlsClientConfiguration config = new BlsClientConfiguration();
                  config.setEndpoint(endpoint);
                  config.setCredentials(new DefaultBceCredentials(ak, sk));
           // Create BLS client
                  BlsClient client = new BlsClient(config);
                  QueryLogRecordRequest request = new QueryLogRecordRequest();
           // Set target project group name
                  request.setProject("default");
           // Set target logstore name
                  request.setLogStoreName("logstorename");
           // Set start time of log query
                  request.setStartDateTime("2025-03-17T02:04:05Z");
           // Set end time of log query
                  request.setEndDateTime("2025-03-17T15:04:05Z");
           // Set search statement
                  request.setQuery("match level:info");
           // Execute log search
                  QueryLogRecordResponse response = client.queryLogRecord(request);
                  System.out.println(response);
              }
          }

          Histogram API QueryLogHistogram

          Users can analyze data using statistical histograms by submitting query search statements. Note that only one logstore can be queried at a time.

          Query statements support three formats, such as:

          • match search statement: return the log histogram statistics that meets search conditions
          • SQL statement: return the histogram statistics of all logs
          • match search statement | SQL statement: according to search conditions, return the following restrictions for log histogram statistics query that meet search conditions:

            • Index must be enabled on the logstore; otherwise, the histogram query will return an error
            • The maximum query time is 60 seconds; an error is returned if no statistic result is obtained over 60 seconds.

          For search syntax, please refer to Search Syntax

          Use the following code to query logs that meet the specified conditions within the intended LogStore.

          import com.baidubce.auth.DefaultBceCredentials;
          import com.baidubce.services.bls.BlsClient;
          import com.baidubce.services.bls.BlsClientConfiguration;
          import com.baidubce.services.bls.model.logrecord.QueryLogHistogramRequest;
          import com.baidubce.services.bls.model.logrecord.QueryLogHistogramResponse;
          public class ExampleQueryLogHistogram {
              public static void main(String[] args) {
                  String ak = "Your Ak";
                  String sk = "Your Sk";
                  String endpoint = "bls-log.bj.baidubce.com";
                  BlsClientConfiguration config = new BlsClientConfiguration();
                  config.setEndpoint(endpoint);
                  config.setCredentials(new DefaultBceCredentials(ak, sk));
           // Create BLS client
                  BlsClient client = new BlsClient(config);
                  QueryLogHistogramRequest request = new QueryLogHistogramRequest();
           // Set target project group name
                  request.setProject("default");
           // Set target logstore name
                  request.setLogStoreName("logstorename");
           // Set start time of log query
                  request.setStartDateTime("2025-03-17T02:04:05Z");
           // Set end time of log query
                  request.setEndDateTime("2025-03-17T15:04:05Z");
           // Set search statement
                  request.setQuery("match level:info");
           // Execute log histogram analysis
                  QueryLogHistogramResponse response = client.queryLogHistogram(request);
                  System.out.println(response);
              }
          }
          Previous
          Install the SDK Package
          Next
          Android SDK