Skip to content

Conversation

@m90
Copy link

@m90 m90 commented Mar 12, 2018

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
Copy link

codecov bot commented Mar 12, 2018

Codecov Report

Merging #1282 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          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%> (ø) ⬆️

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 5d3f30c...41a00d2. Read the comment docs.

thinkerou
thinkerou previously approved these changes Sep 7, 2018
@thinkerou thinkerou added the type/bug Found something you weren't expecting? Report it here! label Sep 7, 2018
@thinkerou thinkerou added this to the 1.4 milestone Sep 7, 2018
@thinkerou thinkerou self-assigned this Sep 7, 2018

fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %s\n%s",
fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %39s |%s %-7s %s %s\n%s",
end.Format("2006/01/02 - 15:04:05"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the client ip length first. Please try the following example.

width := 0
if len(clientIP) > 15 {
  width = 39
} else {
  width = 15
}

fmt.Printf("%*s\n", width, "Hello World!")

see https://play.golang.org/p/pLKiETJ-NX

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

width := 15
if len(clientIP) > 15 {
  width = 39
}

@m90
Copy link
Author

m90 commented Sep 7, 2018

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.

@thinkerou
Copy link
Member

thinkerou commented Sep 7, 2018

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.

@theonlypwner
Copy link

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.

@appleboy
Copy link
Member

appleboy commented Mar 16, 2020

move to 1.7 milestones.

@appleboy appleboy modified the milestones: 1.6, 1.7 Mar 16, 2020
@thinkerou thinkerou modified the milestones: 1.7, v1.8 Oct 21, 2020
@thinkerou thinkerou modified the milestones: v1.8, v1.9 Nov 21, 2021
@thinkerou thinkerou modified the milestones: v1.9, v1.10 Jan 17, 2023
@appleboy appleboy modified the milestones: v1.10, v1.11 Mar 21, 2024
@appleboy appleboy removed this from the v1.11 milestone Jun 18, 2025
@appleboy appleboy added this to the v1.12 milestone Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/bug Found something you weren't expecting? Report it here!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IP Addresses misalign log lines

4 participants