A large number of logs are generated when Object Storage Service (OSS) resources are accessed. After you enable and configure logging for a bucket, OSS generates logs every hour in accordance with predefined naming conventions and then stores the logs as objects in a specified bucket.
Enable logging for a bucket
The following 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 used to store the log objects.
// The source bucket and the destination bucket must reside in the same region. The source and destination bucket can both be the same bucket or different buckets.
request.setTargetBucketName("yourTargetBucketName");
// Specify the directory in which the log objects 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();
View the logging configurations of a bucket
The following code provides an example on how to view the logging configurations of a bucket:
GetBucketLoggingRequest request = new GetBucketLoggingRequest();
request.setBucketName("yourSourceBucketName");
OSSAsyncTask task = oss.asyncGetBucketLogging(request, new OSSCompletedCallback<GetBucketLoggingRequest, GetBucketLoggingResult>() {
@Override
public void onSuccess(GetBucketLoggingRequest request, GetBucketLoggingResult result) {
OSSLog.logInfo("info: " + result.getTargetBucketName()+"*"+result.getTargetPrefix());
}
@Override
public void onFailure(GetBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
OSSLog.logError("error: "+serviceException.getRawMessage());
}
});
task.waitUntilFinished();
Disable logging for a bucket
The following 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) {
OSSLog.logInfo("code::"+result.getStatusCode());
}
@Override
public void onFailure(DeleteBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
OSSLog.logError("error: "+serviceException.getRawMessage());
}
});
task.waitUntilFinished();
References
- For the complete sample code of logging, 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 view 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.