diff --git a/logger.go b/logger.go index cf92553a..154305ca 100644 --- a/logger.go +++ b/logger.go @@ -9,7 +9,9 @@ import ( "io" "net/http" "os" + "strconv" "time" + "unicode/utf8" "github.com/mattn/go-isatty" ) @@ -182,15 +184,56 @@ var defaultLogFormatter = func(param LogFormatterParams) string { param.Latency = param.Latency.Truncate(time.Microsecond * 10) } - return fmt.Sprintf("[GIN] %v |%s %3d %s|%s %8v %s| %15s |%s %-7s %s %#v\n%s", - param.TimeStamp.Format("2006/01/02 - 15:04:05"), - statusColor, param.StatusCode, resetColor, - latencyColor, param.Latency, resetColor, - param.ClientIP, - methodColor, param.Method, resetColor, - param.Path, - param.ErrorMessage, - ) + // Built by hand instead of fmt.Sprintf to cut per-request allocations; + // the output matches "[GIN] %v |%s %3d %s|%s %8v %s| %15s |%s %-7s %s %#v\n%s". + buf := make([]byte, 0, 256) + buf = append(buf, "[GIN] "...) + buf = param.TimeStamp.AppendFormat(buf, "2006/01/02 - 15:04:05") + buf = append(buf, " |"...) + buf = append(buf, statusColor...) + buf = append(buf, ' ') + var num [20]byte + status := strconv.AppendInt(num[:0], int64(param.StatusCode), 10) + for i := len(status); i < 3; i++ { + buf = append(buf, ' ') + } + buf = append(buf, status...) + buf = append(buf, ' ') + buf = append(buf, resetColor...) + buf = append(buf, '|') + buf = append(buf, latencyColor...) + buf = append(buf, ' ') + buf = appendPadded(buf, param.Latency.String(), 8, false) + buf = append(buf, ' ') + buf = append(buf, resetColor...) + buf = append(buf, "| "...) + buf = appendPadded(buf, param.ClientIP, 15, false) + buf = append(buf, " |"...) + buf = append(buf, methodColor...) + buf = append(buf, ' ') + buf = appendPadded(buf, param.Method, 7, true) + buf = append(buf, ' ') + buf = append(buf, resetColor...) + buf = append(buf, ' ') + buf = strconv.AppendQuote(buf, param.Path) + buf = append(buf, '\n') + buf = append(buf, param.ErrorMessage...) + return string(buf) +} + +// appendPadded appends s padded with spaces to width runes, like fmt's %*s / %-*s. +func appendPadded(buf []byte, s string, width int, left bool) []byte { + pad := width - utf8.RuneCountInString(s) + if left { + buf = append(buf, s...) + } + for ; pad > 0; pad-- { + buf = append(buf, ' ') + } + if !left { + buf = append(buf, s...) + } + return buf } // DisableConsoleColor disables color output in the console. diff --git a/logger_test.go b/logger_test.go index 395d97e6..22fadf7e 100644 --- a/logger_test.go +++ b/logger_test.go @@ -284,6 +284,47 @@ func TestDefaultLogFormatter(t *testing.T) { 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)) } +func TestDefaultLogFormatterPadding(t *testing.T) { + timeStamp := time.Unix(1544173902, 0).UTC() + + // Padding counts runes ("µs"), long fields are not truncated, and the path is Go-quoted. + param := LogFormatterParams{ + TimeStamp: timeStamp, + StatusCode: 404, + Latency: 25 * time.Microsecond, + ClientIP: "::1", + Method: http.MethodOptions, + Path: "/caf\u00e9/\"q\"", + ErrorMessage: "Error #01: not found\n", + } + assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 404 | 25\u00b5s | ::1 | OPTIONS \"/caf\u00e9/\\\"q\\\"\"\nError #01: not found\n", defaultLogFormatter(param)) + + param = LogFormatterParams{ + TimeStamp: timeStamp, + StatusCode: 0, + Latency: 1500 * time.Microsecond, + ClientIP: "2001:db8:85a3::8a2e:370:7334", + Method: "PROPFIND", + Path: "/a\tb\x00", + } + assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 0 | 1.5ms | 2001:db8:85a3::8a2e:370:7334 | PROPFIND \"/a\\tb\\x00\"\n", defaultLogFormatter(param)) +} + +func BenchmarkDefaultLogFormatter(b *testing.B) { + param := LogFormatterParams{ + TimeStamp: time.Now(), + StatusCode: http.StatusOK, + Latency: 1234567 * time.Nanosecond, + ClientIP: "192.168.100.200", + Method: http.MethodGet, + Path: "/api/v1/users/12345", + } + b.ReportAllocs() + for b.Loop() { + _ = defaultLogFormatter(param) + } +} + func TestColorForMethod(t *testing.T) { colorForMethod := func(method string) string { p := LogFormatterParams{