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 (
"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