From 4a5e10852775164ae03a8cfffe5b86a1f11f018a Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 8 Mar 2019 11:50:30 +0800 Subject: [PATCH] logger color function refactor,easy to read and run more quickly --- logs/logger.go | 8 ++++++-- router.go | 37 ++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/logs/logger.go b/logs/logger.go index b6f0c86b..c744d45c 100644 --- a/logs/logger.go +++ b/logs/logger.go @@ -180,7 +180,7 @@ func initColor() { // 3xx return White // 4xx return Yellow // 5xx return Red -func ColorByStatus(cond bool, code int) string { +func ColorByStatus(code int) string { once.Do(initColor) switch { case code >= 200 && code < 300: @@ -202,7 +202,7 @@ func ColorByStatus(cond bool, code int) string { // PATCH return Green // HEAD return Magenta // OPTIONS return WHITE -func ColorByMethod(cond bool, method string) string { +func ColorByMethod(method string) string { once.Do(initColor) if c := colorMap[method]; c != "" { return c @@ -210,6 +210,10 @@ func ColorByMethod(cond bool, method string) string { return reset } +func ResetColor() string { + return reset +} + // Guard Mutex to guarantee atomic of W32Debug(string) function var mu sync.Mutex diff --git a/router.go b/router.go index c5d0cda4..946635d9 100644 --- a/router.go +++ b/router.go @@ -900,34 +900,29 @@ Admin: } if FilterMonitorFunc(r.Method, r.URL.Path, timeDur, pattern, statusCode) { + routerName := "" if runRouter != nil { - go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, runRouter.Name(), timeDur) - } else { - go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, "", timeDur) + routerName = runRouter.Name() } + go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, routerName, timeDur) } } if BConfig.RunMode == DEV && !BConfig.Log.AccessLogs { - var devInfo string - iswin := (runtime.GOOS == "windows") - statusColor := logs.ColorByStatus(iswin, statusCode) - methodColor := logs.ColorByMethod(iswin, r.Method) - resetColor := logs.ColorByMethod(iswin, "") - if findRouter { - if routerInfo != nil { - devInfo = fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s r:%s", context.Input.IP(), statusColor, statusCode, - resetColor, timeDur.String(), "match", methodColor, r.Method, resetColor, r.URL.Path, - routerInfo.pattern) - } else { - devInfo = fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s", context.Input.IP(), statusColor, statusCode, resetColor, - timeDur.String(), "match", methodColor, r.Method, resetColor, r.URL.Path) - } - } else { - devInfo = fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s", context.Input.IP(), statusColor, statusCode, resetColor, - timeDur.String(), "nomatch", methodColor, r.Method, resetColor, r.URL.Path) + match := map[bool]string{true: "match", false: "nomatch"} + devInfo := fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s", + context.Input.IP(), + logs.ColorByStatus(statusCode), statusCode, logs.ResetColor(), + timeDur.String(), + match[findRouter], + logs.ColorByMethod(r.Method), r.Method, logs.ResetColor(), + r.URL.Path) + if routerInfo != nil { + devInfo += fmt.Sprintf(" r:%s", routerInfo.pattern) } - if iswin { + + //todo one logger enough,in fact no need to separate logger into windows and others + if runtime.GOOS == "windows" { logs.W32Debug(devInfo) } else { logs.Debug(devInfo)