From d9c2ad1f712178f85fd81a40fa02f5a19ae2463c Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Fri, 24 Apr 2026 07:35:32 +0300 Subject: [PATCH] feat: Add structured JSON logging to PerformanceMiddleware slow request handler with comprehensive request metadata - Replace fmt.Appendf with json.Marshal for structured log output - Add logData map with level/type/method/path/query/response_time/status/user_agent/ip/timestamp fields - Update logSlowRequest to output JSON-formatted logs with newline terminator for better log parsing and monitoring integration --- .../internal/middleware/performance.go | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/apps/api-server/internal/middleware/performance.go b/apps/api-server/internal/middleware/performance.go index 236671b..369563c 100644 --- a/apps/api-server/internal/middleware/performance.go +++ b/apps/api-server/internal/middleware/performance.go @@ -2,7 +2,7 @@ package middleware import ( "context" - "fmt" + "encoding/json" "net/http" "sync" "time" @@ -80,15 +80,23 @@ func (pm *PerformanceMiddleware) updateMetrics(statusCode int, responseTime time // logSlowRequest logs slow requests for monitoring func (pm *PerformanceMiddleware) logSlowRequest(c *gin.Context, responseTime time.Duration) { - // In production, this would go to a structured logging system - // For now, we'll use the standard logger - gin.DefaultWriter.Write(fmt.Appendf(nil, - "[SLOW REQUEST] %s %s - %v - Status: %d\n", - c.Request.Method, - c.Request.URL.Path, - responseTime, - c.Writer.Status(), - )) + // Structured logging for slow requests + logData := map[string]any{ + "level": "warn", + "type": "slow_request", + "method": c.Request.Method, + "path": c.Request.URL.Path, + "query": c.Request.URL.RawQuery, + "response_time": responseTime.String(), + "status": c.Writer.Status(), + "user_agent": c.Request.UserAgent(), + "ip": c.ClientIP(), + "timestamp": time.Now().UTC().Format(time.RFC3339), + } + + // Convert to JSON for structured logging + logJSON, _ := json.Marshal(logData) + gin.DefaultWriter.Write(append(logJSON, '\n')) } // GetMetrics returns current performance metrics