From 31bd15217b93de2d1ae1e2b658020d4e9faad0a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Tue, 25 Jun 2024 02:38:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/http/kernel.go | 1 + app/http/middleware/log.go | 20 ++++++++++++++++++++ config/logging.go | 7 +++++++ 3 files changed, 28 insertions(+) create mode 100644 app/http/middleware/log.go 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, + }, }, }) }