github.com/jtzjtz/kit@v1.0.2/log/sls_log_test.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/aliyun/aliyun-log-go-sdk"
     6  	"github.com/aliyun/aliyun-log-go-sdk/producer"
     7  	"github.com/jtzjtz/kit/conn"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func initProducer() {
    14  
    15  	producerConfig := producer.GetDefaultProducerConfig()
    16  	producerConfig.AccessKeyID = "********"
    17  	producerConfig.Endpoint = "cn-beijing-intranet.log.aliyuncs.com"
    18  	producerConfig.AccessKeySecret = "*******"
    19  	producerConfig.AllowLogLevel = ""
    20  	producerConfig.LogFileName = "./data.log"
    21  
    22  	conn.InitSLSProducer(producerConfig)
    23  	conn.SetDefaultAccessLog("app-point-data", "app_data_server_test", "topic", "127.0.0.1")
    24  	//conn.InitSLSProducer(producerConfig, "sq-project", "infolog", "topic", "127.0.0.1")
    25  }
    26  func TestAccessLog(t *testing.T) {
    27  
    28  	initProducer()
    29  	defer conn.Close()
    30  	content := make(map[string]string)
    31  	content["event_id"] = "adfadsadsf"
    32  	content["service_name"] = "shequ-service"
    33  	content["server_ip"] = "localIP"
    34  	content["server_hostname"], _ = os.Hostname()
    35  	content["create_time"] = time.Now().Format("2006-01-02 15:04:05")
    36  	if err := AddAccessMap(content); err != nil {
    37  		t.Error(err.Error())
    38  	}
    39  
    40  }
    41  func TestCreateClient(t *testing.T) {
    42  	AccessKeyID := "****"                                                           //阿里云访问密钥AccessKey ID。更多信息,请参见访问密钥。阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维。
    43  	AccessKeySecret := "*****"                                                      //阿里云访问密钥AccessKey Secret。
    44  	Endpoint := "cn-beijing-intranet.log.aliyuncs.com"                              //日志服务的域名。更多信息,请参见服务入口。此处以杭州为例,其它地域请根据实际情况填写。
    45  	Client := sls.CreateNormalInterface(Endpoint, AccessKeyID, AccessKeySecret, "") //创建Client。
    46  
    47  	project, err := Client.CreateProject("sq-project", "社区project") //输入Project名称和描述。
    48  	if err != nil {
    49  		fmt.Println(err)
    50  	}
    51  	fmt.Println(project)
    52  	err = Client.CreateLogStore("sq-project", "infolog", 2, 2, true, 16) //输入Project名称、Logstore名称、数据保存时长、Shard数量、开启自动分裂Shard功能和最大分裂数。如果数据保存时长配置为3650,表示永久保存。
    53  	if err != nil {
    54  		fmt.Println(err)
    55  	}
    56  
    57  }