remove gls

This commit is contained in:
bryanqiu 2022-10-24 10:09:22 +08:00
parent 8393a71c5a
commit 52b4d750d1

86
log.go
View File

@ -19,10 +19,12 @@ import (
) )
var ( var (
glogger *log.Logger = nil glogger *log.Logger = nil
once sync.Once = sync.Once{} once sync.Once = sync.Once{}
mylogger *Logger = nil mylogger *Logger = nil
modellogs map[string]*Logger = map[string]*Logger{} modulelogs map[string]*Logger = map[string]*Logger{}
logidSetter func(string) = nil
logidGetter func() string = nil
) )
const ErrLogPanic = "~~~~panic~~~~~~" const ErrLogPanic = "~~~~panic~~~~~~"
@ -47,6 +49,9 @@ func NoticefWithDepth(calldepth int, format string, v ...interface{}) {
func Warningf(format string, v ...interface{}) { func Warningf(format string, v ...interface{}) {
mylogger.Warningf(format, v...) mylogger.Warningf(format, v...)
} }
func Errorf(format string, v ...interface{}) {
mylogger.Warningf(format, v...)
}
func Panicf(format string, v ...interface{}) { func Panicf(format string, v ...interface{}) {
mylogger.Panicf(format, v...) mylogger.Panicf(format, v...)
} }
@ -56,19 +61,19 @@ func Fatalf(format string, v ...interface{}) {
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
type Logger struct { type Logger struct {
model string module string
logger *log.Logger logger *log.Logger
} }
func New(model string) (logger *Logger) { func New(module string) (logger *Logger) {
if model == "" { if module == "" {
model = "undefine" module = "undefine"
} }
if _, ok := modellogs[model]; ok { if _, ok := modulelogs[module]; ok {
return modellogs[model] return modulelogs[module]
} else { } else {
modellogs[model] = &Logger{model: model, logger: glogger} modulelogs[module] = &Logger{module: module, logger: glogger}
return modellogs[model] return modulelogs[module]
} }
} }
@ -103,6 +108,10 @@ func (log *Logger) Warningf(format string, v ...interface{}) {
log.logwrite("WARNING", 3, format, v...) log.logwrite("WARNING", 3, format, v...)
} }
func (log *Logger) Errorf(format string, v ...interface{}) {
log.logwrite("ERROR", 3, format, v...)
}
func (log *Logger) Fatalf(format string, v ...interface{}) { func (log *Logger) Fatalf(format string, v ...interface{}) {
log.logwrite("FATAL", 3, format, v...) log.logwrite("FATAL", 3, format, v...)
} }
@ -111,36 +120,48 @@ func (log *Logger) Panicf(format string, v ...interface{}) {
log.logwrite("PANIC", 3, format, v...) log.logwrite("PANIC", 3, format, v...)
} }
func (log *Logger) logwrite(typ string, calldepth int, format string, v ...interface{}) { func GetLogid() string {
var ( if logidGetter != nil {
l = gls.GetGlsValue("logid") return logidGetter()
id = "0"
)
if l != nil {
id = l.(string)
} }
format = strings.Trim(format, "\n") return ""
}
func SetLogid(logid string) {
if logidSetter != nil {
logidSetter(logid)
}
}
func (log *Logger) logwrite(typ string, calldepth int, format string, v ...interface{}) {
var idstr = ""
if id := GetLogid(); id != "" {
idstr = "[" + id + "] "
}
format = strings.Trim(format, "\n")
switch typ { switch typ {
case "PANIC": case "PANIC":
log.logger.SetPrefix("\x1b[31m" + "PANIC [" + id + "] [" + log.model + "] ") log.logger.SetPrefix("\x1b[31m" + "PANIC " + idstr + "[" + log.module + "] ")
case "FATAL": case "FATAL":
log.logger.SetPrefix("\x1b[31m" + "FATAL [" + id + "] [" + log.model + "] ") log.logger.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ")
case "WARNING": case "WARNING":
log.logger.SetPrefix("\x1b[32m" + "WARNING [" + id + "] [" + log.model + "] ") log.logger.SetPrefix("\x1b[32m" + "WARNING " + idstr + "[" + log.module + "] ")
case "ERROR":
log.logger.SetPrefix("\x1b[32m" + "ERROR " + idstr + "[" + log.module + "] ")
case "NOTICE": case "NOTICE":
log.logger.SetPrefix("NOTICE [" + id + "] [" + log.model + "] ") log.logger.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ")
case "DEBUGE": case "DEBUGE":
log.logger.SetPrefix("DEBUGE [" + id + "] [" + log.model + "] ") log.logger.SetPrefix("DEBUGE " + idstr + "[" + log.module + "] ")
default: default:
log.logger.SetPrefix("UNKNOWN [" + id + "] [" + log.model + "] ") log.logger.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ")
} }
if mylogger == log { if mylogger == log {
calldepth = calldepth + 1 calldepth = calldepth + 1
} }
if typ == "FATAL" || typ == "WARNING" { if typ == "FATAL" || typ == "WARNING" || typ == "ERROR" {
log.logger.Output(calldepth, fmt.Sprintf(format+"\x1b[0m\n", v...)) log.logger.Output(calldepth, fmt.Sprintf(format+"\x1b[0m\n", v...))
} else if typ == "NOTICE" { } else if typ == "NOTICE" {
calldepth = calldepth + 2 calldepth = calldepth + 2
@ -227,7 +248,14 @@ func initlog() {
_, logfilename := filepath.Split(os.Args[0]) _, logfilename := filepath.Split(os.Args[0])
LOGSPLITTIME := "20060102" //20060102150405 LOGSPLITTIME := "20060102" //20060102150405
LOGFILENAME := "log/" + logfilename + ".log" LOGFILENAME := logfilename + ".log"
if _, err := os.Stat("log/"); err == nil {
LOGFILENAME = "log/" + LOGFILENAME
} else if _, err := os.Stat("logs/"); err == nil {
LOGFILENAME = "logs/" + LOGFILENAME
} else {
LOGFILENAME = "logs/" + LOGFILENAME
}
glogger = openlog(LOGFILENAME) glogger = openlog(LOGFILENAME)
mylogger = New("system") mylogger = New("system")
@ -251,7 +279,7 @@ func initlog() {
glogger = openlog(LOGFILENAME) glogger = openlog(LOGFILENAME)
mylogger = New("system") mylogger = New("system")
for _, l := range modellogs { for _, l := range modulelogs {
l.logger = glogger l.logger = glogger
} }
} }