edit-icon download-icon

Set logging

Last Updated: Nov 06, 2017

OSS allows you to configure access logging for a bucket. After the configuration, accesses to the bucket are 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 the following:

  • 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, see Set access logging.

Enable bucket logging

The following code uses Bucket#logging= to enable logging:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. bucket.logging = BucketLogging.new(
  7. enable: true, target_bucket: 'logging_bucket', target_prefix: 'my-log')

View bucket logging settings

The following code uses Bucket#logging to display logging configuration:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. log = bucket.logging
  7. puts log.to_s

Disable bucket logging

The following code uses Bucket#logging= to disable logging:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. bucket.logging = BucketLogging.new(enable: false)
Thank you! We've received your feedback.