更新
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package ginx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
Info(ctx context.Context, v ...interface{})
|
||||
Infof(ctx context.Context, format string, v ...interface{})
|
||||
Errorf(ctx context.Context, format string, v ...interface{})
|
||||
}
|
||||
|
||||
type defaultLogger struct{}
|
||||
|
||||
func NewLogger() *defaultLogger {
|
||||
return &defaultLogger{}
|
||||
}
|
||||
|
||||
func (l *defaultLogger) Info(ctx context.Context, v ...interface{}) {
|
||||
log.Printf("[INFO] ", v...)
|
||||
}
|
||||
|
||||
func (l *defaultLogger) Infof(ctx context.Context, format string, v ...interface{}) {
|
||||
log.Printf("[INFO] "+format, v...)
|
||||
}
|
||||
|
||||
func (l *defaultLogger) Errorf(ctx context.Context, format string, v ...interface{}) {
|
||||
log.Printf("[ERROR] "+format, v...)
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 跨域
|
||||
|
||||
func Cors() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 原理是创建的时候注入了一个方法处理响应的信息
|
||||
|
||||
func ResponseFilterMiddleware(c *gin.Context) {
|
||||
|
||||
fmt.Println(c.Writer)
|
||||
blw := &bodyLogWriter{
|
||||
body: bytes.NewBufferString(""),
|
||||
ResponseWriter: c.Writer,
|
||||
}
|
||||
c.Writer = blw
|
||||
c.Next()
|
||||
|
||||
statusCode := c.Writer.Status()
|
||||
|
||||
fmt.Println(statusCode)
|
||||
fmt.Println("拦截输出")
|
||||
}
|
||||
|
||||
type bodyLogWriter struct {
|
||||
gin.ResponseWriter
|
||||
body *bytes.Buffer
|
||||
}
|
||||
|
||||
// 真正输出的时候,它会调用这个输出方法
|
||||
func (w bodyLogWriter) Write(b []byte) (int, error) {
|
||||
// b = []byte(`{"code":0}`)
|
||||
w.body.Write(b)
|
||||
return w.ResponseWriter.Write(b)
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 打印请求参数和响应参数
|
||||
|
||||
func LoggerParams() gin.HandlerFunc {
|
||||
|
||||
return func(ctx *gin.Context) {
|
||||
buf := &bytes.Buffer{}
|
||||
tea := io.TeeReader(ctx.Request.Body, buf)
|
||||
body, err := io.ReadAll(tea)
|
||||
if err != nil {
|
||||
log.Panicf("read body err:%+v", err)
|
||||
}
|
||||
ctx.Request.Body = io.NopCloser(buf)
|
||||
|
||||
// 下面是响应参数缓存
|
||||
blw := &bodyParamsWriter{
|
||||
body: bytes.NewBufferString(""),
|
||||
ResponseWriter: ctx.Writer,
|
||||
}
|
||||
ctx.Writer = blw
|
||||
|
||||
ctx.Next()
|
||||
|
||||
// 判断返回的值是否json
|
||||
contentType := ctx.Writer.Header().Get("Content-Type")
|
||||
|
||||
if !strings.Contains(contentType, "application/json") {
|
||||
return
|
||||
}
|
||||
|
||||
resp := blw.body.String()
|
||||
|
||||
m := map[string]interface{}{
|
||||
"trace_id": ctx.Keys["trace_id"],
|
||||
"method": ctx.Request.Method,
|
||||
"path": ctx.FullPath(),
|
||||
"client_ip": ctx.ClientIP(),
|
||||
"body": string(body),
|
||||
"response": resp,
|
||||
"request_header": ctx.Request.Header,
|
||||
"response_code": ctx.Writer.Status(),
|
||||
}
|
||||
b, _ := json.Marshal(m)
|
||||
|
||||
log.Println(string(b))
|
||||
}
|
||||
|
||||
// return gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
|
||||
// buf := &bytes.Buffer{}
|
||||
// tea := io.TeeReader(param.Request.Body, buf)
|
||||
// body, err := ioutil.ReadAll(tea)
|
||||
// if err != nil {
|
||||
// log.Panicf("read body err:%+v", err)
|
||||
// }
|
||||
// param.Request.Body = io.NopCloser(buf)
|
||||
|
||||
// fmt.Println("ddddd",string(body))
|
||||
|
||||
// // 你的自定义格式
|
||||
// return fmt.Sprintf("[VISIT] trace_id:%s | 参数:%s | IP:%s | 时间:%s | 方法:%s | 路径:%s | 协议:%s | 状态码:%d | 耗时:%s | 代理头:%s | 错误信息:%s \n",
|
||||
// // trace_id
|
||||
// param.Keys["trace_id"],
|
||||
// body,
|
||||
// //客户端IP
|
||||
// param.ClientIP,
|
||||
// //时间格式
|
||||
// param.TimeStamp.Format("2006-01-02 15:04:05"),
|
||||
// //http请求方式 get post等
|
||||
// param.Method,
|
||||
// //客户端请求的路径
|
||||
// param.Path,
|
||||
// //http请求协议版本
|
||||
// param.Request.Proto,
|
||||
// //http请求状态码
|
||||
// param.StatusCode,
|
||||
// //请求耗时
|
||||
// param.Latency,
|
||||
// //http请求代理头
|
||||
// param.Request.UserAgent(),
|
||||
// //处理请求错误时设置错误消息
|
||||
// param.ErrorMessage,
|
||||
// )
|
||||
// })
|
||||
}
|
||||
|
||||
type bodyParamsWriter struct {
|
||||
gin.ResponseWriter
|
||||
body *bytes.Buffer
|
||||
}
|
||||
|
||||
// 真正输出的时候,它会调用这个输出方法
|
||||
func (w bodyParamsWriter) Write(b []byte) (int, error) {
|
||||
// b = []byte(`{"code":0}`)
|
||||
w.body.Write(b)
|
||||
return w.ResponseWriter.Write(b)
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"code.yun.ink/pkg/ginx"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 获取请求参数
|
||||
|
||||
func Params() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
buf := &bytes.Buffer{}
|
||||
tea := io.TeeReader(ctx.Request.Body, buf)
|
||||
body, err := io.ReadAll(tea)
|
||||
if err != nil {
|
||||
ginx.Op.Logger.Errorf(ctx, "read body err=%+v", err)
|
||||
}
|
||||
ctx.Request.Body = io.NopCloser(buf)
|
||||
|
||||
// 下面是响应参数缓存
|
||||
blw := &bodyParamsWriter{
|
||||
body: bytes.NewBufferString(""),
|
||||
ResponseWriter: ctx.Writer,
|
||||
}
|
||||
ctx.Writer = blw
|
||||
|
||||
// 请求参数
|
||||
|
||||
rd := reqData{
|
||||
Method: ctx.Request.Method,
|
||||
Path: ctx.FullPath(),
|
||||
ClientIP: ctx.ClientIP(),
|
||||
Body: string(body),
|
||||
Header: ctx.Request.Header,
|
||||
}
|
||||
|
||||
ginx.Op.Logger.Info(ctx, "request params", rd)
|
||||
|
||||
ctx.Next()
|
||||
|
||||
// 判断返回的值是否json
|
||||
contentType := ctx.Writer.Header().Get("Content-Type")
|
||||
|
||||
if !strings.Contains(contentType, "application/json") {
|
||||
return
|
||||
}
|
||||
|
||||
resp := blw.body.String()
|
||||
|
||||
rsd := respData{
|
||||
HttpCode: ctx.Writer.Status(),
|
||||
Response: resp,
|
||||
// Header: ctx.Writer.Header(),
|
||||
}
|
||||
ginx.Op.Logger.Info(ctx, "response params", rsd)
|
||||
}
|
||||
}
|
||||
|
||||
type reqData struct {
|
||||
Method string `json:"method"`
|
||||
Path string `json:"path"`
|
||||
ClientIP string `json:"client_ip"`
|
||||
Body string `json:"body"`
|
||||
Header map[string][]string `json:"header"`
|
||||
}
|
||||
|
||||
type respData struct {
|
||||
HttpCode int `json:"http_code"`
|
||||
Response string `json:"response"`
|
||||
// Header map[string][]string `json:"header"`
|
||||
}
|
||||
|
||||
type bodyParamsWriter struct {
|
||||
gin.ResponseWriter
|
||||
body *bytes.Buffer
|
||||
}
|
||||
|
||||
// 真正输出的时候,它会调用这个输出方法
|
||||
func (w bodyParamsWriter) Write(b []byte) (int, error) {
|
||||
// b = []byte(`{"code":0}`)
|
||||
w.body.Write(b)
|
||||
return w.ResponseWriter.Write(b)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package ginx
|
||||
|
||||
type Options struct {
|
||||
Logger Logger
|
||||
}
|
||||
|
||||
func defaultOptions() Options {
|
||||
return Options{
|
||||
Logger: NewLogger(),
|
||||
}
|
||||
}
|
||||
|
||||
// 常量
|
||||
var Op Options = defaultOptions()
|
||||
|
||||
type Option func(*Options)
|
||||
|
||||
func InitOptions(opts ...Option) {
|
||||
for _, opt := range opts {
|
||||
opt(&Op)
|
||||
}
|
||||
}
|
||||
|
||||
func SetLogger(log Logger) Option {
|
||||
return func(o *Options) {
|
||||
o.Logger = log
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user