@@ -32,6 +32,6 @@ func Init() {
|
|||||||
log.Fatalln("Can't read config!")
|
log.Fatalln("Can't read config!")
|
||||||
}
|
}
|
||||||
if err := viper.Unmarshal(conf); err != nil {
|
if err := viper.Unmarshal(conf); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -35,26 +34,3 @@ func Init() {
|
|||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Gin() gin.HandlerFunc {
|
|
||||||
return func(ctx *gin.Context) {
|
|
||||||
startTime := time.Now()
|
|
||||||
ctx.Next()
|
|
||||||
endTime := time.Now()
|
|
||||||
latencyTime := endTime.Sub(startTime)
|
|
||||||
reqMethod := ctx.Request.Method
|
|
||||||
reqUri := ctx.Request.RequestURI
|
|
||||||
statusCode := ctx.Writer.Status()
|
|
||||||
clientIP := ctx.ClientIP()
|
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
"METHOD": reqMethod,
|
|
||||||
"URI": reqUri,
|
|
||||||
"STATUS": statusCode,
|
|
||||||
"LATENCY": latencyTime,
|
|
||||||
"CLIENT_IP": clientIP,
|
|
||||||
}).Info("HTTP REQUEST")
|
|
||||||
|
|
||||||
ctx.Next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
48
middleware/gin_logger.go
Normal file
48
middleware/gin_logger.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GinLogger(log *logrus.Logger) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
var body []byte
|
||||||
|
if c.Request.Body != nil {
|
||||||
|
body, _ = io.ReadAll(c.Request.Body)
|
||||||
|
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
|
||||||
|
var errorMessage string
|
||||||
|
if len(c.Errors) > 0 {
|
||||||
|
errorMessage = c.Errors.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := log.WithFields(logrus.Fields{
|
||||||
|
"status": c.Writer.Status(),
|
||||||
|
"method": c.Request.Method,
|
||||||
|
"uri": c.Request.RequestURI,
|
||||||
|
"ip": c.ClientIP(),
|
||||||
|
"latency": time.Since(startTime).String(),
|
||||||
|
"user_agent": c.Request.UserAgent(),
|
||||||
|
"request_body": string(body),
|
||||||
|
"errors": errorMessage,
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(c.Errors) > 0 || c.Writer.Status() >= 500 {
|
||||||
|
entry.Error("HTTP_ERROR")
|
||||||
|
} else if c.Writer.Status() >= 400 {
|
||||||
|
entry.Warn("HTTP_CLIENT_ERROR")
|
||||||
|
} else {
|
||||||
|
entry.Info("HTTP_SUCCESS")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"nixcn-cms/logger"
|
"nixcn-cms/middleware"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.Use(logger.Gin(), gin.Recovery())
|
r.Use(gin.Recovery(), middleware.GinLogger())
|
||||||
|
|
||||||
Router(r)
|
Router(r)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user