2020-04-01 11:38:28 +08:00
|
|
|
package grlogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-13 19:42:29 +08:00
|
|
|
"haiwai-grlogs/logs"
|
2020-04-24 17:15:43 +08:00
|
|
|
"os"
|
|
|
|
"sync"
|
2020-04-01 11:38:28 +08:00
|
|
|
)
|
|
|
|
|
2020-04-24 17:15:43 +08:00
|
|
|
var loggers = sync.Map{}
|
2020-04-01 11:38:28 +08:00
|
|
|
|
2023-03-13 19:42:29 +08:00
|
|
|
var envkey = "GAORE_ENVIRONMENT"
|
2020-04-27 21:26:13 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
var defaultModeMapping = map[string][]AdapterTupple{
|
|
|
|
"es": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterElasticSearch}},
|
|
|
|
"ali": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterAliLs}},
|
|
|
|
"": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterFile}},
|
|
|
|
}
|
|
|
|
|
2020-04-01 11:38:28 +08:00
|
|
|
type Logger struct {
|
2020-04-27 21:26:13 +08:00
|
|
|
Lable string
|
2020-04-01 11:38:28 +08:00
|
|
|
*logs.BeeLogger
|
|
|
|
}
|
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
type Getter func() *Logger
|
|
|
|
|
2020-04-28 10:37:47 +08:00
|
|
|
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
|
2020-05-06 14:32:26 +08:00
|
|
|
if call, ok := adatperSetMapper[adapter]; ok {
|
2020-04-27 21:26:13 +08:00
|
|
|
if err := call(self, level); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self
|
2020-04-01 11:38:28 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 14:32:26 +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-05-16 14:45:18 +08:00
|
|
|
func newLoggerFromMap(label string, defaultmode string, channelLens ...int64) Getter {
|
2020-04-01 11:38:28 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
if tmp, ok := loggers.Load(label); ok {
|
|
|
|
return tmp.(Getter)
|
2020-04-27 21:26:13 +08:00
|
|
|
}
|
2020-04-01 11:38:28 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
var l *Logger
|
|
|
|
var once sync.Once
|
|
|
|
wapperGetter := Getter(func() *Logger {
|
|
|
|
once.Do(func() {
|
2020-05-15 18:33:47 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
var channelLensNum int64 = 100
|
|
|
|
if len(channelLens) > 0 && channelLens[0] > 0 {
|
|
|
|
channelLensNum = channelLens[0]
|
|
|
|
}
|
2020-04-01 11:38:28 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
l = &Logger{BeeLogger: logs.NewLogger(channelLensNum)}
|
|
|
|
l.Lable = label
|
|
|
|
l.BeeLogger = logs.NewLogger(channelLensNum)
|
|
|
|
l.BeeLogger.Lable = label
|
|
|
|
l.Env = os.Getenv(envkey)
|
|
|
|
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
|
|
|
|
l.EnableFuncCallDepth(true)
|
|
|
|
l.SetLogFuncCallDepth(2)
|
|
|
|
if mode, ok := defaultModeMapping[defaultmode]; ok {
|
|
|
|
for _, v := range mode {
|
|
|
|
l.SetAdapter(v.Level, v.Adapter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-05-15 18:33:47 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
return l
|
|
|
|
})
|
2020-04-28 10:37:47 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
tmp, loaded := loggers.LoadOrStore(label, wapperGetter)
|
|
|
|
if loaded {
|
|
|
|
return tmp.(Getter)
|
2020-04-28 10:37:47 +08:00
|
|
|
}
|
2020-04-28 15:49:04 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
return wapperGetter
|
|
|
|
}
|
2020-04-28 10:37:47 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
func Get(label string, channelLens ...int64) *Logger {
|
|
|
|
getter := newLoggerFromMap(label, "", channelLens...)
|
|
|
|
return getter()
|
2020-05-05 17:04:08 +08:00
|
|
|
}
|
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
func GetEs(label string, channelLens ...int64) *Logger {
|
|
|
|
getter := newLoggerFromMap(label, "es", channelLens...)
|
|
|
|
return getter()
|
|
|
|
}
|
2020-05-06 10:19:07 +08:00
|
|
|
|
2020-05-16 14:45:18 +08:00
|
|
|
func GetAli(label string, channelLens ...int64) *Logger {
|
|
|
|
getter := newLoggerFromMap(label, "ali", channelLens...)
|
|
|
|
return getter()
|
2020-05-06 10:19:07 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 11:10:39 +08:00
|
|
|
func Close(lables ...string) {
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
for _, lable := range lables {
|
|
|
|
wg.Add(1)
|
2023-03-13 19:42:29 +08:00
|
|
|
go func(lable string) {
|
2020-05-06 11:10:39 +08:00
|
|
|
if v, ok := loggers.Load(lable); ok {
|
|
|
|
if tmp, ok := v.(*Logger); ok {
|
|
|
|
loggers.Delete(lable)
|
|
|
|
tmp.BeeLogger.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Done()
|
2023-03-13 19:42:29 +08:00
|
|
|
}(lable)
|
2020-05-05 17:04:08 +08:00
|
|
|
}
|
2020-05-06 11:10:39 +08:00
|
|
|
wg.Wait()
|
|
|
|
|
2020-05-05 17:04:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func CloseAll() {
|
2020-05-06 11:10:39 +08:00
|
|
|
wg := &sync.WaitGroup{}
|
2020-05-05 17:04:08 +08:00
|
|
|
loggers.Range(func(key, value interface{}) bool {
|
2020-05-06 11:10:39 +08:00
|
|
|
wg.Add(1)
|
2020-05-05 17:04:08 +08:00
|
|
|
go func() {
|
|
|
|
if tmp, ok := value.(*Logger); ok {
|
|
|
|
tmp.BeeLogger.Close()
|
2020-05-06 11:10:39 +08:00
|
|
|
loggers.Delete(key)
|
2020-05-05 17:04:08 +08:00
|
|
|
}
|
2020-05-06 11:10:39 +08:00
|
|
|
wg.Done()
|
2020-05-05 17:04:08 +08:00
|
|
|
}()
|
|
|
|
return true
|
|
|
|
})
|
2020-05-06 11:10:39 +08:00
|
|
|
wg.Wait()
|
2020-04-01 11:38:28 +08:00
|
|
|
}
|
2020-05-16 14:45:18 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
var level Level = LevelInfo
|
|
|
|
if os.Getenv(envkey) == "dev" {
|
|
|
|
level = LevelAll
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultModeMapping["es"] = []AdapterTupple{{level, AdapterConsole}, {level, AdapterElasticSearch}}
|
|
|
|
defaultModeMapping["ali"] = []AdapterTupple{{level, AdapterConsole}, {level, AdapterAliLs}}
|
|
|
|
defaultModeMapping[""] = []AdapterTupple{{level, AdapterConsole}, {level, AdapterFile}}
|
|
|
|
}
|