From 2679e8a2bf3a39f3e36c1f1f51f22f5f455c7f1d Mon Sep 17 00:00:00 2001 From: boyce Date: Wed, 20 Jan 2021 11:16:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EStack=E7=AD=89=E7=BA=A7?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/log.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/log/log.go b/log/log.go index 40f73f0..8d2b542 100644 --- a/log/log.go +++ b/log/log.go @@ -6,6 +6,7 @@ import ( "log" "os" "path" + "runtime/debug" "strings" "time" ) @@ -18,7 +19,8 @@ const ( releaseLevel = 1 warningLevel = 2 errorLevel = 3 - fatalLevel = 4 + stackLevel = 4 + fatalLevel = 5 ) const ( @@ -26,6 +28,7 @@ const ( printReleaseLevel = "[release] " printWarningLevel = "[warning] " printErrorLevel = "[error ] " + printStackLevel = "[stack ] " printFatalLevel = "[fatal ] " ) @@ -51,6 +54,8 @@ func New(strLevel string, pathname string, flag int) (*Logger, error) { level = warningLevel case "error": level = errorLevel + case "stack": + level = stackLevel case "fatal": level = fatalLevel default: @@ -159,6 +164,10 @@ func (logger *Logger) Error(format string, a ...interface{}) { logger.doPrintf(errorLevel, printErrorLevel, format, a...) } +func (logger *Logger) Stack(format string, a ...interface{}) { + logger.doPrintf(stackLevel, printStackLevel, format, a...) +} + func (logger *Logger) Fatal(format string, a ...interface{}) { logger.doPrintf(fatalLevel, printFatalLevel, format, a...) } @@ -188,6 +197,11 @@ func Error(format string, a ...interface{}) { gLogger.doPrintf(errorLevel, printErrorLevel, format, a...) } +func Stack(format string, a ...interface{}) { + s := string(debug.Stack()) + gLogger.doPrintf(stackLevel, printStackLevel, s+"\n"+format, a...) +} + func Fatal(format string, a ...interface{}) { gLogger.doPrintf(fatalLevel, printFatalLevel, format, a...) }