- 
                Notifications
    
You must be signed in to change notification settings  - Fork 8.5k
 
Adjust log format to cater for ipv6 values #1282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
          Codecov Report
 @@          Coverage Diff           @@
##           master   #1282   +/-   ##
======================================
  Coverage    98.4%   98.4%           
======================================
  Files          34      34           
  Lines        1759    1759           
======================================
  Hits         1731    1731           
  Misses         23      23           
  Partials        5       5
 Continue to review full report at Codecov. 
  | 
    
| 
               | 
          ||
| 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"), | 
There was a problem hiding this comment.
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!")There was a problem hiding this comment.
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
}| 
           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   | 
    
| 
           IPv6 addresses may be up to 45 characters long because of IPv4-mapped IPv6 addresses. In Linux,   | 
    
| 
           move to 1.7 milestones.  | 
    
Should fix #1267
The length might seem drastic, but from what I understand a v6 IP might actually grow up to that length: