Log Management

Last updated: 2023-09-13 15:06:02

Feature Overview

This document provides an overview of APIs and SDK code samples related to logging.
API
Operation
Description
Setting log management
Enables logging for a source bucket
Querying log management
Queries the logging configuration of a source bucket

SDK API References

For parameters and method description of all APIs in the SDK, see SDK API Reference.

Setting log management

Note

This API is used to enable logging for a source bucket and store the access logs in a specified destination bucket.

Sample code

Objective-C
QCloudPutBucketLoggingRequest *request = [QCloudPutBucketLoggingRequest new];

// Status of the logging configuration. If there is no subnode information, logging is disabled
QCloudBucketLoggingStatus *status = [QCloudBucketLoggingStatus new];

// Specific logging configuration; this mainly refers to the destination bucket
QCloudLoggingEnabled *loggingEnabled = [QCloudLoggingEnabled new];

// Destination bucket for storing logs; this can be the source bucket (not recommended) or a bucket in the same region under the same account
loggingEnabled.targetBucket = @"examplebucket-1250000000";

// Specified path in the destination bucket for storing logs
loggingEnabled.targetPrefix = @"mylogs";

status.loggingEnabled = loggingEnabled;
request.bucketLoggingStatus = status;

// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
request.bucket = @"examplebucket-1250000000";

[request setFinishBlock:^(id outputObject, NSError *error) {
// outputObject contains all response HTTP headers
NSDictionary* info = (NSDictionary *) outputObject;
}];
[[QCloudCOSXMLService defaultCOSXML] PutBucketLogging:request];
Note
For the complete sample, go to GitHub.
Swift
let req = QCloudPutBucketLoggingRequest.init();

// Status of the logging configuration. If there is no subnode information, logging is disabled
let status = QCloudBucketLoggingStatus.init();

// Specific logging configuration; this mainly refers to the destination bucket
let loggingEnabled = QCloudLoggingEnabled.init();

// Destination bucket for storing logs; this can be the source bucket (not recommended) or a bucket in the same region under the same account
// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
loggingEnabled.targetBucket = "examplebucket-1250000000";

// Specified path in the destination bucket for storing logs
loggingEnabled.targetPrefix = "logs/";

status.loggingEnabled = loggingEnabled;
req.bucketLoggingStatus = status;

// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
req.bucket = "examplebucket-1250000000";
req.finishBlock = {(result,error) in
if let result = result {
// The result contains the response header information
} else {
print(error!);
}
}

QCloudCOSXMLService.defaultCOSXML().putBucketLogging(req);
Note
For the complete sample, go to GitHub.

Querying log management

Note

This API is used to query the logging configuration of a specified bucket.

Sample code

Objective-C
QCloudGetBucketLoggingRequest *getReq = [QCloudGetBucketLoggingRequest new];

// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
getReq.bucket = @"examplebucket-1250000000";

[getReq setFinishBlock:^(QCloudBucketLoggingStatus * _Nonnull result,
NSError * _Nonnull error) {
// Log configuration information
QCloudLoggingEnabled *loggingEnabled = result.loggingEnabled;
}];
[[QCloudCOSXMLService defaultCOSXML]GetBucketLogging:getReq];
Note
For the complete sample, go to GitHub.
Swift
let req = QCloudGetBucketLoggingRequest.init();

// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
req.bucket = "examplebucket-1250000000";
req.setFinish { (result, error) in
if let result = result {
// Log configuration information
let enabled = result.loggingEnabled
} else {
print(error!);
}
};
QCloudCOSXMLService.defaultCOSXML().getBucketLogging(req);
Note
For the complete sample, go to GitHub.