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
This commit is contained in:
nutcas3
2026-04-24 07:35:32 +03:00
parent 3b338b0e20
commit d9c2ad1f71
@@ -2,7 +2,7 @@ package middleware
import ( import (
"context" "context"
"fmt" "encoding/json"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@@ -80,15 +80,23 @@ func (pm *PerformanceMiddleware) updateMetrics(statusCode int, responseTime time
// logSlowRequest logs slow requests for monitoring // logSlowRequest logs slow requests for monitoring
func (pm *PerformanceMiddleware) logSlowRequest(c *gin.Context, responseTime time.Duration) { func (pm *PerformanceMiddleware) logSlowRequest(c *gin.Context, responseTime time.Duration) {
// In production, this would go to a structured logging system // Structured logging for slow requests
// For now, we'll use the standard logger logData := map[string]any{
gin.DefaultWriter.Write(fmt.Appendf(nil, "level": "warn",
"[SLOW REQUEST] %s %s - %v - Status: %d\n", "type": "slow_request",
c.Request.Method, "method": c.Request.Method,
c.Request.URL.Path, "path": c.Request.URL.Path,
responseTime, "query": c.Request.URL.RawQuery,
c.Writer.Status(), "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 // GetMetrics returns current performance metrics