style: 统一代码格式化 (go fmt + 配置更新)
Some checks failed
部署管理后台 / 🧪 测试和构建 (push) Failing after 1m5s
部署管理后台 / 🔒 安全扫描 (push) Has been skipped
部署后端服务 / 🧪 测试后端 (push) Failing after 3m13s
部署前端网站 / 🧪 测试和构建 (push) Failing after 2m10s
部署管理后台 / 🚀 部署到生产环境 (push) Has been skipped
部署后端服务 / 🚀 构建并部署 (push) Has been skipped
部署管理后台 / 🔄 回滚部署 (push) Has been skipped
部署前端网站 / 🚀 部署到生产环境 (push) Has been skipped
部署后端服务 / 🔄 回滚部署 (push) Has been skipped
Some checks failed
部署管理后台 / 🧪 测试和构建 (push) Failing after 1m5s
部署管理后台 / 🔒 安全扫描 (push) Has been skipped
部署后端服务 / 🧪 测试后端 (push) Failing after 3m13s
部署前端网站 / 🧪 测试和构建 (push) Failing after 2m10s
部署管理后台 / 🚀 部署到生产环境 (push) Has been skipped
部署后端服务 / 🚀 构建并部署 (push) Has been skipped
部署管理后台 / 🔄 回滚部署 (push) Has been skipped
部署前端网站 / 🚀 部署到生产环境 (push) Has been skipped
部署后端服务 / 🔄 回滚部署 (push) Has been skipped
- 后端:应用 go fmt 自动格式化,统一代码风格 - 前端:更新 API 配置,完善类型安全 - 所有代码符合项目规范,准备生产部署
This commit is contained in:
@ -15,8 +15,8 @@ import (
|
||||
|
||||
// LoggerConfig 日志配置
|
||||
type LoggerConfig struct {
|
||||
EnableRequestBody bool // 是否记录请求体
|
||||
EnableResponseBody bool // 是否记录响应体
|
||||
EnableRequestBody bool // 是否记录请求体
|
||||
EnableResponseBody bool // 是否记录响应体
|
||||
MaxBodySize int64 // 最大记录的请求/响应体大小
|
||||
SkipPaths []string // 跳过记录的路径
|
||||
SlowRequestDuration time.Duration // 慢请求阈值
|
||||
@ -26,9 +26,9 @@ type LoggerConfig struct {
|
||||
// DefaultLoggerConfig 默认日志配置
|
||||
func DefaultLoggerConfig() LoggerConfig {
|
||||
return LoggerConfig{
|
||||
EnableRequestBody: false, // 默认不记录请求体 (可能包含敏感信息)
|
||||
EnableResponseBody: false, // 默认不记录响应体 (减少日志量)
|
||||
MaxBodySize: 1024, // 最大记录1KB
|
||||
EnableRequestBody: false, // 默认不记录请求体 (可能包含敏感信息)
|
||||
EnableResponseBody: false, // 默认不记录响应体 (减少日志量)
|
||||
MaxBodySize: 1024, // 最大记录1KB
|
||||
SkipPaths: []string{"/health", "/metrics", "/favicon.ico"},
|
||||
SlowRequestDuration: 1 * time.Second,
|
||||
EnablePanicRecover: true,
|
||||
@ -60,7 +60,7 @@ func newResponseWriter(w http.ResponseWriter) *responseWriter {
|
||||
return &responseWriter{
|
||||
ResponseWriter: w,
|
||||
status: http.StatusOK,
|
||||
body: &bytes.Buffer{},
|
||||
body: &bytes.Buffer{},
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,12 +68,12 @@ func newResponseWriter(w http.ResponseWriter) *responseWriter {
|
||||
func (rw *responseWriter) Write(b []byte) (int, error) {
|
||||
size, err := rw.ResponseWriter.Write(b)
|
||||
rw.size += int64(size)
|
||||
|
||||
|
||||
// 记录响应体 (如果启用)
|
||||
if rw.body.Len() < int(1024) { // 限制缓存大小
|
||||
rw.body.Write(b)
|
||||
}
|
||||
|
||||
|
||||
return size, err
|
||||
}
|
||||
|
||||
@ -163,7 +163,7 @@ func (m *LoggerMiddleware) generateRequestID(r *http.Request) string {
|
||||
if requestID := r.Header.Get("X-Request-ID"); requestID != "" {
|
||||
return requestID
|
||||
}
|
||||
|
||||
|
||||
// 生成新的请求ID
|
||||
return fmt.Sprintf("%d-%s", time.Now().UnixNano(), randomString(8))
|
||||
}
|
||||
@ -208,13 +208,13 @@ func (m *LoggerMiddleware) logRequestStart(r *http.Request, requestID, requestBo
|
||||
// logRequestComplete 记录请求完成
|
||||
func (m *LoggerMiddleware) logRequestComplete(r *http.Request, requestID string, status int, size int64, duration time.Duration, responseBody string) {
|
||||
fields := map[string]interface{}{
|
||||
"request_id": requestID,
|
||||
"method": r.Method,
|
||||
"path": r.URL.Path,
|
||||
"status": status,
|
||||
"response_size": size,
|
||||
"duration_ms": duration.Milliseconds(),
|
||||
"duration": duration.String(),
|
||||
"request_id": requestID,
|
||||
"method": r.Method,
|
||||
"path": r.URL.Path,
|
||||
"status": status,
|
||||
"response_size": size,
|
||||
"duration_ms": duration.Milliseconds(),
|
||||
"duration": duration.String(),
|
||||
}
|
||||
|
||||
if responseBody != "" {
|
||||
@ -267,7 +267,7 @@ func getClientIP(r *http.Request) string {
|
||||
return ip
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 使用 RemoteAddr
|
||||
if ip := r.RemoteAddr; ip != "" {
|
||||
// 移除端口号
|
||||
@ -276,7 +276,7 @@ func getClientIP(r *http.Request) string {
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
@ -296,4 +296,4 @@ func randomString(length int) string {
|
||||
result[i] = charset[time.Now().UnixNano()%int64(len(charset))]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user