logging

更新时间:
复制 MD 格式

Accessing OSS generates numerous access logs. You can enable logging to store these logs in a specified CloudBox bucket. Log files are generated hourly and follow a fixed naming convention. You can then analyze the stored logs by using tools such as Simple Log Service or by building a Spark cluster.

Prerequisites

  • OSS on CloudBox is available only in the China (Hangzhou), China (Shanghai), China (Shenzhen), China (Heyuan), China (Beijing), and China (Chengdu) regions.

  • You have purchased a CloudBox.

  • You have created a VPC and a vSwitch for the CloudBox.

  • You have contacted technical support to request a SingleTunnel network type for your CloudBox's VPC.

Notes

  • For a source bucket with a region attribute, the target bucket for logging can be the same as or different from the source bucket. However, the target bucket must be in the same region and belong to the same account.

    When you configure logging for a source bucket, the log delivery operation itself generates new logs. If the source and target buckets are the same, the logging feature records and delivers these new logs, which causes a logging loop. We recommend using different source and target buckets.

  • If a source bucket does not have a region attribute, the target bucket for logging must be the same as the source bucket.

  • Log files are expected to be generated within 48 hours. A log file for a specific time period may not record all requests from that period. Some requests may appear in the log file of the previous or next time period. Therefore, the completeness and timeliness of log records for a specific time period are not guaranteed.

  • OSS generates a log file every hour until you disable logging. To reduce your storage costs, promptly delete log files that you no longer need.

    You can use a lifecycle rule to periodically delete log files. For more information, see Lifecycle rule based on last modification time.

  • To avoid disrupting the OSS-HDFS service or causing data contamination, do not set the Log Prefix to.dlsdata/ when configuring a logging rule for an OSS-HDFS-enabled bucket.

  • OSS may add fields to the end of logs as needed. Ensure your log processing tools are forward-compatible to handle new log fields. Effective September 17, 2025, the Bucket ARN field will be added to the log content.

  • We recommend against delivering logs to a bucket with ObjectWorm enabled. Log files are generated continuously. If ObjectWorm is enabled, log files within the retention period cannot be deleted, which can cause your storage costs to increase steadily.

Log file naming convention

Log files follow this naming convention:

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

Parameter

Description

TargetPrefix

The prefix for the log file name.

SourceBucket

The name of the source bucket that generates access logs.

YYYY-mm-DD-HH-MM-SS

The time partition of the log. From left to right, the fields represent the year, month, day, hour, minute, and second. Logs are stored on an hourly basis. For example, if HH is 01, the log file contains records from 01:00:00 to 01:59:59. The minute (MM) and second (SS) values are always 00.

UniqueString

A system-generated string that uniquely identifies the log file.

Procedure

OSS console

  1. Log on to the OSS console.

  2. In the left-side navigation pane, choose Data Service > OSS on CloudBox Buckets. Then, in the bucket list, click the name of the target bucket.

  3. In the left-side navigation pane, choose Logging > Logging.
  4. Enable logging, and then configure the following parameters.

    • Log Storage Bucket: From the drop-down list, select the bucket to store the log records. You can select only a bucket that is in the same region and belongs to the same Alibaba Cloud account.

    • Log Prefix: The destination directory for the log files. If you specify a prefix, the log files are saved to the specified directory in the target bucket. If you do not specify a prefix, the log files are saved to the root directory of the target bucket. For example, if you set the log prefix to log/, the log files are stored in the log/ directory.

  5. Click Save.

Alibaba Cloud SDK

You can enable logging only by using Alibaba Cloud SDK for Java. Version 3.15.0 or later is required.

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.SetBucketLoggingRequest;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Specify the data endpoint of the CloudBox bucket.
        String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the CloudBox bucket for which you want to enable logging, for example, examplebucket.
        String bucketName = "examplebucket";
        // Specify the name of the target CloudBox bucket to store log files. The targetBucketName and bucketName can be the same or different.
        String targetBucketName = "destbucket";
        // Set the directory to store log files to log/. If you specify this parameter, log files are saved to the specified directory in the target bucket. If you do not specify this parameter, log files are saved to the root directory of the target bucket.
        String targetPrefix = "log/";
        // Specify the region where the CloudBox bucket is located.
        String region = "cn-hangzhou";
        // Specify the CloudBox ID.
        String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer needed, call the shutdown method to release resources.
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
                .clientConfiguration(conf)
                .region(region)
                .cloudBoxId(cloudBoxId)
                .build();

        try {
            SetBucketLoggingRequest request = new SetBucketLoggingRequest(bucketName);
            request.setTargetBucket(targetBucketName);
            request.setTargetPrefix(targetPrefix);
            ossClient.setBucketLogging(request);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

Ossutil

For more information about how to use ossutil to configure logging, see put-bucket-logging.

REST API

If your application has high customization requirements, you can initiate REST API requests directly. This requires you to manually write code to calculate the signature. For more information, see PutBucketLogging.