grlogs/log.go

53 lines
1.0 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
}
func (self *Logger) SetAdapter(level int, adapter string) *Logger {
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
}
func New(label string, channelLens ...int64) (l *Logger) {
var channellens int64
s, ok := loggers.LoadOrStore(label, new(Logger))
l = s.(*Logger)
2020-04-01 11:38:28 +08:00
if len(channelLens) > 0 {
channellens = channelLens[0]
}
2020-04-01 11:38:28 +08:00
if !ok {
l.Lable = label
l.BeeLogger = logs.NewLogger(channellens)
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
}
return
2020-04-01 11:38:28 +08:00
}
func Get(label string, channelLens ...int64) (l *Logger) {
var filelevel int = LevelWarning
if os.Getenv(envkey) == "dev" {
filelevel = LevelAll
2020-04-01 11:38:28 +08:00
}
l = New(label, channelLens...).SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole)
return
2020-04-01 11:38:28 +08:00
}