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:
AskaEth committed 2026-09-11 01:59:58 +08:00
1 parent 8e707e1e9b
commit e5e0944d26
32 files changed
+2017 -138

No files matched your search

+24 -1
View File
@@ -5,9 +5,12 @@ import (
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
"git.yeij.top/AskaEth/Scoreboard/server/internal/logging"
)
// Config 是 config.yaml 的映射。
@@ -37,6 +40,13 @@ type Server struct {
// Log 是日志配置。
type Log struct {
Level string `yaml:"level"`
// Dir 是日志根目录,其下分 operations/(放行类)与 interceptions/(拦截类);
// 留空时用 <dataDir>/logs。
Dir string `yaml:"dir"`
// MaxSizeMB 是单个日志文件的大小上限(MB);<=0 用 logging.DefaultMaxSizeMB。
MaxSizeMB int `yaml:"maxSizeMB"`
// KeepFiles 是每个类别保留的份数(含当前文件);<=0 用 logging.DefaultKeepFiles。
KeepFiles int `yaml:"keepFiles"`
}
// Default 返回一份可直接使用的默认配置。
@@ -45,10 +55,23 @@ func Default() *Config {
Server: Server{Host: "0.0.0.0", Port: 19540},
DataDir: "data",
Backup: Backup{IntervalMinutes: 5, Keep: 20},
Log: Log{Level: "info"},
Log: Log{
Level: "info",
MaxSizeMB: logging.DefaultMaxSizeMB,
KeepFiles: logging.DefaultKeepFiles,
},
}
}
// DirPath 返回日志根目录:配了 log.dir 就用它,否则落在数据目录下的 logs/。
// 相对路径保持相对(与 dataDir 的处理一致),现场怎么填就怎么用。
func (l Log) DirPath(dataDir string) string {
if dir := strings.TrimSpace(l.Dir); dir != "" {
return dir
}
return filepath.Join(dataDir, "logs")
}
// Load 读取配置文件;文件不存在时返回默认配置。
func Load(path string) (*Config, error) {
cfg := Default()
+30
View File
@@ -63,3 +63,33 @@ func TestApplyDataDir(t *testing.T) {
})
}
}
// 日志配置:默认值开箱即用,配了 log.dir 就按它走,不配就落在 <dataDir>/logs。
func TestLogConfigDefaultsAndDirPath(t *testing.T) {
def := Default().Log
if def.Level != "info" {
t.Fatalf("默认级别应为 info,得到 %q", def.Level)
}
if def.MaxSizeMB <= 0 || def.KeepFiles <= 0 {
t.Fatalf("大小上限与保留份数都要有默认值,得到 %+v", def)
}
if got := def.DirPath("/data/sb"); got != filepath.Join("/data/sb", "logs") {
t.Fatalf("未配 dir 时应落在数据目录下的 logs,得到 %q", got)
}
path := filepath.Join(t.TempDir(), "config.yaml")
raw := "log:\n level: warn\n dir: /var/log/scoreboard\n maxSizeMB: 5\n keepFiles: 3\n"
if err := os.WriteFile(path, []byte(raw), 0o644); err != nil {
t.Fatalf("写入配置: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("加载配置: %v", err)
}
if cfg.Log.Level != "warn" || cfg.Log.MaxSizeMB != 5 || cfg.Log.KeepFiles != 3 {
t.Fatalf("log 段解析错误: %+v", cfg.Log)
}
if got := cfg.Log.DirPath("/data/sb"); got != "/var/log/scoreboard" {
t.Fatalf("配了 dir 就该用它,得到 %q", got)
}
}