mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Correct the access log format (#24085)
The default access log format has been unnecessarily escaped, leading to spurious backslashes appearing in log lines. Additionally, the `RemoteAddr` field includes the port, which breaks most log parsers attempting to process it. I've added a call to `net.SplitHostPort()` attempting to isolate the address alone, with a fallback to the original address if it errs. Signed-off-by: Gary Moon <gary@garymoon.net>
This commit is contained in:
		@@ -7,6 +7,7 @@ import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"text/template"
 | 
			
		||||
@@ -67,17 +68,23 @@ func AccessLogger() func(http.Handler) http.Handler {
 | 
			
		||||
				requestID = parseRequestIDFromRequestHeader(req)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			reqHost, _, err := net.SplitHostPort(req.RemoteAddr)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				reqHost = req.RemoteAddr
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			next.ServeHTTP(w, r)
 | 
			
		||||
			rw := w.(ResponseWriter)
 | 
			
		||||
 | 
			
		||||
			buf := bytes.NewBuffer([]byte{})
 | 
			
		||||
			err := logTemplate.Execute(buf, routerLoggerOptions{
 | 
			
		||||
			err = logTemplate.Execute(buf, routerLoggerOptions{
 | 
			
		||||
				req:            req,
 | 
			
		||||
				Identity:       &identity,
 | 
			
		||||
				Start:          &start,
 | 
			
		||||
				ResponseWriter: rw,
 | 
			
		||||
				Ctx: map[string]interface{}{
 | 
			
		||||
					"RemoteAddr": req.RemoteAddr,
 | 
			
		||||
					"RemoteHost": reqHost,
 | 
			
		||||
					"Req":        req,
 | 
			
		||||
				},
 | 
			
		||||
				RequestID: &requestID,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user