28 lines
700 B
Go
28 lines
700 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/cloudwego/hertz/pkg/app"
|
|
|
|
"gitea.micah.wiki/pandora/starter/pkg/logx"
|
|
)
|
|
|
|
func LogMiddleware() app.HandlerFunc {
|
|
return func(ctx context.Context, c *app.RequestContext) {
|
|
start := time.Now()
|
|
|
|
uri := string(c.Request.URI().Path())
|
|
query := string(c.Request.URI().QueryString())
|
|
body := string(c.Request.Body())
|
|
if len(body) > 0 {
|
|
body = strings.ReplaceAll(body, "\n", "\\n")
|
|
}
|
|
logx.CtxInfo(ctx, "com_request_in uri: `%s`, query: `%s`, body: `%s`", uri, query, body)
|
|
c.Next(ctx)
|
|
logx.CtxInfo(ctx, "com_request_out uri: `%s`, cost: `%+v`, resp: `%s`", uri, time.Since(start), string(c.Response.Body()))
|
|
}
|
|
}
|