You need to enable JavaScript to run this app.
导航
管理实时日志(Go SDK)
最近更新时间:2024.02.04 18:31:05首次发布时间:2023.03.15 14:39:19

TOS 支持日志分析功能,支持通过日志服务检索分析您访问 TOS 过程中产生的访问日志。通过 TOS Go SDK 您可以设置日志分析功能配置。

设置实时日志配置规则

注意

  • 使用日志分析功能需要您已开通日志服务功能,并已授权 TOS 访问火山引擎日志服务 TLS。
  • 开启日志分析功能后,日志服务会自动创建日志项目及主题存放 TOS 的相关日志。

示例代码

以下代码用于设置桶的实时日志配置规则。

package main

import (
   "context"
   "fmt"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对于的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   input := &tos.PutBucketRealTimeLogInput{
      Bucket: bucketName,
      Configuration: tos.RealTimeLogConfiguration{
         // TLS Role 的配置
         Role: "TOSLogTLSRole",
         // TLS 的配置
         Configuration: tos.AccessLogConfiguration{
            UseServiceTopic: true,
            TLSProjectID:    "TLSProjectID",
            TLSTopicID:      "TLSTopicID",
         },
      },
   }
   output, err := client.PutBucketRealTimeLog(context.Background(), input)
   checkErr(err)
   fmt.Println("PutBucketRealTimeLog Request ID:", output.RequestID)
}

获取实时日志配置规则

注意

要获取桶的实时日志配置规则,默认您必须为桶所有者。

示例代码

以下代码用于获取桶的实时日志配置规则。

package main

import (
   "context"
   "fmt"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对于的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)

   output, err := client.GetBucketRealTimeLog(context.Background(), &tos.GetBucketRealTimeLogInput{Bucket: bucketName})
   checkErr(err)
   fmt.Println("GetBucketRealTimeLog Request ID:", output.RequestID)
   // 查看 TLS 日志配置
   fmt.Println("GetBucketRealTimeLog Role", output.Configuration.Role)
   fmt.Println("GetBucketRealTimeLog TLSTopicID", output.Configuration.Configuration.TLSTopicID)
   fmt.Println("GetBucketRealTimeLog TLSProjectID", output.Configuration.Configuration.TLSProjectID)
   fmt.Println("GetBucketRealTimeLog UseServiceTopic", output.Configuration.Configuration.UseServiceTopic)

}

删除实时日志配置规则

注意

要删除桶的实时日志配置规则,默认您必须为桶所有者。

示例代码

以下代码用于删除桶的实时日志配置规则。

package main

import (
   "context"
   "fmt"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对于的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)

   output, err := client.DeleteBucketRealTimeLog(context.Background(), &tos.DeleteBucketRealTimeLogInput{Bucket: bucketName})
   checkErr(err)
   fmt.Println("DeleteBucketRealTimeLog Request ID:", output.RequestID)

}

相关文档

关于实时日志配置的更多信息,请参见日志分析