gin icon indicating copy to clipboard operation
gin copied to clipboard

Optimize the logger middleware

Open mstmdev opened this issue 3 years ago • 2 comments

Use the out.Write to write the log content immediately, because the LogFormatter returns a string, the fmt.Fprint is unnecessary here.

mstmdev avatar Jan 06 '22 16:01 mstmdev

The benchmark test result:

Command:

go test -test.v -test.bench ^BenchmarkLoggerMiddleware$

Environment:

version: go1.18.3
goos: windows
goarch: amd64
pkg: github.com/gin-gonic/gin
cpu: Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz

Result:

BenchmarkLoggerMiddleware(New Pull Request)
BenchmarkLoggerMiddleware-16             1244007               951.5 ns/op           270 B/op         14 allocs/op

BenchmarkLoggerMiddleware(Old Master)
BenchmarkLoggerMiddleware-16             1000000              1036 ns/op             288 B/op         16 allocs/op

mstmdev avatar Mar 25 '22 08:03 mstmdev

Codecov Report

Merging #3016 (8dbee16) into master (92dd245) will increase coverage by 0.00%. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #3016   +/-   ##
=======================================
  Coverage   98.06%   98.06%           
=======================================
  Files          43       43           
  Lines        3148     3150    +2     
=======================================
+ Hits         3087     3089    +2     
  Misses         48       48           
  Partials       13       13           
Flag Coverage Δ
go-1.15 98.06% <100.00%> (+<0.01%) :arrow_up:
go-1.16 98.03% <100.00%> (+<0.01%) :arrow_up:
go-1.17 97.96% <100.00%> (+<0.01%) :arrow_up:
go-1.18 97.96% <100.00%> (+<0.01%) :arrow_up:
macos-latest 98.06% <100.00%> (+<0.01%) :arrow_up:
nomsgpack 98.03% <100.00%> (+<0.01%) :arrow_up:
ubuntu-latest 98.06% <100.00%> (+<0.01%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
logger.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 92dd245...8dbee16. Read the comment docs.

codecov[bot] avatar Mar 25 '22 09:03 codecov[bot]