haiwai-grlogs/log.go
2023-03-13 19:42:29 +08:00

146 lines
3.2 KiB
Go

package grlogs
import (
"fmt"
"haiwai-grlogs/logs"
"os"
"sync"
)
var loggers = sync.Map{}
var envkey = "GAORE_ENVIRONMENT"
var defaultModeMapping = map[string][]AdapterTupple{
"es": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterElasticSearch}},
"ali": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterAliLs}},
"": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterFile}},
}
type Logger struct {
Lable string
*logs.BeeLogger
}
type Getter func() *Logger
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
}
func (self *Logger) DropAdapter(adapter Adapter) *Logger {
if call, ok := adatperDropMapper[adapter]; ok {
if err := call(self); err != nil {
fmt.Println(err)
}
}
return self
}
func newLoggerFromMap(label string, defaultmode string, channelLens ...int64) Getter {
if tmp, ok := loggers.Load(label); ok {
return tmp.(Getter)
}
var l *Logger
var once sync.Once
wapperGetter := Getter(func() *Logger {
once.Do(func() {
var channelLensNum int64 = 100
if len(channelLens) > 0 && channelLens[0] > 0 {
channelLensNum = channelLens[0]
}
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)
}
}
})
return l
})
tmp, loaded := loggers.LoadOrStore(label, wapperGetter)
if loaded {
return tmp.(Getter)
}
return wapperGetter
}
func Get(label string, channelLens ...int64) *Logger {
getter := newLoggerFromMap(label, "", channelLens...)
return getter()
}
func GetEs(label string, channelLens ...int64) *Logger {
getter := newLoggerFromMap(label, "es", channelLens...)
return getter()
}
func GetAli(label string, channelLens ...int64) *Logger {
getter := newLoggerFromMap(label, "ali", channelLens...)
return getter()
}
func Close(lables ...string) {
wg := &sync.WaitGroup{}
for _, lable := range lables {
wg.Add(1)
go func(lable string) {
if v, ok := loggers.Load(lable); ok {
if tmp, ok := v.(*Logger); ok {
loggers.Delete(lable)
tmp.BeeLogger.Close()
}
}
wg.Done()
}(lable)
}
wg.Wait()
}
func CloseAll() {
wg := &sync.WaitGroup{}
loggers.Range(func(key, value interface{}) bool {
wg.Add(1)
go func() {
if tmp, ok := value.(*Logger); ok {
tmp.BeeLogger.Close()
loggers.Delete(key)
}
wg.Done()
}()
return true
})
wg.Wait()
}
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}}
}