添加日志记录中间件

Signed-off-by: jagger <cossjie@foxmail.com>
This commit is contained in:
jagger 2025-02-27 16:50:11 +08:00
parent 0bc023ce84
commit 6a4a787c73
2 changed files with 61 additions and 1 deletions

View File

@ -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)
}

3
pcm.go
View File

@ -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)