package graylog import ( "encoding/json" "github.com/gin-gonic/gin" "github.com/robertkowalski/graylog-golang" ) var grayLog *gelf.Gelf func init() { grayLog = gelf.New(gelf.Config{ GraylogPort: 62001, GraylogHostname: "172.18.138.7", Connection: "wan", MaxChunkSizeWan: 42, MaxChunkSizeLan: 1337, }) } func Log(log interface{}) { logStr, _ := json.Marshal(log) grayLog.Log(string(logStr)) } func LogApi(shortMsg string, desc string, extraDesc string, reqSource string) { grayLogData := struct { ShortMessage string `json:"short_message"` ReqSource string `json:"req_source"` AdsDetail string `json:"ads_detail"` AdsExtraDetail string `json:"ads_extra_detail"` }{ ShortMessage: shortMsg, ReqSource: reqSource, AdsDetail: desc, AdsExtraDetail: extraDesc, } Log(grayLogData) } // 发送返回日志 func ReportGrayLog(c *gin.Context, uid int, response string, jsonFlag int) { path := c.Request.URL.Path reqTs := c.Request.Time() ip = req.headers.get('X-Forwarded-For',"") req_source_ip = req_data.get('ip',[]) if len(req_source_ip): check_req_source_flag = redis_ip.check_req_source(req_data.get('req_source',[])[0]) if check_req_source_flag: ip = req_source_ip[0] if ip=="": ip = self.request.headers.get("X-Real-IP","") if ip=="": ip = req.remote_ip if ip.find(",") > 0: ip = ip.split(",")[0] device_id = req_data.get('device_id,', '') request = urllib.unquote(urllib.urlencode(req_data, 'utf-8')) province,city,city_code = ip_location_helper.get_region_by_ip(ip) gelf_data = {} gelf_data['short_message'] = 'ads_api_log' gelf_data['_user_id'] = user_id gelf_data['_device_id'] = device_id gelf_data['_ip'] = ip gelf_data['_uri'] = path gelf_data['_province']=province gelf_data['_city']=city gelf_data['_city_code']=city_code for key,value in req_data.items(): if str(key) in ['ip']: continue gelf_data['_{0}'.format(key)] = common.my_str(value[0]) req_len = 102400 gelf_data['_request'] = request[0:req_len] #json_response = json.dumps(response) ### 结果不上报了 json_response = "" if len(json_response) < req_len and len(json_response) > 60: try: json_response = json.dumps( response, indent=4, sort_keys=True, ensure_ascii=False).encode("utf-8") except: send_try_except() try: json_response = json_response.decode("utf-8") except: pass gelf_data['_response'] = json_response[0:req_len] gelf_data['_response_time'] = round(req_ts * 1000.0, 2) send_to_graylog_tornado(gelf_data, req=req, req_data=cur_req_data) }