Set logging

Last Updated: Oct 24, 2017

You can configure bucket access logging to store the bucket access logs in a specified bucket for future analysis. The logs are stored as objects in the specified bucket. A text object is generated each hour. The object name format is as follows:

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

The TargetPrefix needs to be specified by the user during configuration.

The logging configuration consists of the following parts:

  • TargetBucket: The name of the target bucket. The log objects generated are stored in this bucket.

  • TargetPrefix: The prefix of the log object name. It can be null.

For more information about the log object name format and log format, see Bucket access logging.

Enable logging

The following code enables the logging feature, stores logs in the current bucket, and limits the log objects that have logging/ as prefix:

  1. # -*- coding: utf-8 -*-
  2. import oss2
  3. from oss2.models import BucketLogging
  4. auth = oss2.Auth ('Your AccessKeyID', 'Your AccessKeySecret')
  5. bucket = oss2.Bucket (auth, 'Your endpoint', 'your bucket name')
  6. bucket.put_bucket_logging(BucketLogging(bucket.bucket_name, 'logging/'))

View logging settings

  1. logging = bucket.get_bucket_logging()
  2. print('TargetBucket={0}, TargetPrefix={1}'.format(logging.target_bucket, logging.target_prefix))

Disable logging

  1. bucket.delete_bucket_logging()
Thank you! We've received your feedback.