feat(logger): color latency (#4146)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ljz · committed by GitHub · parent 731374f · Browse files · 2 files·+54 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: lizhao <lizhao@qq.com>
Diff stats
2 files+54 added−9 removed
- other:+54 added−9 removed
Provenance
- Commit
4dec17a- Tree
f885da8- Author
ljz <641390597@qq.com>- Committer
GitHub <noreply@github.com>- Parent
731374f
2 files changed+54 added−9 removed
modified:logger_test.go+21 added-4 removed
275 unmodified lines276 ErrorMessage: "",277 isTerm: false,278 }279 280- assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 5s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseParam))281- assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 2743h29m3s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseLongDurationParam))280+ assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 5s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseParam))281+ assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 2743h29m0s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseLongDurationParam))282 283- assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 5s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueParam))284- assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 2743h29m3s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueLongDurationParam))283+ assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m|\x1b[97;41m 5s \x1b[0m| 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueParam))284+ assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m|\x1b[97;41m 2743h29m0s \x1b[0m| 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueLongDurationParam))285 }286 287 func TestColorForMethod(t *testing.T) {288 colorForMethod := func(method string) string {27 unmodified lines316 assert.Equal(t, yellow, colorForStatus(http.StatusNotFound), "4xx should be yellow")317 assert.Equal(t, red, colorForStatus(2), "other things should be red")318 }319 320+func TestColorForLatency(t *testing.T) {321+ colorForLantency := func(latency time.Duration) string {322+ p := LogFormatterParams{323+ Latency: latency,324+ }325+ return p.LatencyColor()326+ }327+328+ assert.Equal(t, white, colorForLantency(time.Duration(0)), "0 should be white")329+ assert.Equal(t, white, colorForLantency(time.Millisecond*20), "20ms should be white")330+ assert.Equal(t, green, colorForLantency(time.Millisecond*150), "150ms should be green")331+ assert.Equal(t, cyan, colorForLantency(time.Millisecond*250), "250ms should be cyan")332+ assert.Equal(t, yellow, colorForLantency(time.Millisecond*600), "600ms should be yellow")333+ assert.Equal(t, magenta, colorForLantency(time.Millisecond*1500), "1.5s should be magenta")334+ assert.Equal(t, red, colorForLantency(time.Second*3), "other things should be red")335+}336+337 func TestResetColor(t *testing.T) {338 p := LogFormatterParams{}339 assert.Equal(t, string([]byte{27, 91, 48, 109}), p.ResetColor())340 }133 unmodified linesmodified:logger.go+33 added-5 removed
101 unmodified lines102 return red103 }104 }105 106+// LatencyColor is the ANSI color for latency107+func (p *LogFormatterParams) LatencyColor() string {108+ latency := p.Latency109+ switch {110+ case latency < time.Millisecond*100:111+ return white112+ case latency < time.Millisecond*200:113+ return green114+ case latency < time.Millisecond*300:115+ return cyan116+ case latency < time.Millisecond*500:117+ return blue118+ case latency < time.Second:119+ return yellow120+ case latency < time.Second*2:121+ return magenta122+ default:123+ return red124+ }125+}126+127 // MethodColor is the ANSI color for appropriately logging http method to a terminal.128 func (p *LogFormatterParams) MethodColor() string {129 method := p.Method130 28 unmodified lines159 }160 161 // defaultLogFormatter is the default log format function Logger middleware uses.162 var defaultLogFormatter = func(param LogFormatterParams) string {142- var statusColor, methodColor, resetColor string163+ var statusColor, methodColor, resetColor, latencyColor string164 if param.IsOutputColor() {165 statusColor = param.StatusCodeColor()166 methodColor = param.MethodColor()167 resetColor = param.ResetColor()168+ latencyColor = param.LatencyColor()169 }170 149- if param.Latency > time.Minute {150- param.Latency = param.Latency.Truncate(time.Second)171+ switch {172+ case param.Latency > time.Minute:173+ param.Latency = param.Latency.Truncate(time.Second * 10)174+ case param.Latency > time.Second:175+ param.Latency = param.Latency.Truncate(time.Millisecond * 10)176+ case param.Latency > time.Millisecond:177+ param.Latency = param.Latency.Truncate(time.Microsecond * 10)178 }179+152- return fmt.Sprintf("[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",180+ return fmt.Sprintf("[GIN] %v |%s %3d %s|%s %8v %s| %15s |%s %-7s %s %#v\n%s",181 param.TimeStamp.Format("2006/01/02 - 15:04:05"),182 statusColor, param.StatusCode, resetColor,155- param.Latency,183+ latencyColor, param.Latency, resetColor,184 param.ClientIP,185 methodColor, param.Method, resetColor,186 param.Path,187 param.ErrorMessage,122 unmodified lines