TOS 支持日志分析功能,支持通过日志服务,检索分析您访问 TOS 过程中产生的访问日志。通过 TOS Java SDK 您可以设置日志分析功能的相关配置。
您可以通过 TOS Java SDK 的 putBucketRealTimeLog
接口设置桶的实时日志配置。
注意
以下代码展示如何设置桶的实时日志配置规则。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.model.bucket.AccessLogConfiguration; import com.volcengine.tos.model.bucket.PutBucketRealTimeLogInput; import com.volcengine.tos.model.bucket.PutBucketRealTimeLogOutput; import com.volcengine.tos.model.bucket.RealTimeLogConfiguration; public class PutBucketRealTimeLogExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; String realTimeLogRole = "your real time log role"; String tlsProjectId = "the tls project id"; String tlsTopicId = "the tls topic id"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ RealTimeLogConfiguration configuration = new RealTimeLogConfiguration().setRole(realTimeLogRole) .setConfiguration(new AccessLogConfiguration() .setUseServiceTopic(true) .setTlsProjectID(tlsProjectId) .setTlsTopicID(tlsTopicId)); PutBucketRealTimeLogInput input = new PutBucketRealTimeLogInput().setBucket(bucketName) .setConfiguration(configuration); PutBucketRealTimeLogOutput output = tos.putBucketRealTimeLog(input); System.out.println("putBucketRealTimeLog succeed"); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("putBucketRealTimeLog failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("putBucketRealTimeLog failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("putBucketRealTimeLog failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
您可以通过 TOS Java SDK 的 getBucketRealTimeLog
接口获取桶的实时日志配置。
注意
要获取桶的实时日志配置规则,默认您必须为桶所有者。
以下代码展示如何获取桶的实时日志配置规则。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.model.bucket.GetBucketRealTimeLogInput; import com.volcengine.tos.model.bucket.GetBucketRealTimeLogOutput; public class GetBucketRealTimeLogExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ GetBucketRealTimeLogInput input = new GetBucketRealTimeLogInput().setBucket(bucketName); GetBucketRealTimeLogOutput output = tos.getBucketRealTimeLog(input); System.out.println("getBucketRealTimeLog succeed, the config is " + output.getConfiguration()); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("getBucketRealTimeLog failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("getBucketRealTimeLog failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("getBucketRealTimeLog failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
您可以通过 TOS Java SDK 的 deleteBucketRealTimeLog
接口删除桶的实时日志配置。
注意
要删除桶的实时日志配置规则,默认您必须为桶所有者。
以下代码展示如何删除桶的实时日志配置规则。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.model.bucket.DeleteBucketRealTimeLogInput; import com.volcengine.tos.model.bucket.DeleteBucketRealTimeLogOutput; public class DeleteBucketRealTimeLogExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ DeleteBucketRealTimeLogInput input = new DeleteBucketRealTimeLogInput().setBucket(bucketName); DeleteBucketRealTimeLogOutput output = tos.deleteBucketRealTimeLog(input); System.out.println("deleteBucketRealTimeLog succeed, " + output); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("deleteBucketRealTimeLog failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("deleteBucketRealTimeLog failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("deleteBucketRealTimeLog failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
关于实时日志配置的更多信息,请参见日志分析。