From c014aca84bf5d29dbe09e6554bb4f4e4cfe98367 Mon Sep 17 00:00:00 2001 From: liangzy Date: Tue, 5 May 2020 17:04:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E9=98=BF=E9=87=8Csls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log.go | 22 +++++++++++++++++++++- log_test.go | 23 ++++++++++++++--------- logs/alils/alils.go | 4 ++-- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/log.go b/log.go index 1e490a6..de68f00 100644 --- a/log.go +++ b/log.go @@ -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 + }) } diff --git a/log_test.go b/log_test.go index 281a930..fc0a0b4 100644 --- a/log_test.go +++ b/log_test.go @@ -33,13 +33,18 @@ func TestGetLogger(t *testing.T) { } func TestGetAliLs(t *testing.T) { - l := Get("es").SetAdapter(LevelAll, AdapterAliLs) - l.Info("endport") - l.Info("endport") - l.Info("endport") - l.Info("endport") - l.Error("Error") - l.Error("Error") - l.Warn("Warn") - time.Sleep(time.Millisecond * 500) + + 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\n\n Error") + l.Warn("Warn") + } + + Close("test_alils") } diff --git a/logs/alils/alils.go b/logs/alils/alils.go index c79c590..7c29b2d 100644 --- a/logs/alils/alils.go +++ b/logs/alils/alils.go @@ -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() {