混入日志包
This commit is contained in:
@@ -1,38 +1,12 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/lestrrat/go-file-rotatelogs"
|
||||
"github.com/rifflock/lfshook"
|
||||
"github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"main/init/qmlog"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Logger() gin.HandlerFunc {
|
||||
logClient := logrus.New()
|
||||
//禁止logrus的输出
|
||||
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
||||
if err != nil {
|
||||
fmt.Println("err", err)
|
||||
}
|
||||
logClient.Out = src
|
||||
logClient.SetLevel(logrus.DebugLevel)
|
||||
apiLogPath := "api.log"
|
||||
logWriter, err := rotatelogs.New(
|
||||
apiLogPath+".%Y-%m-%d-%H-%M.log",
|
||||
rotatelogs.WithLinkName(apiLogPath), // 生成软链,指向最新日志文件
|
||||
rotatelogs.WithMaxAge(7*24*time.Hour), // 文件最大保存时间
|
||||
rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔
|
||||
)
|
||||
writeMap := lfshook.WriterMap{
|
||||
logrus.InfoLevel: logWriter,
|
||||
logrus.FatalLevel: logWriter,
|
||||
}
|
||||
lfHook := lfshook.NewHook(writeMap, &logrus.JSONFormatter{})
|
||||
logClient.AddHook(lfHook)
|
||||
|
||||
return func(c *gin.Context) {
|
||||
// 开始时间
|
||||
start := time.Now()
|
||||
@@ -50,7 +24,7 @@ func Logger() gin.HandlerFunc {
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := c.Request.Body.Read(buf)
|
||||
requestParams := buf[0:n]
|
||||
logClient.Infof("| %3d | %13v | %15s | %s %s |%s|",
|
||||
qmlog.QMLog.Infof("| %3d | %13v | %15s | %s %s |%s|",
|
||||
statusCode,
|
||||
latency,
|
||||
clientIP,
|
||||
|
||||
Reference in New Issue
Block a user