"use strict" var Q = require('q'); var OujRedis = require('./OujRedis.js'); var configs = require('../../conf/config.inc.js'); var php = require('phpjs'); var call_id = null; var objRedis = OujRedis.init('logstash_redis'); class CallLog { constructor(objController) { /** * @type {Controller} */ this.objController = objController; } setFromUrl(url) { this.fromUrl = url; } getCallId() { if (!call_id) { call_id = new Date().getTime() + "" + parseInt(Math.random() * 10000); } return call_id; } logSelfCall(code, response) { var _req = this.objController && this.objController._req || {}; var _startTime = this.objController && this.objController._startTime; var data = {}; data['call_id'] = this.getCallId(); if (this.fromUrl) { data['url'] = this.fromUrl; } else { data['url'] = '/' + CONTROLLER_NAME + '/' + ACTION_NAME; } data['method'] = _req.method; var getParam = php.http_build_query(_req.query); var postParam = php.http_build_query(_req.body); var param = ''; if (getParam && postParam) { param = getParam + '&' + postParam; } else if (getParam) { param = getParam; } else { param = postParam; } data['param'] = param; if (_req.headers) { data['cookie'] = _req.headers.cookie; data['useragent'] = _req.headers['user-agent']; } if (this.objController && this.objController.debugMsg) { response = '【debugMsg:' + this.objController.debugMsg + "】{response}"; } data['response'] = response ? response.substr(0, 3000) : ''; data['code'] = code; var curTime = new Date().getTime(); data['delay'] = (curTime - _startTime) / 1000; data['server_ip'] = getServerIp(); if (this.objController) { data['client_ip'] = getClientIp(this.objController._req); } else { data['client_ip'] = getServerIp(); } var pushData = { 'message': data, 'type': TYPE_SELF_CALL, 'time': php.date('Y-m-d H:i:s', curTime / 1000) }; writeRedis(pushData); } logModuleCall(method, toUrl, postData, response, startTime) { var data = {}; data['from_call_id'] = this.getCallId(); data['from_url'] = '/'.CONTROLLER_NAME + '/' + ACTION_NAME; data['method'] = method; var parts = explode('?', toUrl); data['to_url'] = parts[0]; var getParam = parts[1]; var postParam = ''; if (php.is_array(postData)) { postParam = php.http_build_query(postData); } else { postParam = postData; } var param = ''; if (getParam && postParam) { param = getParam + '&' + postParam; } else if (getParam) { param = getParam; } else { param = postParam; } data['param'] = param; if (response !== false) { var objResult = JSON.parse(response); data['code'] = objResult['code']; data['response'] = response.substr(0, 3000); } else { data['code'] = CODE_REQUEST_TIMEOUT; } data['delay'] = new Date().getTime() - _startTime; data['server_ip'] = getServerIp(); var pushData = { message: data, type: TYPE_MODULE_CALL, time: date('Y-m-d H:i:s', startTime) }; writeRedis(pushData); } } function writeRedis(pushData) { try { var config = configs['redisInfo']['logstash_redis']; if (config) { objRedis.rpush(CALL_LOG_KEY, JSON.stringify(pushData)); } } catch (ex) { // 这里需要告警 if (DEBUG) { throw ex; } } } module.exports = CallLog;