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
|_BDLogServiceSDK.framework // BLS SDK
| |____Headers
| |____BDLogServiceSDK.h // BLS header file description
| |____BDLogServiceConfig.h // BLS configuration
| |____BDLogServiceClient.h // BLS client
| |____BDLogServiceClient+LogStore.h // BLS logstore API
| |____BDLogServiceClient+LogStream.h // BLS logstream API
| |____BDLogServiceClient+LogRecord.h // BLS read/write log API
| |____BDLogServiceClient+FastQuery.h //BLS fast query API
| |____BDLogServiceClient+Index.h // BLS index API
| |____BDLogServiceClient+LogShipper.h // BLS logshipper API
| |____LogStore.h // BLS logstore model
| |____LogStream.h // BLS logstream model
| |____LogRecord.h // BLS logrecord model
| |____FastQuery.h // BLS fast query Model
| |____LogStoreIndex.h // BLS logstore index model
| |____LogShipper.h // BLS logshipper model
| |____BDLogServiceConstants.h // BLS constant definition
| |____BDCloudSigner.h // BLS AK, SK signatureUse 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))
//Initialize AK SK
NSString *ak = @"<access key>";
NSString *sk = @"<secret key>";
NSString *endpoint = @"<endpoint>";
BDLogServiceConfig *config = [[BDLogServiceConfig alloc] initWithEndpoint:endpoint accessKey:ak secretKey:sk];
BDLogServiceClient *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
LogRecord *logRecord = [[LogRecord alloc] initWithMessage: @"this is log."]- JSON type
LogRecord *logRecord = [[LogRecord alloc] initWithMessage: @"{\"level\":\"info\", \"status\": 200, \"cost\": 304.87}"]- JSON type, including the raw log text
LogRecord *logRecord = [[LogRecord alloc] initWithMessage: @"{\"@raw\":\"info 200 304.87ms this is log.\", \"level\":\"info\", \"status\": 200, \"cost\": 304.87}"]
//Configuration parameters, request interface
NSString *logStoreName = @"<custom name>";
NSString *logStreamName = @"<custom name>";
NSInteger time = (NSInteger)([NSDate date].timeIntervalSince1970 * 1000); //Log timestamp: It cannot exceed current timestamp, in milliseconds
NSString *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
NSArray *logRecords = @[
@{
@"message":@"<Log content: It can be text or JSON string. Refer to the log record format description above>",
@"timestamp":[NSNumber numberWithInteger:time]
},
@{
@"message": @"<Log content: It can be text or JSON string. Refer to the log record format description above>",
@"timestamp": [NSNumber numberWithInteger:time]
}
]; //Log records
__weak typeof(self) weakSelf = self;
[client pushLogRecordWithLogStoreName:logStoreName logStreamName:logStreamName type:type logRecords:logRecords completion:^(NSDictionary * _Nonnull resultDic, NSError * _Nonnull error) {
//resultDic returns the data result from the server
if(error){ //Failure
NSLog(@"%@ %@", resultDic[@"message"], error);
} else { //Success
NSLog(@"The log record is pushed successfully");
}
}];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))
//Initialize AK SK
NSString *ak = @"<access key>";
NSString *sk = @"<secret key>";
NSString *endpoint = @"<endpoint>";
BDLogServiceConfig *config = [[BDLogServiceConfig alloc] initWithEndpoint:endpoint accessKey:ak secretKey:sk];
BDLogServiceClient *client = [[BDLogServiceClient alloc] initWithConfig: config];- Call the createLogStore API to create logstores
//Configuration parameters, request interface
NSString *logStoreName = @"<custom name>";
NSString *retention = @"10"; // Lease period of logstore, unit: days
__weak typeof(self) weakSelf = self;
[client createLogStoreWithLogStoreName:logStoreName retention:[retention intValue] completion:^(NSDictionary * _Nonnull resultDic, NSError * _Nonnull error) {
//resultDic returns the data result from the server
if(error){ //Failure
NSLog(@"%@ %@", resultDic[@"message"], error);
} else { //Success
NSLog(@"LogStore created successfully");
}
}];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)
