From 71b00c8a37f550854f159375289a7069c0cb69c7 Mon Sep 17 00:00:00 2001 From: bryanqiu Date: Tue, 15 Nov 2022 15:19:18 +0800 Subject: [PATCH] add Infof & logLevel --- log.go | 98 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 29 deletions(-) diff --git a/log.go b/log.go index 3860cae..4f16c84 100644 --- a/log.go +++ b/log.go @@ -30,7 +30,9 @@ var ( const ErrLogPanic = "~~~~panic~~~~~~" //////////////global log//////////////////begin/////////////////// - +func SetLogLevel(newlv logLevel) (oldlv logLevel) { + return mylogger.SetLogLevel(newlv) +} func PrintPretty(prefix string, v interface{}) { mylogger.PrintPretty(prefix, v) } @@ -46,6 +48,9 @@ func Noticef(format string, v ...interface{}) { func NoticefWithDepth(calldepth int, format string, v ...interface{}) { mylogger.NoticefWithDepth(calldepth, format, v...) } +func Infof(format string, v ...interface{}) { + mylogger.Infof(format, v...) +} func Warningf(format string, v ...interface{}) { mylogger.Warningf(format, v...) } @@ -60,9 +65,23 @@ func Fatalf(format string, v ...interface{}) { } ////////////////////////////////////////////////////////////////// + +type logLevel int + +const ( + PANIC logLevel = 0 + FATAL logLevel = 1 + ERROR logLevel = 2 + NOTICE logLevel = 3 + WARNING logLevel = 4 + INFO logLevel = 5 + DEBUG logLevel = 9 +) + type Logger struct { - module string - logger *log.Logger + module string + loglevel logLevel + logger *log.Logger } func New(module string) (logger *Logger) { @@ -72,52 +91,67 @@ func New(module string) (logger *Logger) { if _, ok := modulelogs[module]; ok { return modulelogs[module] } else { - modulelogs[module] = &Logger{module: module, logger: glogger} + modulelogs[module] = &Logger{ + module: module, + logger: glogger, + loglevel: DEBUG, + } return modulelogs[module] } } +func (log *Logger) SetLogLevel(newlv logLevel) (oldlv logLevel) { + oldlv = log.loglevel + log.loglevel = newlv + return oldlv +} + func (log *Logger) Print(prefix string, v interface{}) { preety, _ := json.Marshal(v) - log.logwrite("DEBUGE", 3, prefix+"%s\n", preety) + log.logwrite(DEBUG, 3, prefix+"%s\n", preety) } + func (log *Logger) PrintPretty(prefix string, v interface{}) { preety, _ := json.MarshalIndent(v, "==", " ") - log.logwrite("DEBUGE", 3, prefix+"%s\n", preety) + log.logwrite(DEBUG, 3, prefix+"%s\n", preety) } func (log *Logger) Debugf(format string, v ...interface{}) { - log.logwrite("DEBUGE", 3, format, v...) + log.logwrite(DEBUG, 3, format, v...) } func (log *Logger) DebugfWithDepth(calldepth int, format string, v ...interface{}) { calldepth += 3 - log.logwrite("DEBUGE", calldepth, format, v...) + log.logwrite(DEBUG, calldepth, format, v...) } func (log *Logger) Noticef(format string, v ...interface{}) { - log.logwrite("NOTICE", 3, format, v...) + log.logwrite(NOTICE, 3, format, v...) } func (log *Logger) NoticefWithDepth(calldepth int, format string, v ...interface{}) { calldepth += 3 - log.logwrite("NOTICE", calldepth, format, v...) + log.logwrite(NOTICE, calldepth, format, v...) } func (log *Logger) Warningf(format string, v ...interface{}) { - log.logwrite("WARNING", 3, format, v...) + log.logwrite(WARNING, 3, format, v...) +} + +func (log *Logger) Infof(format string, v ...interface{}) { + log.logwrite(INFO, 3, format, v...) } func (log *Logger) Errorf(format string, v ...interface{}) { - log.logwrite("ERROR", 3, format, v...) + log.logwrite(ERROR, 3, format, v...) } func (log *Logger) Fatalf(format string, v ...interface{}) { - log.logwrite("FATAL", 3, format, v...) + log.logwrite(FATAL, 3, format, v...) } func (log *Logger) Panicf(format string, v ...interface{}) { - log.logwrite("PANIC", 3, format, v...) + log.logwrite(PANIC, 3, format, v...) } func GetLogid() string { @@ -133,7 +167,11 @@ func SetLogid(logid string) { } } -func (log *Logger) logwrite(typ string, calldepth int, format string, v ...interface{}) { +func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...interface{}) { + if typ > log.loglevel { + return + } + var idstr = "" if id := GetLogid(); id != "" { idstr = "[" + id + "] " @@ -141,32 +179,34 @@ func (log *Logger) logwrite(typ string, calldepth int, format string, v ...inter format = strings.Trim(format, "\n") switch typ { - case "PANIC": + case PANIC: log.logger.SetPrefix("\x1b[31m" + "PANIC " + idstr + "[" + log.module + "] ") - case "FATAL": - log.logger.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ") - case "WARNING": - log.logger.SetPrefix("\x1b[32m" + "WARNING " + idstr + "[" + log.module + "] ") - case "ERROR": + case FATAL: + log.logger.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ") + case WARNING: + 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 " + idstr + "[" + log.module + "] ") - case "DEBUGE": - log.logger.SetPrefix("DEBUGE " + idstr + "[" + log.module + "] ") + case INFO: + log.logger.SetPrefix("INFO " + idstr + "[" + log.module + "] ") + case NOTICE: + log.logger.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ") + case DEBUG: + log.logger.SetPrefix("DEBUG " + idstr + "[" + log.module + "] ") default: - log.logger.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ") + log.logger.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ") } if mylogger == log { calldepth = calldepth + 1 } - if typ == "FATAL" || typ == "WARNING" || typ == "ERROR" { + if typ == FATAL || typ == WARNING || typ == ERROR { log.logger.Output(calldepth, fmt.Sprintf(format+"\x1b[0m\n", v...)) - } else if typ == "NOTICE" { + } else if typ == NOTICE { calldepth = calldepth + 2 log.logger.Output(calldepth, fmt.Sprintf(format+"\n", v...)) - } else if typ == "PANIC" { + } else if typ == PANIC { stack := strings.Replace(string(debug.Stack()), "\n", "\n== ", -1) stack = str.SkipLine(stack, calldepth*2+1) v = append(v, stack)