All Products
Search
Document Center

Object Storage Service:Logging

Last Updated:Mar 04, 2024

Object Storage Service (OSS) generates access logs to record events related to resources stored in OSS buckets. After you enable and configure logging for a bucket, OSS generates log objects hourly based on predefined naming rules and then stores the log objects in a specified bucket.

Usage notes

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

Note

If you call an API operation to enable or disable logging, you can view the logging configurations only by refreshing the page. You cannot view the logging configurations by switching between tabs.

Enable logging for a bucket

The following sample code provides an example on how to enable logging for a bucket:

PutBucketLoggingRequest request = new PutBucketLoggingRequest();
// Specify the name of the source bucket for which you want to enable logging. 
request.setBucketName("yourSourceBucketName");
// Specify the name of the destination bucket in which you want to store the logs. 
// The source bucket and the destination bucket must be located in the same region. The source bucket and the destination bucket can be the same bucket or different buckets. 
request.setTargetBucketName("yourTargetBucketName");
// Specify the directory in which the logs are stored. 
request.setTargetPrefix("<yourTargetPrefix>");

OSSAsyncTask task = oss.asyncPutBucketLogging(request, new OSSCompletedCallback<PutBucketLoggingRequest, PutBucketLoggingResult>() {
    @Override
    public void onSuccess(PutBucketLoggingRequest request, PutBucketLoggingResult result) {
        OSSLog.logInfo("code::"+result.getStatusCode());
    }

    @Override
    public void onFailure(PutBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
         OSSLog.logError("error: "+serviceException.getRawMessage());
    }
});
task.waitUntilFinished();

Query the logging configurations of a bucket

The following sample code provides an example on how to query the logging configurations of a bucket:

Note

The query result is in the "testBucket*path" format, in which "testBucket" before "*" is the bucket name, and "path" is the storage path in which the logging configurations are stored.

GetBucketLoggingRequest request = new GetBucketLoggingRequest();
request.setBucketName("yourSourceBucketName");
OSSAsyncTask task = oss.asyncGetBucketLogging(request, new OSSCompletedCallback<GetBucketLoggingRequest, GetBucketLoggingResult>() {
    @Override
    public void onSuccess(GetBucketLoggingRequest request, GetBucketLoggingResult result) {
        Log.i("i", "info: " + result.getTargetBucketName()+"*"+result.getTargetPrefix());

    }

    @Override
    public void onFailure(GetBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
       // Handle request exceptions. 
              if (clientException != null) {
                  // Handle client-side exceptions, such as network errors. 
                  clientException.printStackTrace();
              }
              if (serviceException != null) {
                  // Handle server-side exceptions. 
                  Log.e("ErrorCode", serviceException.getErrorCode());
                  Log.e("RequestId", serviceException.getRequestId());
                  Log.e("HostId", serviceException.getHostId());
                  Log.e("RawMessage", serviceException.getRawMessage());
              }
    }
});
task.waitUntilFinished();

Disable logging for a bucket

The following sample code provides an example on how to disable logging for a bucket:

DeleteBucketLoggingRequest request = new DeleteBucketLoggingRequest();
request.setBucketName("yourSourceBucketName");
OSSAsyncTask task = oss.asyncDeleteBucketLogging(request, new OSSCompletedCallback<DeleteBucketLoggingRequest, DeleteBucketLoggingResult>() {
    @Override
    public void onSuccess(DeleteBucketLoggingRequest request, DeleteBucketLoggingResult result) {
        Log.i("i", "code:"+result.getStatusCode());

    }

    @Override
    public void onFailure(DeleteBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
        // Handle request exceptions. 
              if (clientException != null) {
                  // Handle client-side exceptions, such as network errors. 
                  clientException.printStackTrace();
              }
              if (serviceException != null) {
                  // Handle server-side exceptions. 
                  Log.e("ErrorCode", serviceException.getErrorCode());
                  Log.e("RequestId", serviceException.getRequestId());
                  Log.e("HostId", serviceException.getHostId());
                  Log.e("RawMessage", serviceException.getRawMessage());
              }
    }
});

task.waitUntilFinished();
            

References

  • For the complete sample code that is used to manage logging for a bucket, visit GitHub.

  • For more information about the API operation that you can call to enable logging for a bucket, see PutBucketLogging.

  • For more information about the API operation that you can call to query the logging configurations of a bucket, see GetBucketLogging.

  • For more information about the API operation that you can call to disable logging for a bucket, see DeleteBucketLogging.

  • For more information about how to initialize an OSSClient instance, see Initialization.