diff --git a/log.go b/log.go index 686390d..4bf6ae9 100644 --- a/log.go +++ b/log.go @@ -19,10 +19,12 @@ import ( ) var ( - glogger *log.Logger = nil - once sync.Once = sync.Once{} - mylogger *Logger = nil - modellogs map[string]*Logger = map[string]*Logger{} + glogger *log.Logger = nil + once sync.Once = sync.Once{} + mylogger *Logger = nil + modulelogs map[string]*Logger = map[string]*Logger{} + logidSetter func(string) = nil + logidGetter func() string = nil ) const ErrLogPanic = "~~~~panic~~~~~~" @@ -47,6 +49,9 @@ func NoticefWithDepth(calldepth int, format string, v ...interface{}) { func Warningf(format string, v ...interface{}) { mylogger.Warningf(format, v...) } +func Errorf(format string, v ...interface{}) { + mylogger.Warningf(format, v...) +} func Panicf(format string, v ...interface{}) { mylogger.Panicf(format, v...) } @@ -56,19 +61,19 @@ func Fatalf(format string, v ...interface{}) { ////////////////////////////////////////////////////////////////// type Logger struct { - model string + module string logger *log.Logger } -func New(model string) (logger *Logger) { - if model == "" { - model = "undefine" +func New(module string) (logger *Logger) { + if module == "" { + module = "undefine" } - if _, ok := modellogs[model]; ok { - return modellogs[model] + if _, ok := modulelogs[module]; ok { + return modulelogs[module] } else { - modellogs[model] = &Logger{model: model, logger: glogger} - return modellogs[model] + modulelogs[module] = &Logger{module: module, logger: glogger} + return modulelogs[module] } } @@ -103,6 +108,10 @@ func (log *Logger) Warningf(format string, v ...interface{}) { 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{}) { log.logwrite("FATAL", 3, format, v...) } @@ -111,36 +120,48 @@ func (log *Logger) Panicf(format string, v ...interface{}) { log.logwrite("PANIC", 3, format, v...) } -func (log *Logger) logwrite(typ string, calldepth int, format string, v ...interface{}) { - var ( - l = gls.GetGlsValue("logid") - id = "0" - ) - if l != nil { - id = l.(string) +func GetLogid() string { + if logidGetter != nil { + return logidGetter() } - 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 { case "PANIC": - log.logger.SetPrefix("\x1b[31m" + "PANIC [" + id + "] [" + log.model + "] ") + log.logger.SetPrefix("\x1b[31m" + "PANIC " + idstr + "[" + log.module + "] ") case "FATAL": - log.logger.SetPrefix("\x1b[31m" + "FATAL [" + id + "] [" + log.model + "] ") + log.logger.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ") 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": - log.logger.SetPrefix("NOTICE [" + id + "] [" + log.model + "] ") + log.logger.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ") case "DEBUGE": - log.logger.SetPrefix("DEBUGE [" + id + "] [" + log.model + "] ") + log.logger.SetPrefix("DEBUGE " + idstr + "[" + log.module + "] ") default: - log.logger.SetPrefix("UNKNOWN [" + id + "] [" + log.model + "] ") + log.logger.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ") } if mylogger == log { 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...)) } else if typ == "NOTICE" { calldepth = calldepth + 2 @@ -227,7 +248,14 @@ func initlog() { _, logfilename := filepath.Split(os.Args[0]) 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) mylogger = New("system") @@ -251,7 +279,7 @@ func initlog() { glogger = openlog(LOGFILENAME) mylogger = New("system") - for _, l := range modellogs { + for _, l := range modulelogs { l.logger = glogger } }