Log Management

Last updated: 2020-09-09 11:34:31

    Overview

    This document provides an overview of APIs and SDK code samples related to logging.

    API Operation Description
    PUT Bucket logging Setting logging configuration Enables logging for a source bucket
    GET Bucket logging Querying logging configuration Queries the logging configuration of a source bucket

    SDK API Reference

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

    Setting Logging Configuration

    API description

    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, logging is disabled
    QCloudBucketLoggingStatus *status = [QCloudBucketLoggingStatus new];
    
    // Specific logging configuration mainly on 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: `BucketName-APPID`
    request.bucket = @"examplebucket-1250000000";
    
    [request setFinishBlock:^(id outputObject, NSError *error) {
       // outputObject contains all the HTTP response 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, logging is disabled
    let status = QCloudBucketLoggingStatus.init();
    
    // Specific logging configuration mainly on 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: `BucketName-APPID`
    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: `BucketName-APPID`
    req.bucket = "examplebucket-1250000000";
    req.finishBlock = {(result,error) in
        if let result = result {
            // “result” contains response headers
        } else {
            print(error!);
        }
    }
    
    QCloudCOSXMLService.defaultCOSXML().putBucketLogging(req);

    Note:

    For the complete sample, go to GitHub.

    Querying Logging Configuration

    API description

    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: `BucketName-APPID`
    getReq.bucket = @"examplebucket-1250000000";
    
    [getReq setFinishBlock:^(QCloudBucketLoggingStatus * _Nonnull result,
                             NSError * _Nonnull error) {
        // Logging configuration
        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: `BucketName-APPID`
    req.bucket = "examplebucket-1250000000";
    req.setFinish { (result, error) in
        if let result = result {
            // Logging configuration
            let enabled = result.loggingEnabled
        } else {
            print(error!);
        }
    };
    QCloudCOSXMLService.defaultCOSXML().getBucketLogging(req);

    Note:

    For the complete sample, go to GitHub.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help