Skip to content

fix(serve): standard Apache Combined Log Format for HTTP access log (fix #126) - #128

Merged
c2j merged 1 commit into
c2j:mainfrom
Climentine:fix/issue-126-access-log-clf
Aug 3, 2026
Merged

c2j merged 1 commit into
c2j:mainfrom
Climentine:fix/issue-126-access-log-clf

Conversation

@Climentine

Copy link
Copy Markdown
Contributor

#126 serve: HTTP 访问日志格式不标准

改动

  1. src/server/access_log.rs — 访问日志改为标准 Apache Combined Log Format:

    %h - - [%t] "%r" %s %b "%{Referer}i" "%{User-agent}i" <latency>ms
    

    实际输出示例:

    127.0.0.1 - - [31/Jul/2026:04:16:31 +0800] "GET /api/v1/nodes/search-sql?q=dat_trd_equity HTTP/1.1" 200 - "-" "-" 16070ms
    
    • %t 使用 CLF [dd/MMM/yyyy:HH:mm:ss +zzzz],含正确时区偏移(替代原先无时区、手写 UTC 时钟)
    • %r 渲染真实 HTTP 版本(HTTP/1.1 / HTTP/2 …),不再硬编码 HTTP/1.1
    • 捕获并输出 Referer / User-Agent(缺省为 -)
    • 静态资源(js/css/img/fonts/favicon)继续过滤
    • 依赖新增 chrono(仅 serve feature,optional)
  2. src/server/handlers.rs — 修复 axum 层顺序 bug:.fallback(serve_asset) 之前加在 .layer(middleware) 之后,导致 404(含 SPA/asset 兜底)请求完全绕过访问日志中间件。把 .fallback() 移到 .layer() 之前后,所有请求都会经过日志。

  3. tests/serve_api.rs — 新增回归断言:404(fallback 处理)请求必须出现在 http.log

验证

检查 结果
cargo build --features serve / --features full
cargo fmt -- --check
cargo clippy --features serve(涉及文件) ✅ 零告警
access_log 单测 ✅ 6/6
cargo test --features serve --test serve_api ✅ 8/8(含新增 404 断言)
cargo test --features full 全量 ✅ 除 1 个既有且无关的 Windows 路径分隔符失败(import::parser::tests::test_path_mapping_applied,本改动未触碰该文件)
实机自测 ✅ 日志行与 #126 示例逐字段一致

说明

  • 末尾 <latency>msserve: HTTP 访问日志格式不标准 #126 要求保留的扩展;GoAccess / AWStats / Logstash 默认正则需一行自定义配置(字段提取类工具如 Apache/Splunk/ELK Search 开箱可用)。
  • 本 PR 未包含工作区中无关的 AGENTS.md 改动。

@c2j c2j left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM — 建议先合并此 PR

验证结果

检查项 结果
cargo check --features serve
cargo fmt -- --check
cargo clippy --features serve ✅ (无新增告警)
access_log 单测 ✅ 6/6
集成测试 serve_api ✅ 8/8

代码评审

Apache CLF 格式正确性: ✅

%h %l %u [%t] "%r" %>s %b "%{Referer}i" "%{User-agent}i" + 自定义 <latency>ms

验证输出:

127.0.0.1 - - [03/Aug/2026:17:07:56 +0800] "GET /api/v1/stats HTTP/1.1" 200 - "-" "-" 0ms
127.0.0.1 - - [03/Aug/2026:17:07:56 +0800] "GET /api/v1/nope-not-a-route HTTP/1.1" 404 - "-" "-" 0ms
  • CLF 时间戳 %d/%b/%Y:%H:%M:%S %z03/Aug/2026:17:07:56 +0800
  • HTTP 版本动态渲染 (0.9~3) ✅
  • 404 请求正确记录(middleware ordering fix 有效)✅

chrono 依赖引入: ✅ 作为 optional feature 添加,不影响其他 feature 编译

Middleware ordering fix: ✅ fallback 移到 layer(middleware) 之前,确保 404 请求经过 access log

合并注意事项

#127 也修改 handlers.rstests/serve_api.rs,两个 PR 存在冲突。建议先合并此 PR,然后 #127 rebase 到 main 后重新验证。

@c2j
c2j merged commit e9fe730 into c2j:main Aug 3, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants