2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 10:17:17 +08:00

feat: 添加日志记录中间件

This commit is contained in:
耗子
2024-06-25 02:38:13 +08:00
parent e6b6f06706
commit 31bd15217b
3 changed files with 28 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ type Kernel struct {
// These middleware are run during every request to your application.
func (kernel Kernel) Middleware() []http.Middleware {
return []http.Middleware{
middleware.Log(),
middleware.Status(),
}
}

View File

@@ -0,0 +1,20 @@
package middleware
import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
)
// Log 记录请求日志
func Log() http.Middleware {
return func(ctx http.Context) {
facades.Log().Channel("http").With(map[string]any{
"Method": ctx.Request().Method(),
"URL": ctx.Request().FullUrl(),
"IP": ctx.Request().Ip(),
"UA": ctx.Request().Header("User-Agent"),
"Body": ctx.Request().All(),
}).Info("HTTP Request")
ctx.Request().Next()
}
}

View File

@@ -37,6 +37,13 @@ func init() {
"days": 7,
"print": true,
},
"http": map[string]any{
"driver": "daily",
"path": "storage/logs/http.log",
"level": "info",
"days": 7,
"print": false,
},
},
})
}