picgo/middleware/log.go

26 lines
607 B
Go
Raw Permalink Normal View History

2024-08-06 20:37:34 +08:00
package middleware
import (
"net/http"
"picgo/corelib"
"time"
)
// LoggingMiddleware 是一个中间件函数,用于记录 HTTP 请求日志
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 获取请求的详细信息
start := time.Now()
method := r.Method
path := r.URL.Path
remoteAddr := r.RemoteAddr
referer := r.Referer()
// 调用下一个处理程序
next.ServeHTTP(w, r)
// 记录日志
corelib.Logger.Infof("[%s] %s %s %s %s", time.Since(start), method, path, remoteAddr, referer)
})
}