From 6a4a787c73441cb3d86dc19da41b540607463c34 Mon Sep 17 00:00:00 2001 From: jagger Date: Thu, 27 Feb 2025 16:50:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=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 Signed-off-by: jagger --- internal/middleware/logmiddleware.go | 59 ++++++++++++++++++++++++++++ pcm.go | 3 +- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 internal/middleware/logmiddleware.go diff --git a/internal/middleware/logmiddleware.go b/internal/middleware/logmiddleware.go new file mode 100644 index 00000000..41a589b9 --- /dev/null +++ b/internal/middleware/logmiddleware.go @@ -0,0 +1,59 @@ +package middleware + +import ( + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + "net/http" + "net/http/httputil" + "strings" +) + +const ( + // ApplicationJson stands for application/json. + ApplicationJson = "application/json" + // ContentType is the header key for Content-Type. + ContentType = "Content-Type" +) + +func LogMiddleware(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + proxy := &responseProxy{w: w} + requestLog(r) + next(proxy, r) + logx.Infof("LogMiddleware response uri:%s jsonResult :%+v", r.RequestURI, string(proxy.body)) + } +} + +type responseProxy struct { + w http.ResponseWriter + body []byte +} + +func (p *responseProxy) Header() http.Header { + return p.w.Header() +} +func (p *responseProxy) Write(data []byte) (int, error) { + p.body = append(p.body, data...) + return p.w.Write(data) +} +func (p *responseProxy) WriteHeader(statusCode int) { + p.w.WriteHeader(statusCode) +} + +func requestLog(r *http.Request) { + // 打印所有header + logx.Infof("LogMiddleware request uri:%s header :%+v", r.RequestURI, r.Header) + // json日志 + if withJsonBody(r) { + requestDump, err := httputil.DumpRequest(r, true) + logx.Infof("LogMiddleware request uri:%s jsonParams :%+v, err:%+v", r.RequestURI, string(requestDump), err) + } else { + // form表单日志和其他 + formParams, err := httpx.GetFormValues(r) + logx.Infof("LogMiddleware request uri:%s formParams :%+v, err:%+v", r.RequestURI, formParams, err) + } +} + +func withJsonBody(r *http.Request) bool { + return r.ContentLength > 0 && strings.Contains(r.Header.Get(ContentType), ApplicationJson) +} diff --git a/pcm.go b/pcm.go index 95215948..fb645b82 100644 --- a/pcm.go +++ b/pcm.go @@ -24,6 +24,7 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/internal/config" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/cron" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/handler" + "gitlink.org.cn/JointCloud/pcm-coordinator/internal/middleware" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/mqs" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" ) @@ -40,7 +41,7 @@ func main() { defer serviceGroup.Stop() server := rest.MustNewServer(c.RestConf, rest.WithCors()) - + server.Use(middleware.LogMiddleware) ctx := svc.NewServiceContext(c) // start log component logx.MustSetup(c.LogConf)