feat: 分级拦截日志 + 两类日志文件;成绩录入防呆/解析/宽度修复
## 日志(用户要求:分级 + 每个拦截都要有指出代码行的调试日志 + 两类文件)
- 新增 internal/logging:拦截点捕获(file:line + 函数名,相对路径)、两类文件轮转、
按级别/类别分流。httpapi 与 middleware 各自薄封装,无循环依赖。
- 两类目录(数据目录下):
operations/ 放行类(INFO「操作已放行」+ 常规运行日志,按 log.level)
interceptions/ 拦截类(WARN 中文原因 + 拦截点 + method/path/actor/role/requestId、
DEBUG 上下文、ERROR 服务端异常)—— **固定 debug 采集**,
否则默认 info 下"每次拦截的调试日志"根本看不见
- 命名 operations-YYYYMMDD[.N].log,按天+按大小轮转,每类保留 keepFiles 份;写盘失败
降级只写控制台且只 Warn 一次。
- 所有业务错误统一走 newAPIError 捕获拦截点(11 处内联 &apiError{} 已收进构造函数,
防止绕过);中间件层拦截(401/403/CSRF/未改初始密码/429)也留痕,响应体与状态码一字未变。
- config.yaml 的 log 段新增 dir / maxSizeMB / keepFiles(留空即默认)。
- 服务端包数 12 → 13(新增 internal/logging)。
## 成绩录入(用户实机踩坑)
- 真因一:成绩格 placeholder 是光秃秃的 `1:06.33`,而全仓库没有任何 ::placeholder 样式,
示例文本与真实输入长得一样 → 用户把灰字当成已填内容,那 13 格其实是空的。
- 真因二:2 条报错是秒位 ≥ 60(1:70)或全角冒号(1:06);1:06 与 1:06.00 在解析器里
本来就等价(都 = 66),格式不是问题。
- 真因三:成绩格没有宽度定义(旁边名次 72px、状态 110px 都有),被挤到第 4 位数字看不见。
修复:parseSecondsDetail 区分 empty/seconds-overflow/unrecognized;新增 entryGuards.ts
纯函数(录入页与审核页共用),报错**打出那一格的实际内容**并说明原因;解析器新认全角
冒号/全角数字/1分06秒 等写法;成绩格 min-width 112px(按 fontTools 实测文本宽度定);
示例文本改 `例 1:06.33` 等并给录入表格内的 ::placeholder 加淡色斜体;新增
onBeforeRouteLeave 离开守卫(原来切菜单会静默丢未保存成绩)。
测试:服务端 13 包全绿(372 个测试函数);前端 vue-tsc 通过、205 例全过;Console 18 文件。
docs/ 与 devdocs/ 同步(FAQ 到 Q76,部署指南新增第 10 节「服务端日志与拦截复盘」)。
This commit is contained in:
1 parent
8e707e1e9b
commit
e5e0944d26
32 files changed
+2017
-138
No files matched your search
@@ -3,7 +3,6 @@ package httpapi
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -17,32 +16,78 @@ type apiError struct {
|
||||
code string
|
||||
message string
|
||||
detail any
|
||||
// origin 是「拦截点」:构造这个错误的 file:line 与函数名,例如
|
||||
// internal/httpapi/repo_events.go:231 normalizeAvgMode。拦截日志靠它定位到校验代码。
|
||||
origin string
|
||||
}
|
||||
|
||||
func (e *apiError) Error() string { return e.code + ": " + e.message }
|
||||
|
||||
// newAPIError 是所有业务错误的唯一构造入口:在这里捕获拦截点。
|
||||
// skip=3 是 callerOrigin → newAPIError → 具体构造函数 → 真正做校验的调用点,
|
||||
// 少一层就会把 helper 自己记成拦截点,那样日志等于没说。
|
||||
func newAPIError(status int, code, message string, detail any) *apiError {
|
||||
return &apiError{
|
||||
status: status,
|
||||
code: code,
|
||||
message: message,
|
||||
detail: detail,
|
||||
origin: callerOrigin(3),
|
||||
}
|
||||
}
|
||||
|
||||
func badRequest(message string, detail any) *apiError {
|
||||
return &apiError{status: http.StatusBadRequest, code: "VALIDATION_FAILED", message: message, detail: detail}
|
||||
return newAPIError(http.StatusBadRequest, "VALIDATION_FAILED", message, detail)
|
||||
}
|
||||
|
||||
func notFound(message string) *apiError {
|
||||
return &apiError{status: http.StatusNotFound, code: "VALIDATION_FAILED", message: message}
|
||||
return newAPIError(http.StatusNotFound, "VALIDATION_FAILED", message, nil)
|
||||
}
|
||||
|
||||
func conflict(message string) *apiError {
|
||||
return &apiError{status: http.StatusConflict, code: "CONFLICT", message: message}
|
||||
return newAPIError(http.StatusConflict, "CONFLICT", message, nil)
|
||||
}
|
||||
|
||||
func stateInvalid(message string) *apiError {
|
||||
return &apiError{status: http.StatusConflict, code: "STATE_INVALID", message: message}
|
||||
return newAPIError(http.StatusConflict, "STATE_INVALID", message, nil)
|
||||
}
|
||||
|
||||
// unauthorized 是 401:code 区分「没登录」与「凭据不对」。
|
||||
func unauthorized(code, message string) *apiError {
|
||||
return newAPIError(http.StatusUnauthorized, code, message, nil)
|
||||
}
|
||||
|
||||
// forbidden 是 403:角色不足或来源校验不过。
|
||||
func forbidden(message string) *apiError {
|
||||
return newAPIError(http.StatusForbidden, "FORBIDDEN", message, nil)
|
||||
}
|
||||
|
||||
// rateLimited 是 429:写操作/登录太频繁,稍后可重试。
|
||||
func rateLimited(message string) *apiError {
|
||||
return newAPIError(http.StatusTooManyRequests, "RATE_LIMITED", message, nil)
|
||||
}
|
||||
|
||||
// tooLarge 是 413:正文超过上限,与「格式不对」区分开,前端才知道该怎么处理。
|
||||
func tooLarge(message string) *apiError {
|
||||
return newAPIError(http.StatusRequestEntityTooLarge, "VALIDATION_FAILED", message, nil)
|
||||
}
|
||||
|
||||
// serviceUnavailable 是 503:依赖未接入或目录未配置,配置好即可用,重试无用。
|
||||
func serviceUnavailable(code, message string) *apiError {
|
||||
return newAPIError(http.StatusServiceUnavailable, code, message, nil)
|
||||
}
|
||||
|
||||
func internal(err error) *apiError {
|
||||
return &apiError{status: http.StatusInternalServerError, code: "INTERNAL", message: "服务端异常", detail: err.Error()}
|
||||
return newAPIError(http.StatusInternalServerError, "INTERNAL", "服务端异常", err.Error())
|
||||
}
|
||||
|
||||
// internalMsg 是 internal 的可自定义文案版本:5xx 的 message 也要让现场看懂发生了什么。
|
||||
func internalMsg(message string, err error) *apiError {
|
||||
return newAPIError(http.StatusInternalServerError, "INTERNAL", message, err.Error())
|
||||
}
|
||||
|
||||
func notImplemented(message string) *apiError {
|
||||
return &apiError{status: http.StatusNotImplemented, code: "INTERNAL", message: message}
|
||||
return newAPIError(http.StatusNotImplemented, "INTERNAL", message, nil)
|
||||
}
|
||||
|
||||
// handler 是业务处理器:返回 data 时自动包成 {ok:true,data},返回 *apiError 时输出统一错误。
|
||||
@@ -56,7 +101,7 @@ func (s *server) handle(fn handler) http.HandlerFunc {
|
||||
if errors.Is(err, errHandled) {
|
||||
return
|
||||
}
|
||||
writeAPIError(w, s.logger, err)
|
||||
s.writeAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "data": data})
|
||||
@@ -67,21 +112,27 @@ func (s *server) handle(fn handler) http.HandlerFunc {
|
||||
func (s *server) handleVoid(fn func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := fn(w, r); err != nil {
|
||||
writeAPIError(w, s.logger, err)
|
||||
s.writeAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "data": nil})
|
||||
}
|
||||
}
|
||||
|
||||
func writeAPIError(w http.ResponseWriter, logger *slog.Logger, err error) {
|
||||
// writeAPIError 把业务错误写成统一响应,并按级别记日志:
|
||||
// 4xx 是「我们主动拦下的」,WARN 给出原因与拦截点(默认 info 级别下必须可见),DEBUG 补上下文;
|
||||
// 5xx 是服务端自身异常,用 ERROR 并带上拦截点。
|
||||
func (s *server) writeAPIError(w http.ResponseWriter, r *http.Request, err error) {
|
||||
var api *apiError
|
||||
if !errors.As(err, &api) {
|
||||
api = internal(err)
|
||||
}
|
||||
if api.status >= http.StatusInternalServerError {
|
||||
logger.Error("请求失败", "code", api.code, "err", api.message)
|
||||
// 兜底:错误没经构造函数(拿不到 origin)时,沿栈找到返回它的那段业务代码。
|
||||
if api.origin == "" {
|
||||
api.origin = originFromStack(3)
|
||||
}
|
||||
logIntercepted(r, s.logger, api)
|
||||
|
||||
body := map[string]any{
|
||||
"ok": false,
|
||||
"error": map[string]any{
|
||||
|
||||
Reference in new issue
Block a user