From 6634f04d9b4603d96cb665c4772d2cdec7013d9e Mon Sep 17 00:00:00 2001 From: Damon Zhao Date: Fri, 5 Sep 2014 15:53:53 +0800 Subject: [PATCH 1/2] (feature)add http method log-color,like http response status code --- logger.go | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/logger.go b/logger.go index 56602c0..878ce19 100644 --- a/logger.go +++ b/logger.go @@ -27,11 +27,14 @@ func ErrorLoggerT(typ uint32) HandlerFunc { } 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}) - reset = string([]byte{27, 91, 48, 109}) + 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 Logger() HandlerFunc { @@ -68,14 +71,34 @@ func Logger() HandlerFunc { default: color = red } + + var methodColor string + method := c.Request.Method + switch { + case method == "GET": + methodColor = blue + case method == "POST": + methodColor = cyan + case method == "PUT": + methodColor = yellow + case method == "DELETE": + methodColor = red + case method == "PATCH": + methodColor = green + case method == "HEAD": + methodColor = magenta + case method == "OPTIONS": + methodColor = white + } end := time.Now() latency := end.Sub(start) - stdlogger.Printf("[GIN] %v |%s %3d %s| %12v | %s %4s %s\n%s", + stdlogger.Printf("[GIN] %v |%s %3d %s| %12v | %s |%s %4s %s| %s\n%s", end.Format("2006/01/02 - 15:04:05"), color, code, reset, latency, requester, - c.Request.Method, c.Request.URL.Path, + methodColor, method, reset, + c.Request.URL.Path, c.Errors.String(), ) } From 953c589b3236df613a769d52cbf6e0b3e1cc3982 Mon Sep 17 00:00:00 2001 From: Damon Zhao Date: Sat, 13 Sep 2014 12:40:37 +0800 Subject: [PATCH 2/2] change %4s to %-7s for align --- logger.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logger.go b/logger.go index 878ce19..4a95f66 100644 --- a/logger.go +++ b/logger.go @@ -92,7 +92,7 @@ func Logger() HandlerFunc { } end := time.Now() latency := end.Sub(start) - stdlogger.Printf("[GIN] %v |%s %3d %s| %12v | %s |%s %4s %s| %s\n%s", + stdlogger.Printf("[GIN] %v |%s %3d %s| %12v | %s |%s %-7s %s| %s\n%s", end.Format("2006/01/02 - 15:04:05"), color, code, reset, latency,