接入阿里sls

This commit is contained in:
liangzy 2020-05-05 17:04:08 +08:00
parent 4843033fe0
commit c014aca84b
3 changed files with 37 additions and 12 deletions

22
log.go
View File

@ -71,5 +71,25 @@ func GetEs(label string, channelLens ...int64) (l *Logger) {
l.SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole).SetAdapter(filelevel, AdapterElasticSearch)
}
return
}
func Close(lable string) {
if v, ok := loggers.Load(lable); ok {
if tmp, ok := v.(*Logger); ok {
tmp.BeeLogger.Close()
}
}
loggers.Delete(lable)
}
func CloseAll() {
loggers.Range(func(key, value interface{}) bool {
go func() {
if tmp, ok := value.(*Logger); ok {
tmp.BeeLogger.Close()
}
}()
return true
})
}

View File

@ -33,13 +33,18 @@ func TestGetLogger(t *testing.T) {
}
func TestGetAliLs(t *testing.T) {
l := Get("es").SetAdapter(LevelAll, AdapterAliLs)
l := Get("test_alils").SetAdapter(LevelAll, AdapterAliLs)
for i := 0; i < 1; i++ {
l.Info("endport")
l.Info("endport")
l.Info("endport")
l.Info("endport")
l.Error("Error")
l.Error("Error")
l.Error("Error\n\n Error")
l.Warn("Warn")
time.Sleep(time.Millisecond * 500)
}
Close("test_alils")
}

View File

@ -35,12 +35,12 @@ func (a *alilsLogger) Init(jsonconfig string) error {
if err != nil {
return err
}
producerConfig := producer.GetDefaultProducerConfig()
producerConfig.Endpoint = a.Endpoint
producerConfig.AccessKeyID = a.KeyID
producerConfig.AccessKeySecret = a.KeySecret
producerConfig.LingerMs = 100
producerConfig.AllowLogLevel = "error"
a.producer = producer.InitProducer(producerConfig)
a.callback = &Callback{}
a.producer.Start()
@ -72,8 +72,8 @@ func (a *alilsLogger) WriteMsg(when time.Time, msg string, level int, lable stri
}
func (a *alilsLogger) Destroy() {
a.producer.Close(60)
a.producer.SafeClose()
a.producer.Close(300)
}
func (a *alilsLogger) Flush() {