更新alisls 配置

This commit is contained in:
余 欣怀 2023-03-13 19:42:29 +08:00
parent 8b94a0d256
commit 54fcd232bd
6 changed files with 32 additions and 23 deletions

View File

@ -5,7 +5,7 @@
1. 引入 1. 引入
``` ```
import "golib.gaore.com/GaoreGo/grlogs" import "golib.gaore.com/GaoreGo/haiwai-grlogs"
``` ```
2. 简单用法 2. 简单用法
``` ```
@ -29,11 +29,11 @@ export GRLOG_APP_NAME=mkt.gaore.com;
``` ```
还需要额外引入es库完成初始化动作 还需要额外引入es库完成初始化动作
```go ```go
import _ "golib.gaore.com/GaoreGo/grlogs/logs/es" import _ "haiwai-grlogs/logs/es"
``` ```
```go ```go
import _ "golib.gaore.com/GaoreGo/grlogs/logs/alils" import _ "haiwai-grlogs/logs/alils"
``` ```
5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录** 5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录**
@ -51,7 +51,7 @@ package grlogs
import ( import (
"fmt" "fmt"
_ "golib.gaore.com/GaoreGo/grlogs/logs/es" _ "haiwai-grlogs/logs/es"
"testing" "testing"
"time" "time"
) )

View File

@ -56,23 +56,28 @@ var adatperSetMapper = map[Adapter]func(l *Logger, level Level) error{
} }
func GenAliConfig(level Level) *AliLSConfig { func GenAliConfig(level Level) *AliLSConfig {
var project string = "gaore-app-logstore" var project = "gr-go-project"
var endpoint string var endpoint string
if os.Getenv(envkey) == "prod" || os.Getenv(envkey) == "" || os.Getenv(envkey) == "gray" { intranet := os.Getenv("GRLOG_INTERNET") != "public"
endpoint = project + ".cn-shenzhen-intranet.log.aliyuncs.com" if intranet && (os.Getenv(envkey) == "development" || os.Getenv(envkey) == "testing") {
} else if os.Getenv(envkey) == "dev" { intranet = false
endpoint = project + ".cn-shenzhen.log.aliyuncs.com"
} }
if intranet {
endpoint = "cn-hongkong-intranet.log.aliyuncs.com"
} else {
endpoint = "cn-hongkong.log.aliyuncs.com"
}
source := os.Getenv("GRLOG_SOURCE")
c := &AliLSConfig{ c := &AliLSConfig{
Project: project, Project: project,
Endpoint: endpoint, Endpoint: endpoint,
KeyID: "LTAI4GCHwcqtrFD4DHRHxR4k", KeyID: "LTAI5tMA7iMGFfuc3xSRVCGQ",
KeySecret: "Ln19xfVYy6OMlJeF9aBvFl4fhRUKBl", KeySecret: "z98EpzmYmgiVLKizxP7pMF4BPbHfPV",
LogStore: "gaore-app-logstore", LogStore: os.Getenv("GRLOG_APP_NAME"),
Topics: []string{os.Getenv("GRLOG_APP_NAME")}, Topics: []string{os.Getenv("GRLOG_TOPIC")},
Source: "", Source: source,
Level: level, Level: level,
FlushWhen: 0, FlushWhen: 0,
} }

8
log.go
View File

@ -2,14 +2,14 @@ package grlogs
import ( import (
"fmt" "fmt"
"golib.gaore.com/GaoreGo/grlogs/logs" "haiwai-grlogs/logs"
"os" "os"
"sync" "sync"
) )
var loggers = sync.Map{} var loggers = sync.Map{}
var envkey = "CENTER_RUNMODE" var envkey = "GAORE_ENVIRONMENT"
var defaultModeMapping = map[string][]AdapterTupple{ var defaultModeMapping = map[string][]AdapterTupple{
"es": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterElasticSearch}}, "es": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterElasticSearch}},
@ -103,7 +103,7 @@ func Close(lables ...string) {
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
for _, lable := range lables { for _, lable := range lables {
wg.Add(1) wg.Add(1)
go func() { go func(lable string) {
if v, ok := loggers.Load(lable); ok { if v, ok := loggers.Load(lable); ok {
if tmp, ok := v.(*Logger); ok { if tmp, ok := v.(*Logger); ok {
loggers.Delete(lable) loggers.Delete(lable)
@ -111,7 +111,7 @@ func Close(lables ...string) {
} }
} }
wg.Done() wg.Done()
}() }(lable)
} }
wg.Wait() wg.Wait()

View File

@ -2,8 +2,9 @@ package grlogs
import ( import (
"errors" "errors"
_ "golib.gaore.com/GaoreGo/grlogs/logs/alils" _ "haiwai-grlogs/logs/alils"
_ "golib.gaore.com/GaoreGo/grlogs/logs/es" _ "haiwai-grlogs/logs/es"
"os"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -62,12 +63,15 @@ func TestDropAdapter(t *testing.T) {
} }
func TestNew(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{} wg := sync.WaitGroup{}
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
wg.Add(1) wg.Add(1)
go func(i int) { go func(i int) {
GetAli("ok").Debug("%d", i) GetAli("ok").Debug("%d", i)
time.Sleep(time.Second * 10)
wg.Done() wg.Done()
}(i) }(i)
} }

View File

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/aliyun/aliyun-log-go-sdk/producer" "github.com/aliyun/aliyun-log-go-sdk/producer"
"golib.gaore.com/GaoreGo/grlogs/logs" "haiwai-grlogs/logs"
"os" "os"
"runtime" "runtime"
"time" "time"

View File

@ -10,7 +10,7 @@ import (
"time" "time"
"golib.gaore.com/GaoreGo/goes" "golib.gaore.com/GaoreGo/goes"
"golib.gaore.com/GaoreGo/grlogs/logs" "haiwai-grlogs/logs"
) )
// NewES return a LoggerInterface // NewES return a LoggerInterface