H5W3
当前位置:H5W3 > go > 正文

【go】gin 为什么插入了2次记录

// 日志记录到文件
func LogerMiddleware() gin.HandlerFunc {
dir := file.GetPwd()
logFilePath := fmt.Sprintf("%s/%s%s", dir, setting.AppSetting.RuntimeRootPath, setting.AppSetting.LogSavePath)
logFileName := "gin"
// 日志文件
fileName := path.Join(logFilePath, logFileName)
fmt.Println("LogerMiddleware: " + fileName)
// 写入文件
src, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
if err != nil {
fmt.Println("err", err)
}
// 实例化
logger := logrus.New()
//设置日志级别
logger.SetLevel(logrus.DebugLevel)
//设置输出
logger.Out = src
// 设置 rotatelogs
logWriter, err := rotatelogs.New(
// 分割后的文件名称
fileName+".%Y-%m-%d.log",
// 生成软链,指向最新日志文件
rotatelogs.WithLinkName(fileName),
// 设置最大保存时间(7天)
rotatelogs.WithMaxAge(7*24*time.Hour),
// 设置日志切割时间间隔(1天)
rotatelogs.WithRotationTime(24*time.Hour),
)
writeMap := lfshook.WriterMap{
logrus.InfoLevel:  logWriter,
logrus.FatalLevel: logWriter,
logrus.DebugLevel: logWriter,
logrus.WarnLevel:  logWriter,
logrus.ErrorLevel: logWriter,
logrus.PanicLevel: logWriter,
}
logger.AddHook(lfshook.NewHook(writeMap, &logrus.TextFormatter{
TimestampFormat: "15:04:05",
}))
return func(c *gin.Context) {
//开始时间
startTime := time.Now()
//处理请求
c.Next()
//结束时间
endTime := time.Now()
logger.Infof("%3d | %8v | %13s | %s | %s | %s | %s | %s |",
c.Writer.Status(),
endTime.Sub(startTime),
c.ClientIP(),
c.Request.Method,
c.Request.RequestURI,
c.Request.Proto,
c.Request.UserAgent(),
c.Request.Referer(),
)
}
}
time="16:48:31" level=info msg="200 | 4.000226207s |     127.0.0.1 | GET | /gapi/MenuGoods/login | HTTP/1.0 | Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 Edg/80.0.361.69 |  |"
time="2020-04-03T16:48:31+08:00" level=info msg="200 | 4.000226207s |     127.0.0.1 | GET | /gapi/MenuGoods/login | HTTP/1.0 | Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 Edg/80.0.361.69 |  |"
time="16:54:12" level=info msg="200 | 4.000376069s |     127.0.0.1 | GET | /gapi/MenuGoods/login | HTTP/1.0 | Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 Edg/80.0.361.69 |  |"
time="2020-04-03T16:54:12+08:00" level=info msg="200 | 4.000376069s |     127.0.0.1 | GET | /gapi/MenuGoods/login | HTTP/1.0 | Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 Edg/80.0.361.69 |  |"

时间都不一样,应该是两次独立的请求吧

你给logger加的hook也写入了日志吧

回答

本文地址:H5W3 » 【go】gin 为什么插入了2次记录

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址