haiwai-grlogs/log.go

131 lines
2.6 KiB
Go
Raw Normal View History

2020-04-01 11:38:28 +08:00
package grlogs
import (
"fmt"
"golib.gaore.com/GaoreGo/grlogs/logs"
"os"
"sync"
2020-04-01 11:38:28 +08:00
)
var loggers = sync.Map{}
2020-04-01 11:38:28 +08:00
var envkey = "CENTER_RUNMODE"
2020-04-01 11:38:28 +08:00
type Logger struct {
Lable string
2020-04-01 11:38:28 +08:00
*logs.BeeLogger
}
2020-04-28 10:37:47 +08:00
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
if call, ok := adatperSetMapper[adapter]; ok {
if err := call(self, level); err != nil {
fmt.Println(err)
}
}
return self
2020-04-01 11:38:28 +08:00
}
func (self *Logger) DropAdapter(adapter Adapter) *Logger {
if call, ok := adatperDropMapper[adapter]; ok {
if err := call(self); err != nil {
fmt.Println(err)
}
}
return self
}
2020-04-28 10:37:47 +08:00
func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
var channellens int64
2020-04-28 10:37:47 +08:00
var tmp interface{}
2020-04-01 11:38:28 +08:00
2020-04-28 10:37:47 +08:00
tmp, loaded = loggers.LoadOrStore(label, new(Logger))
l = tmp.(*Logger)
if len(channelLens) > 0 {
channellens = channelLens[0]
}
2020-04-01 11:38:28 +08:00
2020-04-28 10:37:47 +08:00
if !loaded {
l.Lable = label
l.BeeLogger = logs.NewLogger(channellens)
2020-04-28 15:27:39 +08:00
l.BeeLogger.Lable = label
l.Env = os.Getenv(envkey)
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
2020-04-28 15:27:39 +08:00
l.EnableFuncCallDepth(true)
l.SetLogFuncCallDepth(2)
}
return
2020-04-01 11:38:28 +08:00
}
func Get(label string, channelLens ...int64) (l *Logger) {
2020-04-28 15:49:04 +08:00
var filelevel Level = LevelInfo
2020-04-28 10:37:47 +08:00
var loaded bool
if os.Getenv(envkey) == "dev" {
filelevel = LevelAll
2020-04-01 11:38:28 +08:00
}
2020-04-28 10:37:47 +08:00
if l, loaded = New(label, channelLens...); !loaded {
l.SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole)
}
2020-04-28 15:49:04 +08:00
return
}
func GetEs(label string, channelLens ...int64) (l *Logger) {
var filelevel Level = LevelInfo
var loaded bool
if os.Getenv(envkey) == "dev" {
filelevel = LevelAll
}
2020-04-28 10:37:47 +08:00
2020-04-28 15:49:04 +08:00
if l, loaded = New(label, channelLens...); !loaded {
l.SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole).SetAdapter(filelevel, AdapterElasticSearch)
}
return
2020-05-05 17:04:08 +08:00
}
2020-05-06 10:19:07 +08:00
func GetAli(label string, channelLens ...int64) (l *Logger) {
var filelevel Level = LevelInfo
var loaded bool
if os.Getenv(envkey) == "dev" {
filelevel = LevelAll
}
if l, loaded = New(label, channelLens...); !loaded {
l.SetAdapter(LevelAll, AdapterConsole).SetAdapter(filelevel, AdapterFile).SetAdapter(filelevel, AdapterAliLs)
2020-05-06 10:19:07 +08:00
}
return
}
func Close(lables ...string) {
wg := &sync.WaitGroup{}
for _, lable := range lables {
wg.Add(1)
go func() {
if v, ok := loggers.Load(lable); ok {
if tmp, ok := v.(*Logger); ok {
loggers.Delete(lable)
tmp.BeeLogger.Close()
}
}
wg.Done()
}()
2020-05-05 17:04:08 +08:00
}
wg.Wait()
2020-05-05 17:04:08 +08:00
}
func CloseAll() {
wg := &sync.WaitGroup{}
2020-05-05 17:04:08 +08:00
loggers.Range(func(key, value interface{}) bool {
wg.Add(1)
2020-05-05 17:04:08 +08:00
go func() {
if tmp, ok := value.(*Logger); ok {
tmp.BeeLogger.Close()
loggers.Delete(key)
2020-05-05 17:04:08 +08:00
}
wg.Done()
2020-05-05 17:04:08 +08:00
}()
return true
})
wg.Wait()
2020-04-01 11:38:28 +08:00
}