graylog.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package graylog
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "github.com/robertkowalski/graylog-golang"
  6. )
  7. var grayLog *gelf.Gelf
  8. func init() {
  9. grayLog = gelf.New(gelf.Config{
  10. GraylogPort: 62001,
  11. GraylogHostname: "172.18.138.7",
  12. Connection: "wan",
  13. MaxChunkSizeWan: 42,
  14. MaxChunkSizeLan: 1337,
  15. })
  16. }
  17. func Log(log interface{}) {
  18. logStr, _ := json.Marshal(log)
  19. grayLog.Log(string(logStr))
  20. }
  21. func LogApi(shortMsg string, desc string, extraDesc string, reqSource string) {
  22. grayLogData := struct {
  23. ShortMessage string `json:"short_message"`
  24. ReqSource string `json:"req_source"`
  25. AdsDetail string `json:"ads_detail"`
  26. AdsExtraDetail string `json:"ads_extra_detail"`
  27. }{
  28. ShortMessage: shortMsg,
  29. ReqSource: reqSource,
  30. AdsDetail: desc,
  31. AdsExtraDetail: extraDesc,
  32. }
  33. Log(grayLogData)
  34. }
  35. // 发送返回日志
  36. func ReportGrayLog(c *gin.Context, uid int, response string, jsonFlag int) {
  37. path := c.Request.URL.Path
  38. reqTs := c.Request.Time()
  39. ip = req.headers.get('X-Forwarded-For',"")
  40. req_source_ip = req_data.get('ip',[])
  41. if len(req_source_ip):
  42. check_req_source_flag = redis_ip.check_req_source(req_data.get('req_source',[])[0])
  43. if check_req_source_flag:
  44. ip = req_source_ip[0]
  45. if ip=="":
  46. ip = self.request.headers.get("X-Real-IP","")
  47. if ip=="":
  48. ip = req.remote_ip
  49. if ip.find(",") > 0:
  50. ip = ip.split(",")[0]
  51. device_id = req_data.get('device_id,', '')
  52. request = urllib.unquote(urllib.urlencode(req_data, 'utf-8'))
  53. province,city,city_code = ip_location_helper.get_region_by_ip(ip)
  54. gelf_data = {}
  55. gelf_data['short_message'] = 'ads_api_log'
  56. gelf_data['_user_id'] = user_id
  57. gelf_data['_device_id'] = device_id
  58. gelf_data['_ip'] = ip
  59. gelf_data['_uri'] = path
  60. gelf_data['_province']=province
  61. gelf_data['_city']=city
  62. gelf_data['_city_code']=city_code
  63. for key,value in req_data.items():
  64. if str(key) in ['ip']:
  65. continue
  66. gelf_data['_{0}'.format(key)] = common.my_str(value[0])
  67. req_len = 102400
  68. gelf_data['_request'] = request[0:req_len]
  69. #json_response = json.dumps(response)
  70. ### 结果不上报了
  71. json_response = ""
  72. if len(json_response) < req_len and len(json_response) > 60:
  73. try:
  74. json_response = json.dumps(
  75. response, indent=4, sort_keys=True,
  76. ensure_ascii=False).encode("utf-8")
  77. except:
  78. send_try_except()
  79. try:
  80. json_response = json_response.decode("utf-8")
  81. except:
  82. pass
  83. gelf_data['_response'] = json_response[0:req_len]
  84. gelf_data['_response_time'] = round(req_ts * 1000.0, 2)
  85. send_to_graylog_tornado(gelf_data, req=req, req_data=cur_req_data)
  86. }