xfy f2352ab9cc docs(config,stream,logging,handler,proxy,cache,server,ssl,middleware): 为核心模块添加详细 GoDoc 文档注释
- config: 为 Config 和所有子配置结构添加完整文档,包含使用示例和注意事项
- stream: 为负载均衡器和服务器添加详细的参数、返回值和功能说明
- logging: 为日志格式化和输出函数添加文档,说明支持的变量替换
- handler: 为路由器、静态文件和 sendfile 处理器添加文档
- proxy: 为健康检查器和代理功能添加完整文档
- cache/server/ssl/middleware: 补充相关模块的文档注释
- config.example.yaml: 添加可信代理配置、加密套件示例,更新压缩级别说明

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 15:36:09 +08:00

74 lines
1.8 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 提供访问日志中间件,记录每个请求的详细信息。
//
// 该文件包含访问日志相关的核心逻辑,包括:
// - 请求方法和路径记录
// - 响应状态码和大小记录
// - 请求处理耗时记录
//
// 使用示例:
//
// accessLog := accesslog.New(cfg.Logging)
// chain := middleware.NewChain(accessLog)
//
// 作者xfy
package accesslog
import (
"time"
"github.com/valyala/fasthttp"
"rua.plus/lolly/internal/config"
"rua.plus/lolly/internal/logging"
)
// AccessLog 访问日志中间件,记录请求方法、路径、状态码、响应大小和处理时间。
type AccessLog struct {
// logger 日志记录器实例,用于输出访问日志
logger *logging.Logger
}
// New 创建访问日志中间件。
//
// 参数:
// - cfg: 日志配置,包含输出路径、格式等设置
//
// 返回值:
// - *AccessLog: 访问日志中间件实例
func New(cfg *config.LoggingConfig) *AccessLog {
return &AccessLog{
logger: logging.New(cfg),
}
}
// Name 返回中间件名称。
//
// 返回值:
// - string: 中间件名称 "accesslog"
func (a *AccessLog) Name() string {
return "accesslog"
}
// Process 包装 handler在请求处理后记录访问日志。
//
// 参数:
// - next: 下一个请求处理器
//
// 返回值:
// - fasthttp.RequestHandler: 包装后的请求处理器
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 关闭日志文件。
//
// 返回值:
// - error: 关闭失败时返回错误,成功返回 nil
func (a *AccessLog) Close() error {
return a.logger.Close()
}