From 54fcd232bdec5b0c8ce64cfa10a5ed462fc613d0 Mon Sep 17 00:00:00 2001 From: yuxh Date: Mon, 13 Mar 2023 19:42:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0alisls=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- funtion.go | 25 +++++++++++++++---------- log.go | 8 ++++---- log_test.go | 10 +++++++--- logs/alils/alils.go | 2 +- logs/es/es.go | 2 +- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 8e5e01d..a2e38c8 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ 1. 引入 ``` -import "golib.gaore.com/GaoreGo/grlogs" +import "golib.gaore.com/GaoreGo/haiwai-grlogs" ``` 2. 简单用法 ``` @@ -29,11 +29,11 @@ export GRLOG_APP_NAME=mkt.gaore.com; ``` 还需要额外引入es库,完成初始化动作 ```go -import _ "golib.gaore.com/GaoreGo/grlogs/logs/es" +import _ "haiwai-grlogs/logs/es" ``` ```go -import _ "golib.gaore.com/GaoreGo/grlogs/logs/alils" +import _ "haiwai-grlogs/logs/alils" ``` 5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录** @@ -51,7 +51,7 @@ package grlogs import ( "fmt" - _ "golib.gaore.com/GaoreGo/grlogs/logs/es" + _ "haiwai-grlogs/logs/es" "testing" "time" ) diff --git a/funtion.go b/funtion.go index 380b300..f007b96 100644 --- a/funtion.go +++ b/funtion.go @@ -56,23 +56,28 @@ var adatperSetMapper = map[Adapter]func(l *Logger, level Level) error{ } func GenAliConfig(level Level) *AliLSConfig { - var project string = "gaore-app-logstore" + var project = "gr-go-project" var endpoint string - if os.Getenv(envkey) == "prod" || os.Getenv(envkey) == "" || os.Getenv(envkey) == "gray" { - endpoint = project + ".cn-shenzhen-intranet.log.aliyuncs.com" - } else if os.Getenv(envkey) == "dev" { - endpoint = project + ".cn-shenzhen.log.aliyuncs.com" + intranet := os.Getenv("GRLOG_INTERNET") != "public" + if intranet && (os.Getenv(envkey) == "development" || os.Getenv(envkey) == "testing") { + intranet = false } + if intranet { + endpoint = "cn-hongkong-intranet.log.aliyuncs.com" + } else { + endpoint = "cn-hongkong.log.aliyuncs.com" + } + source := os.Getenv("GRLOG_SOURCE") c := &AliLSConfig{ Project: project, Endpoint: endpoint, - KeyID: "LTAI4GCHwcqtrFD4DHRHxR4k", - KeySecret: "Ln19xfVYy6OMlJeF9aBvFl4fhRUKBl", - LogStore: "gaore-app-logstore", - Topics: []string{os.Getenv("GRLOG_APP_NAME")}, - Source: "", + KeyID: "LTAI5tMA7iMGFfuc3xSRVCGQ", + KeySecret: "z98EpzmYmgiVLKizxP7pMF4BPbHfPV", + LogStore: os.Getenv("GRLOG_APP_NAME"), + Topics: []string{os.Getenv("GRLOG_TOPIC")}, + Source: source, Level: level, FlushWhen: 0, } diff --git a/log.go b/log.go index 0fc221c..544d636 100644 --- a/log.go +++ b/log.go @@ -2,14 +2,14 @@ package grlogs import ( "fmt" - "golib.gaore.com/GaoreGo/grlogs/logs" + "haiwai-grlogs/logs" "os" "sync" ) var loggers = sync.Map{} -var envkey = "CENTER_RUNMODE" +var envkey = "GAORE_ENVIRONMENT" var defaultModeMapping = map[string][]AdapterTupple{ "es": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterElasticSearch}}, @@ -103,7 +103,7 @@ func Close(lables ...string) { wg := &sync.WaitGroup{} for _, lable := range lables { wg.Add(1) - go func() { + go func(lable string) { if v, ok := loggers.Load(lable); ok { if tmp, ok := v.(*Logger); ok { loggers.Delete(lable) @@ -111,7 +111,7 @@ func Close(lables ...string) { } } wg.Done() - }() + }(lable) } wg.Wait() diff --git a/log_test.go b/log_test.go index b05fded..d48ef71 100644 --- a/log_test.go +++ b/log_test.go @@ -2,8 +2,9 @@ package grlogs import ( "errors" - _ "golib.gaore.com/GaoreGo/grlogs/logs/alils" - _ "golib.gaore.com/GaoreGo/grlogs/logs/es" + _ "haiwai-grlogs/logs/alils" + _ "haiwai-grlogs/logs/es" + "os" "sync" "testing" "time" @@ -62,12 +63,15 @@ func TestDropAdapter(t *testing.T) { } func TestNew(t *testing.T) { + os.Setenv("GRLOG_INTERNET", "public") + os.Setenv("GRLOG_APP_NAME", "yoda.hk") + os.Setenv("GRLOG_ALILS_DEBUG", "on") + t.Log(os.Getenv("GRLOG_INTERNET")) wg := sync.WaitGroup{} for i := 0; i < 10; i++ { wg.Add(1) go func(i int) { GetAli("ok").Debug("%d", i) - time.Sleep(time.Second * 10) wg.Done() }(i) } diff --git a/logs/alils/alils.go b/logs/alils/alils.go index a59023a..c8b54dd 100644 --- a/logs/alils/alils.go +++ b/logs/alils/alils.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "github.com/aliyun/aliyun-log-go-sdk/producer" - "golib.gaore.com/GaoreGo/grlogs/logs" + "haiwai-grlogs/logs" "os" "runtime" "time" diff --git a/logs/es/es.go b/logs/es/es.go index abcc255..9c531d3 100644 --- a/logs/es/es.go +++ b/logs/es/es.go @@ -10,7 +10,7 @@ import ( "time" "golib.gaore.com/GaoreGo/goes" - "golib.gaore.com/GaoreGo/grlogs/logs" + "haiwai-grlogs/logs" ) // NewES return a LoggerInterface