Adjust log format to cater for ipv6 values
Should fix #1267
The length might seem drastic, but from what I understand a v6 IP might actually grow up to that length:
IPv6 addresses are represented as eight groups of four hexadecimal digits with the groups being separated by colons, for example 2001:0db8:0000:0042:0000:8a2e:0370:7334, but methods to abbreviate this full notation exist.
Codecov Report
Merging #1282 into master will not change coverage. The diff coverage is
100%.
@@ Coverage Diff @@
## master #1282 +/- ##
======================================
Coverage 98.4% 98.4%
======================================
Files 34 34
Lines 1759 1759
======================================
Hits 1731 1731
Misses 23 23
Partials 5 5
| Impacted Files | Coverage Δ | |
|---|---|---|
| logger.go | 100% <100%> (ø) |
:arrow_up: |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing dataPowered by Codecov. Last update 5d3f30c...41a00d2. Read the comment docs.
I have to admit that I already deleted my fork as this was stale for so long.
This is the patch if you want to apply it directly:
diff --git a/logger.go b/logger.go
index c679c78..a93f70e 100644
--- a/logger.go
+++ b/logger.go
@@ -103,10 +103,16 @@ func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
path = path + "?" + raw
}
- fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %s\n%s",
+ width := 15
+ if len(clientIP) > 15 {
+ width = 39
+ }
+
+ fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %*s |%s %-7s %s %s\n%s",
end.Format("2006/01/02 - 15:04:05"),
statusColor, statusCode, resetColor,
latency,
+ width,
clientIP,
methodColor, method, resetColor,
path,
but I could also just create another fork and apply the patch there, but that would need another PR.
waiting, @appleboy @m90 @kaustubhmallik maybe we not need width param, because the pull request want to solve misalign, as #1267 picture: one request is ipv4, other request is ipv6, they will not align, so we should use an fixed length not un-fixed according to ipv4 or ipv6.
IPv6 addresses may be up to 45 characters long because of IPv4-mapped IPv6 addresses.
In Linux, INET6_ADDRSTRLEN is 46 for 45 characters plus 1 NULL character.
move to 1.7 milestones.