Quick start
Introduction
This document mainly introduces the installation and use of Baidu AI Cloud Log Service iOS SDK.
This document assumes that you have enabled the Baidu AI Cloud log service.
If you haven't enabled or don't understand the log service, please log on to the Log Service Product Homepage for more help.
Environmental requirements
- iOS system version: 9.0 and above
- It is required to register a Baidu AI Cloud user account and open the log service
Install the SDK Package
Installation steps
- Find the Baidu log service from the client SDK list of Baidu AI Cloud Developer Center, download the SDK compressed package, and decompress it to obtainBDLogServiceSDK.framework.
- As needed, copy the framework into the Xcode project directory, such as the lib directory.
- Right-click in Xcode, select "Add Files to "Your Project"", and import the copied framework.
SDK directory structure
1|_BDLogServiceSDK.framework // BLS SDK
2| |____Headers
3 | |____BDLogServiceSDK.h // BLS header file description
4 | |____BDLogServiceConfig.h // BLS configuration
5 | |____BDLogServiceClient.h // BLS client
6 | |____BDLogServiceClient+LogStore.h // BLS logstore API
7 | |____BDLogServiceClient+LogStream.h // BLS logstream API
8 | |____BDLogServiceClient+LogRecord.h // BLS read/write log API
9 | |____BDLogServiceClient+FastQuery.h //BLS fast query API
10 | |____BDLogServiceClient+Index.h // BLS index API
11 | |____BDLogServiceClient+LogShipper.h // BLS logshipper API
12 | |____LogStore.h // BLS logstore model
13 | |____LogStream.h // BLS logstream model
14 | |____LogRecord.h // BLS logrecord model
15 | |____FastQuery.h // BLS fast query Model
16 | |____LogStoreIndex.h // BLS logstore index model
17 | |____LogShipper.h // BLS logshipper model
18 | |____BDLogServiceConstants.h // BLS constant definition
19 | |____BDCloudSigner.h // BLS AK, SK signature
Use of SDK
Example 1: upload logs
- Initialize ak/sk, endpoint, and create a BDLogServiceClient object. (Available endpoints can be viewed in [API Reference-Service Domain Name](BLS/Development Guide/API Reference/Service domain.md))
1//Initialize AK SK
2NSString *ak = @"<access key>";
3NSString *sk = @"<secret key>";
4NSString *endpoint = @"<endpoint>";
5BDLogServiceConfig *config = [[BDLogServiceConfig alloc] initWithEndpoint:endpoint accessKey:ak secretKey:sk];
6BDLogServiceClient *client = [[BDLogServiceClient alloc] initWithConfig: config];
-
Call the pushLogRecord API to upload logs
Log records can be in TEXT or JSON format. For TEXT logs, the records are not processed; for JSON logs, only the first-level JSON fields are automatically detected (nested fields are not supported at this time).
To upload both the processed specific fields and the raw log text, use the JSON format. Include the raw log text in JSON with "@raw" as the key and the raw log text as the value. While processing "@raw," BLS will treat its content as the raw log text.
For example:
- TEXT type
ObjectiveC1 LogRecord *logRecord = [[LogRecord alloc] initWithMessage: @"this is log."]- JSON type
ObjectiveC1 LogRecord *logRecord = [[LogRecord alloc] initWithMessage: @"{\"level\":\"info\", \"status\": 200, \"cost\": 304.87}"]- JSON type, including the raw log text
ObjectiveC1 LogRecord *logRecord = [[LogRecord alloc] initWithMessage: @"{\"@raw\":\"info 200 304.87ms this is log.\", \"level\":\"info\", \"status\": 200, \"cost\": 304.87}"]
1//Configuration parameters, request interface
2NSString *logStoreName = @"<custom name>";
3NSString *logStreamName = @"<custom name>";
4NSInteger time = (NSInteger)([NSDate date].timeIntervalSince1970 * 1000); //Log timestamp: It cannot exceed current timestamp, in milliseconds
5NSString *type = @"TEXT"; //Log record format: It supports JSON/TEXT, defaulting to TEXT. Refer to the log record format description above. All log records in one upload request must be of the same format
6NSArray *logRecords = @[
7 @{
8@"message":@"<Log content: It can be text or JSON string. Refer to the log record format description above>",
9 @"timestamp":[NSNumber numberWithInteger:time]
10 },
11 @{
12@"message": @"<Log content: It can be text or JSON string. Refer to the log record format description above>",
13 @"timestamp": [NSNumber numberWithInteger:time]
14 }
15]; //Log records
16__weak typeof(self) weakSelf = self;
17[client pushLogRecordWithLogStoreName:logStoreName logStreamName:logStreamName type:type logRecords:logRecords completion:^(NSDictionary * _Nonnull resultDic, NSError * _Nonnull error) {
18//resultDic returns the data result from the server
19if(error){ //Failure
20 NSLog(@"%@ %@", resultDic[@"message"], error);
21} else { //Success
22NSLog(@"The log record is pushed successfully");
23 }
24}];
Example 2: create logstores
- Initialize ak/sk, endpoint, and create a BDLogServiceClient object. (Available endpoints can be viewed in [API Reference-Service Domain Name](BLS/Development Guide/API Reference/Service domain.md))
1//Initialize AK SK
2NSString *ak = @"<access key>";
3NSString *sk = @"<secret key>";
4NSString *endpoint = @"<endpoint>";
5BDLogServiceConfig *config = [[BDLogServiceConfig alloc] initWithEndpoint:endpoint accessKey:ak secretKey:sk];
6BDLogServiceClient *client = [[BDLogServiceClient alloc] initWithConfig: config];
- Call the createLogStore API to create logstores
1//Configuration parameters, request interface
2NSString *logStoreName = @"<custom name>";
3NSString *retention = @"10"; // Lease period of logstore, unit: days
4__weak typeof(self) weakSelf = self;
5[client createLogStoreWithLogStoreName:logStoreName retention:[retention intValue] completion:^(NSDictionary * _Nonnull resultDic, NSError * _Nonnull error) {
6//resultDic returns the data result from the server
7if(error){ //Failure
8 NSLog(@"%@ %@", resultDic[@"message"], error);
9} else { //Success
10NSLog(@"LogStore created successfully");
11 }
12}];
The calling process of other service APIs is similar to the examples. For the specific usage of API used by SDK, please refer to: [API Reference-Overview](BLS/Development Guide/API Reference/Overview.md)
