serverLogs.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package logs
  2. import (
  3. log "app.yhyue.com/moapp/jylog"
  4. . "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
  5. "bp.jydev.jianyu360.cn/BaseService/gateway/core/logs/internal/notice"
  6. "bp.jydev.jianyu360.cn/BaseService/gateway/core/logs/internal/savedb"
  7. "bp.jydev.jianyu360.cn/BaseService/gateway/core/router"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/gogf/gf/v2/errors/gerror"
  11. "github.com/gogf/gf/v2/net/ghttp"
  12. "github.com/gogf/gf/v2/os/gtime"
  13. "github.com/gogf/gf/v2/text/gstr"
  14. "strings"
  15. "time"
  16. )
  17. // server 请求日志
  18. //type serverLog struct {
  19. // Notice internal.LogsNotice
  20. // SDb internal.SaveLogsDb
  21. //}
  22. type serverLog struct {
  23. Notice *notice.Notice
  24. SDb *savedb.DbLogs
  25. }
  26. func (s *serverLog) initNotice(nc *notice.Notice) *serverLog {
  27. s.Notice = nc
  28. return s
  29. }
  30. func (s *serverLog) initSaveLog(sDB *savedb.DbLogs) *serverLog {
  31. s.SDb = sDB
  32. go s.SDb.SaveLogTask()
  33. return s
  34. }
  35. // RecordLogAndNotice 记录请求日志及提醒
  36. func (s *serverLog) RecordLogAndNotice(r *ghttp.Request, err error) {
  37. handlerCtx := router.GetGContext(r.GetCtx())
  38. //请求时间校验
  39. tookTime := gtime.TimestampMilli() - r.EnterTime
  40. if err == nil {
  41. compareTime := handlerCtx.RouterRule.TimeOut
  42. if compareTime == 0 {
  43. compareTime = 2000
  44. }
  45. if tookTime > compareTime {
  46. err = NewErrorWithCode(SERVER_DETAIL_TIMEOUT, fmt.Sprintf("接口超时: %d>%d", handlerCtx.RouterRule.TimeOut, tookTime))
  47. }
  48. }
  49. var (
  50. scheme = "http"
  51. proto = r.Header.Get("X-Forwarded-Proto")
  52. code = gerror.Code(err)
  53. codeDetail = code.Detail()
  54. codeDetailStr string
  55. )
  56. if r.TLS != nil || gstr.Equal(proto, "https") {
  57. scheme = "https"
  58. }
  59. if codeDetail != nil { //存在错误信息
  60. codeDetailStr = gstr.Replace(fmt.Sprintf(`%+v`, codeDetail), "\n", " ")
  61. } else if err != nil {
  62. codeDetailStr = err.Error()
  63. }
  64. contextDetail := fmt.Sprintf(
  65. `%d %s "%s://%s%s proxy:%s %s" %d ms, %s, "%s", "%s", %d, "%s", "%+v"`,
  66. r.Response.Status, r.Method, scheme, r.Host, r.URL.String(), handlerCtx.ServerAddr, r.Proto,
  67. tookTime, r.GetClientIp(), r.Referer(), r.UserAgent(),
  68. code.Code(), code.Message(), codeDetailStr,
  69. )
  70. if err != nil {
  71. // 发送提醒信息
  72. if code.Code() > 2000 && s.Notice != nil {
  73. _ = s.Notice.SendWarnMsg(map[string]interface{}{
  74. "log": contextDetail,
  75. })
  76. }
  77. log.WithContext(r.Context()).Debugln(gerror.Stack(err))
  78. }
  79. if s.SDb != nil {
  80. s.SDb.AddLogToTask(PaseReq(handlerCtx, r))
  81. }
  82. //日志输出
  83. log.WithContext(r.Context()).Info(contextDetail)
  84. }
  85. func PaseReq(ctx *router.GContext, r *ghttp.Request) map[string]interface{} {
  86. md, _ := json.Marshal(r.Request.Form)
  87. m := map[string]interface{}{
  88. "ip": r.GetClientIp(),
  89. "refer": r.Referer(),
  90. "mdescribe": string(md),
  91. "client": r.UserAgent(),
  92. "os": getOS(r.UserAgent()),
  93. "browse": getBrowse(r.UserAgent()),
  94. "method": r.Method,
  95. "url": r.URL.String(),
  96. }
  97. if ctx.Sess != nil {
  98. if ctx.Sess.UserId != "" {
  99. m["userid"] = ctx.Sess.UserId
  100. }
  101. if ctx.Sess.NewUid != 0 {
  102. m["userid_new"] = ctx.Sess.NewUid
  103. }
  104. if ctx.Sess.EntId != 0 {
  105. m["userid"] = ctx.Sess.EntId
  106. }
  107. }
  108. timeNow := time.Now()
  109. m["date"] = timeNow.Unix()
  110. m["year"] = timeNow.Year()
  111. m["month"] = timeNow.Month()
  112. m["day"] = timeNow.Day()
  113. m["hour"] = timeNow.Hour()
  114. m["minutes"] = timeNow.Minute()
  115. return m
  116. }
  117. // getOS 获取平台类型
  118. func getOS(useros string) string {
  119. osVersion := "其他"
  120. if strings.Contains(useros, "NT 6.0") {
  121. osVersion = "Windows Vista/Server 2008"
  122. } else if strings.Contains(useros, "NT 5.2") {
  123. osVersion = "Windows Server 2003"
  124. } else if strings.Contains(useros, "NT 5.1") {
  125. osVersion = "Windows XP"
  126. } else if strings.Contains(useros, "NT 5") {
  127. osVersion = "Windows 2000"
  128. } else if strings.Contains(useros, "Mac") {
  129. osVersion = "Mac"
  130. } else if strings.Contains(useros, "Unix") {
  131. osVersion = "UNIX"
  132. } else if strings.Contains(useros, "Linux") {
  133. osVersion = "Linux"
  134. } else if strings.Contains(useros, "SunOS") {
  135. osVersion = "SunOS"
  136. } else if strings.Contains(useros, "NT 6.3") {
  137. osVersion = "Window8"
  138. } else if strings.Contains(useros, "NT 6.1") {
  139. osVersion = "Window7"
  140. } else if strings.Contains(useros, "NT 10.0") {
  141. osVersion = "Window10"
  142. }
  143. return osVersion
  144. }
  145. // getBrowse 获取浏览器类型
  146. func getBrowse(userbrowser string) string {
  147. browserVersion := "其他"
  148. if strings.Contains(userbrowser, "MSIE") {
  149. browserVersion = "IE"
  150. } else if strings.Contains(userbrowser, "Firefox") {
  151. browserVersion = "Firefox"
  152. } else if strings.Contains(userbrowser, "Chrome") {
  153. browserVersion = "Chrome"
  154. } else if strings.Contains(userbrowser, "Safari") {
  155. browserVersion = "Safari"
  156. } else if strings.Contains(userbrowser, "rv:11.0") {
  157. browserVersion = "IE11"
  158. }
  159. return browserVersion
  160. }