From 2bd743fcffbd80f1584f1fea87ac931ff08dfe52 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Fri, 17 Jun 2016 15:39:16 +0200 Subject: [PATCH] Enhanced logging during DEV mode --- logs/logger.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ router.go | 21 +++++++++++++++------ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/logs/logger.go b/logs/logger.go index 2f47e569..aff7e992 100644 --- a/logs/logger.go +++ b/logs/logger.go @@ -18,6 +18,7 @@ import ( "io" "sync" "time" + "net/http" ) type logWriter struct { @@ -80,3 +81,48 @@ func formatTimeHeader(when time.Time) ([]byte, int) { return buf[0:], d } + +var ( + green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109}) + white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109}) + yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109}) + red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109}) + blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109}) + magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109}) + cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109}) + reset = string([]byte{27, 91, 48, 109}) +) + +func ColorByStatus(code int) string { + switch { + case code >= 200 && code < 300: + return green + case code >= 300 && code < 400: + return white + case code >= 400 && code < 500: + return yellow + default: + return red + } +} + +func ColorByMethod(method string) string { + switch method { + case http.MethodGet: + return blue + case http.MethodPost: + return cyan + case http.MethodPut: + return yellow + case http.MethodDelete: + return red + case http.MethodPatch: + return green + case http.MethodHead: + return magenta + case http.MethodOptions: + return white + default: + return reset + } +} diff --git a/router.go b/router.go index 960cd104..07abad2c 100644 --- a/router.go +++ b/router.go @@ -816,18 +816,27 @@ Admin: if BConfig.RunMode == DEV || BConfig.Log.AccessLogs { timeDur := time.Since(startTime) var devInfo string + + statusCode := context.ResponseWriter.Status + if statusCode == 0 { statusCode = 200 } + + statusColor := logs.ColorByStatus(statusCode) + methodColor := logs.ColorByMethod(r.Method) + resetColor := logs.ColorByMethod("") + if findRouter { if routerInfo != nil { - devInfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s | % -40s |", r.Method, r.URL.Path, timeDur.String(), "match", routerInfo.pattern) + devInfo = fmt.Sprintf("|%s %3d %s|%7s|%8s|%s %s %-7s %-3s r:%s", statusColor, statusCode, resetColor, + timeDur.String(), "match", methodColor, resetColor, r.Method, r.URL.Path, routerInfo.pattern) } else { - devInfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s |", r.Method, r.URL.Path, timeDur.String(), "match") + devInfo = fmt.Sprintf("|%s %3d %s|%7s|%8s|%s %s %-7s %-3s", statusColor, statusCode, resetColor, + timeDur.String(), "match", methodColor, resetColor, r.Method, r.URL.Path) } } else { - devInfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s |", r.Method, r.URL.Path, timeDur.String(), "notmatch") - } - if DefaultAccessLogFilter == nil || !DefaultAccessLogFilter.Filter(context) { - logs.Debug(devInfo) + devInfo = fmt.Sprintf("|%s %3d %s|%7s|%8s|%s %s %-7s %-3s", statusColor, statusCode, resetColor, + timeDur.String(), "nomatch", methodColor, resetColor, r.Method, r.URL.Path) } + logs.Debug(devInfo) } // Call WriteHeader if status code has been set changed