refactor: extract helpers to reduce formatHTTPRequestLog complexity
This commit is contained in:
@@ -221,27 +221,13 @@ func formatHTTPRequestLog(entry LogEvent) string {
|
|||||||
clientIP := popField(fields, "client_ip")
|
clientIP := popField(fields, "client_ip")
|
||||||
|
|
||||||
parts := []string{entry.TS, formatLogLevel(entry.Level), "http"}
|
parts := []string{entry.TS, formatLogLevel(entry.Level), "http"}
|
||||||
if status != "" {
|
appendNonEmpty(&parts, status)
|
||||||
parts = append(parts, status)
|
appendNonEmpty(&parts, strings.TrimSpace(method+" "+path))
|
||||||
}
|
appendNonEmpty(&parts, duration)
|
||||||
if method != "" || path != "" {
|
appendNonEmpty(&parts, bytes)
|
||||||
parts = append(parts, strings.TrimSpace(method+" "+path))
|
appendKeyValue(&parts, "route", route)
|
||||||
}
|
appendKeyValueQuoted(&parts, "query", query)
|
||||||
if duration != "" {
|
appendClientIP(&parts, clientIP)
|
||||||
parts = append(parts, duration)
|
|
||||||
}
|
|
||||||
if bytes != "" {
|
|
||||||
parts = append(parts, bytes)
|
|
||||||
}
|
|
||||||
if route != "" {
|
|
||||||
parts = append(parts, "route="+route)
|
|
||||||
}
|
|
||||||
if query != "" {
|
|
||||||
parts = append(parts, "query="+quoteIfNeeded(query))
|
|
||||||
}
|
|
||||||
if clientIP != "" && !isLocalClientIP(clientIP) {
|
|
||||||
parts = append(parts, "ip="+clientIP)
|
|
||||||
}
|
|
||||||
appendSortedFields(&parts, fields)
|
appendSortedFields(&parts, fields)
|
||||||
|
|
||||||
if entry.Error != "" {
|
if entry.Error != "" {
|
||||||
@@ -251,6 +237,38 @@ func formatHTTPRequestLog(entry LogEvent) string {
|
|||||||
return strings.Join(parts, " ")
|
return strings.Join(parts, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appendNonEmpty(parts *[]string, value string) {
|
||||||
|
if value == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
*parts = append(*parts, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendKeyValue(parts *[]string, key string, value string) {
|
||||||
|
if value == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
*parts = append(*parts, key+"="+value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendKeyValueQuoted(parts *[]string, key string, value string) {
|
||||||
|
if value == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
*parts = append(*parts, key+"="+quoteIfNeeded(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendClientIP(parts *[]string, clientIP string) {
|
||||||
|
if clientIP == "" || isLocalClientIP(clientIP) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
*parts = append(*parts, "ip="+clientIP)
|
||||||
|
}
|
||||||
|
|
||||||
func appendSortedFields(parts *[]string, fields map[string]any) {
|
func appendSortedFields(parts *[]string, fields map[string]any) {
|
||||||
if len(fields) == 0 {
|
if len(fields) == 0 {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user