Set logging

Last Updated: Nov 02, 2017

OSS allows you to configure access logging for a bucket. After the configuration, accesses to the bucket gets logged. The logs are then stored in a specified bucket on OSS in the format of:

  1. <TargetPrefix><SourceBucket>-YYYY-mm-DD-HH-MM-SS-UniqueString

You must specify the TargetPrefix. A logging rule consists of the following three parts:

  • enable: Whether to enable access logging
  • target_bucket: The bucket where logs are stored
  • target_prefix: The prefix of the log object

For more information about access logging, see Set access logging.

Note: For complete code, see GitHub.

Enable bucket logging

You can enable bucket access logging through OssClient::putBucketLogging:

  1. <?php
  2. /**
  3. * Configure logging configuration for a bucket
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function putBucketLogging($ossClient, $bucket)
  10. {
  11. $option = array();
  12. //Save access logs to the bucket
  13. $targetBucket = $bucket;
  14. $targetPrefix = "access.log";
  15. try {
  16. $ossClient->putBucketLogging($bucket, $targetBucket, $targetPrefix, $option);
  17. } catch (OssException $e) {
  18. printf(__FUNCTION__ . ": FAILED\n");
  19. printf($e->getMessage() . "\n");
  20. return;
  21. }
  22. print(__FUNCTION__ . ": OK" . "\n");
  23. }

View bucket logging settings

You can view bucket access logging through OssClient::getBucketLogging:

  1. <?php
  2. /**
  3. * Obtain the logging configuration of a bucket
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function getBucketLogging($ossClient, $bucket)
  10. {
  11. $loggingConfig = null;
  12. $options = array();
  13. try {
  14. $loggingConfig = $ossClient->getBucketLogging($bucket, $options);
  15. } catch (OssException $e) {
  16. printf(__FUNCTION__ . ": FAILED\n");
  17. printf($e->getMessage() . "\n");
  18. return;
  19. }
  20. print(__FUNCTION__ . ": OK" . "\n");
  21. print($loggingConfig->serializeToXml() . "\n");
  22. }

Disable bucket logging

You can delete the bucket logging configuration through OssClient::deleteBucketLogging:

  1. <?php
  2. /**
  3. * Delete the logging configuration of a bucket
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function deleteBucketLogging($ossClient, $bucket)
  10. {
  11. try {
  12. $ossClient->deleteBucketLogging($bucket);
  13. } catch (OssException $e) {
  14. printf(__FUNCTION__ . ": FAILED\n");
  15. printf($e->getMessage() . "\n");
  16. return;
  17. }
  18. print(__FUNCTION__ . ": OK" . "\n");
  19. }
Thank you! We've received your feedback.