45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.sharkk.net/Sky/Moonshark/core/logger"
|
|
)
|
|
|
|
// StatusColors for different status code ranges
|
|
const (
|
|
colorGreen = "\033[32m" // 2xx - Success
|
|
colorCyan = "\033[36m" // 3xx - Redirection
|
|
colorYellow = "\033[33m" // 4xx - Client Errors
|
|
colorRed = "\033[31m" // 5xx - Server Errors
|
|
colorReset = "\033[0m" // Reset color
|
|
)
|
|
|
|
// LogRequest logs an HTTP request with custom formatting
|
|
func LogRequest(log *logger.Logger, statusCode int, r *http.Request, duration time.Duration) {
|
|
statusColor := getStatusColor(statusCode)
|
|
|
|
// Use the logger's raw message writer to bypass the standard format
|
|
log.LogRaw("%s [%s%d%s] %s %s (%v)",
|
|
time.Now().Format(log.TimeFormat()),
|
|
statusColor, statusCode, colorReset,
|
|
r.Method, r.URL.Path, duration)
|
|
}
|
|
|
|
// getStatusColor returns the ANSI color code for a status code
|
|
func getStatusColor(code int) string {
|
|
switch {
|
|
case code >= 200 && code < 300:
|
|
return colorGreen
|
|
case code >= 300 && code < 400:
|
|
return colorCyan
|
|
case code >= 400 && code < 500:
|
|
return colorYellow
|
|
case code >= 500:
|
|
return colorRed
|
|
default:
|
|
return ""
|
|
}
|
|
}
|