logger.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package middleware
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. "device-manage/app/admin/models"
  8. "device-manage/common/global"
  9. "device-manage/tools"
  10. config2 "device-manage/tools/config"
  11. )
  12. // 日志记录到文件
  13. func LoggerToFile() gin.HandlerFunc {
  14. return func(c *gin.Context) {
  15. // 开始时间
  16. startTime := time.Now()
  17. // 处理请求
  18. c.Next()
  19. // 结束时间
  20. endTime := time.Now()
  21. // 执行时间
  22. latencyTime := endTime.Sub(startTime)
  23. // 请求方式
  24. reqMethod := c.Request.Method
  25. // 请求路由
  26. reqUri := c.Request.RequestURI
  27. // 状态码
  28. statusCode := c.Writer.Status()
  29. // 请求IP
  30. clientIP := c.ClientIP()
  31. // 日志格式
  32. fmt.Printf("%s [INFO] %s %s %3d %13v %15s \r\n",
  33. startTime.Format("2006-01-02 15:04:05"),
  34. reqMethod,
  35. reqUri,
  36. statusCode,
  37. latencyTime,
  38. clientIP,
  39. )
  40. global.RequestLogger.Info(statusCode, latencyTime, clientIP, reqMethod, reqUri)
  41. if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" && config2.LoggerConfig.EnabledDB {
  42. SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime)
  43. }
  44. }
  45. }
  46. // 写入操作日志表
  47. // 该方法后续即将弃用
  48. func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string, reqMethod string, latencyTime time.Duration) {
  49. menu := models.Menu{}
  50. menu.Path = reqUri
  51. menu.Action = reqMethod
  52. menuList, _ := menu.Get()
  53. sysOperLog := models.SysOperLog{}
  54. sysOperLog.OperIp = clientIP
  55. sysOperLog.OperLocation = tools.GetLocation(clientIP)
  56. sysOperLog.Status = tools.IntToString(statusCode)
  57. sysOperLog.OperName = tools.GetUserName(c)
  58. sysOperLog.RequestMethod = c.Request.Method
  59. sysOperLog.OperUrl = reqUri
  60. if reqUri == "/login" {
  61. sysOperLog.BusinessType = "10"
  62. sysOperLog.Title = "用户登录"
  63. sysOperLog.OperName = "-"
  64. } else if strings.Contains(reqUri, "/api/v1/logout") {
  65. sysOperLog.BusinessType = "11"
  66. } else if strings.Contains(reqUri, "/api/v1/getCaptcha") {
  67. sysOperLog.BusinessType = "12"
  68. sysOperLog.Title = "验证码"
  69. } else {
  70. if reqMethod == "POST" {
  71. sysOperLog.BusinessType = "1"
  72. } else if reqMethod == "PUT" {
  73. sysOperLog.BusinessType = "2"
  74. } else if reqMethod == "DELETE" {
  75. sysOperLog.BusinessType = "3"
  76. }
  77. }
  78. sysOperLog.Method = reqMethod
  79. if len(menuList) > 0 {
  80. sysOperLog.Title = menuList[0].Title
  81. }
  82. b, _ := c.Get("body")
  83. sysOperLog.OperParam, _ = tools.StructToJsonStr(b)
  84. sysOperLog.CreateBy = tools.GetUserName(c)
  85. sysOperLog.OperTime = tools.GetCurrentTime()
  86. sysOperLog.LatencyTime = (latencyTime).String()
  87. sysOperLog.UserAgent = c.Request.UserAgent()
  88. if c.Err() == nil {
  89. sysOperLog.Status = "0"
  90. } else {
  91. sysOperLog.Status = "1"
  92. }
  93. _, _ = sysOperLog.Create()
  94. }