diff --git a/app/http/kernel.go b/app/http/kernel.go index a1a86c73..17bf3bc0 100644 --- a/app/http/kernel.go +++ b/app/http/kernel.go @@ -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(), } } diff --git a/app/http/middleware/log.go b/app/http/middleware/log.go new file mode 100644 index 00000000..4dbc829d --- /dev/null +++ b/app/http/middleware/log.go @@ -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() + } +} diff --git a/config/logging.go b/config/logging.go index ce1e9568..abcd31c5 100644 --- a/config/logging.go +++ b/config/logging.go @@ -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, + }, }, }) }