xfy 6f3ecb0a9f feat(middleware,server): 实现访问日志中间件
- 新增 accesslog 中间件,记录请求方法、路径、状态码、响应大小和处理时间
- 集成到 Server 的 single 和 vhost 模式
- 支持 graceful shutdown 时关闭日志文件

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-03 10:54:53 +08:00

42 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package accesslog 提供访问日志中间件,记录每个请求的详细信息。
package accesslog
import (
"time"
"github.com/valyala/fasthttp"
"rua.plus/lolly/internal/config"
"rua.plus/lolly/internal/logging"
)
// AccessLog 访问日志中间件,记录请求方法、路径、状态码、响应大小和处理时间。
type AccessLog struct {
logger *logging.Logger
}
// New 创建访问日志中间件。
func New(cfg *config.LoggingConfig) *AccessLog {
return &AccessLog{
logger: logging.New(cfg),
}
}
// Name 返回中间件名称。
func (a *AccessLog) Name() string {
return "accesslog"
}
// Process 包装 handler在请求处理后记录访问日志。
func (a *AccessLog) Process(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
start := time.Now()
next(ctx)
duration := time.Since(start)
a.logger.LogAccess(ctx, ctx.Response.StatusCode(), int64(len(ctx.Response.Body())), duration)
}
}
// Close 关闭日志文件。
func (a *AccessLog) Close() error {
return a.logger.Close()
}