2020-04-01 11:38:28 +08:00
|
|
|
package grlogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-04-24 17:15:43 +08:00
|
|
|
"golib.gaore.com/GaoreGo/grlogs/logs"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sync"
|
2020-04-01 11:38:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ = iota
|
|
|
|
LEVEL_NONE = iota
|
|
|
|
LEVEL_CRITICAL = iota
|
|
|
|
LEVEL_ERROR = iota
|
|
|
|
LEVEL_WARNING = iota
|
|
|
|
LEVEL_WARN = iota
|
|
|
|
LEVEL_INFO = iota
|
|
|
|
LEVEL_DEBUG = iota
|
|
|
|
LEVEL_ALL = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConsoleLogConfig struct {
|
|
|
|
Level int `json:"level"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileLogConfig struct {
|
|
|
|
Filename string `json:"filename"`
|
|
|
|
Level int `json:"level"`
|
|
|
|
Maxlines int `json:"maxlines"`
|
|
|
|
Daily bool `json:"daily"`
|
|
|
|
Maxdays int `json:"maxdays"`
|
|
|
|
Color bool `json:"color"`
|
|
|
|
Rotate bool `json:"rotate"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConnLogConfig struct {
|
|
|
|
ReconnectOnMsg bool `json:"reconnect_on_msg"`
|
|
|
|
Reconnect bool `json:"reconnect"`
|
|
|
|
Net string `json:"net"`
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
Level int `json:"level"`
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:15:43 +08:00
|
|
|
var loggers = sync.Map{}
|
2020-04-01 11:38:28 +08:00
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
*logs.BeeLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Write(p []byte) (n int, err error) {
|
|
|
|
l.Debug(bytes.NewBuffer(p).String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) SetFormat() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Stop() {
|
|
|
|
l.BeeLogger.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLogger(name string) *Logger {
|
|
|
|
|
2020-04-24 17:15:43 +08:00
|
|
|
if l, ok := loggers.Load(name); ok {
|
|
|
|
return l.(*Logger)
|
2020-04-01 11:38:28 +08:00
|
|
|
} else {
|
|
|
|
var level int = LEVEL_WARN
|
|
|
|
|
2020-04-24 17:15:43 +08:00
|
|
|
if s := os.Getenv("CENTER_RUNMODE"); s == "dev" {
|
2020-04-01 11:38:28 +08:00
|
|
|
level = LEVEL_ALL
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:15:43 +08:00
|
|
|
wd, _ := os.Getwd()
|
|
|
|
|
2020-04-01 11:38:28 +08:00
|
|
|
conf1 := FileLogConfig{
|
2020-04-24 17:15:43 +08:00
|
|
|
Filename: path.Join(wd, fmt.Sprintf("runtime/logs/%s.log", name)),
|
2020-04-01 11:38:28 +08:00
|
|
|
Level: LEVEL_ALL,
|
|
|
|
Maxlines: 0,
|
|
|
|
Daily: true,
|
|
|
|
Maxdays: 7,
|
|
|
|
Color: true,
|
|
|
|
Rotate: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
conf2 := ConsoleLogConfig{
|
|
|
|
Level: level,
|
|
|
|
}
|
|
|
|
|
|
|
|
confString, _ := json.Marshal(&conf1)
|
|
|
|
confString2, _ := json.Marshal(&conf2)
|
2020-04-24 17:15:43 +08:00
|
|
|
l := new(Logger)
|
2020-04-24 17:34:39 +08:00
|
|
|
l.BeeLogger = logs.NewLogger(1000)
|
2020-04-24 17:15:43 +08:00
|
|
|
l.SetLogger(logs.AdapterFile, bytes.NewBuffer(confString).String())
|
|
|
|
l.SetLogger(logs.AdapterConsole, bytes.NewBuffer(confString2).String())
|
|
|
|
l.BeeLogger.SetPrefix("_" + name)
|
|
|
|
l.BeeLogger.EnableFuncCallDepth(true)
|
|
|
|
l.BeeLogger.SetLogFuncCallDepth(2)
|
|
|
|
loggers.Store(name, l)
|
|
|
|
return l
|
2020-04-01 11:38:28 +08:00
|
|
|
}
|
|
|
|
}
|