59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package grlogs
|
|
|
|
import (
|
|
"fmt"
|
|
"golib.gaore.com/GaoreGo/grlogs/logs"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
var loggers = sync.Map{}
|
|
|
|
var envkey = "CENTER_RUNMODE"
|
|
|
|
type Logger struct {
|
|
Lable string
|
|
*logs.BeeLogger
|
|
}
|
|
|
|
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
|
|
if call, ok := adatperMapper[adapter]; ok {
|
|
if err := call(self, level); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
return self
|
|
}
|
|
|
|
func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
|
|
var channellens int64
|
|
var tmp interface{}
|
|
|
|
tmp, loaded = loggers.LoadOrStore(label, new(Logger))
|
|
l = tmp.(*Logger)
|
|
if len(channelLens) > 0 {
|
|
channellens = channelLens[0]
|
|
}
|
|
|
|
if !loaded {
|
|
l.Lable = label
|
|
l.BeeLogger = logs.NewLogger(channellens)
|
|
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
|
|
}
|
|
return
|
|
}
|
|
|
|
func Get(label string, channelLens ...int64) (l *Logger) {
|
|
var filelevel Level = LevelWarning
|
|
var loaded bool
|
|
if os.Getenv(envkey) == "dev" {
|
|
filelevel = LevelAll
|
|
}
|
|
|
|
if l, loaded = New(label, channelLens...); !loaded {
|
|
l.SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole)
|
|
}
|
|
|
|
return
|
|
}
|