2020-04-01 11:38:28 +08:00
|
|
|
package grlogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-24 17:15:43 +08:00
|
|
|
"golib.gaore.com/GaoreGo/grlogs/logs"
|
|
|
|
"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
|
|
|
|
2020-04-27 21:26:13 +08:00
|
|
|
var envkey = "CENTER_RUNMODE"
|
|
|
|
|
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-04-28 10:37:47 +08:00
|
|
|
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
|
2020-04-27 21:26:13 +08:00
|
|
|
if call, ok := adatperMapper[adapter]; ok {
|
|
|
|
if err := call(self, level); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self
|
2020-04-01 11:38:28 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 10:37:47 +08:00
|
|
|
func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
|
2020-04-27 21:26:13 +08:00
|
|
|
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)
|
2020-04-27 21:26:13 +08:00
|
|
|
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 {
|
2020-04-27 21:26:13 +08:00
|
|
|
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)
|
2020-04-27 21:26:13 +08:00
|
|
|
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)
|
2020-04-27 21:26:13 +08:00
|
|
|
}
|
|
|
|
return
|
2020-04-01 11:38:28 +08:00
|
|
|
}
|
|
|
|
|
2020-04-27 21:26:13 +08:00
|
|
|
func Get(label string, channelLens ...int64) (l *Logger) {
|
2020-04-28 10:37:47 +08:00
|
|
|
var filelevel Level = LevelWarning
|
|
|
|
var loaded bool
|
2020-04-27 21:26:13 +08:00
|
|
|
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-27 21:26:13 +08:00
|
|
|
return
|
2020-04-01 11:38:28 +08:00
|
|
|
}
|