123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 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');
- function CallLog(objController) {
- /**
- * @type {Controller}
- */
- this.objController = objController;
- }
- function writeRedis(pushData) {
- try {
- var config = configs['redisInfo']['logstash_redis'];
- if (config) {
- objRedis.rpush('logstash:redis', JSON.stringify(pushData));
- }
- } catch (ex) {
- // 这里需要告警
- if (DEBUG) {
- throw ex;
- }
- }
- }
- CallLog.prototype.getCallId = function() {
- if (!call_id) {
- call_id = new Date().getTime() + "" + parseInt(Math.random() * 10000);
- }
- return call_id;
- }
- CallLog.prototype.logSelfCall = function(code, response) {
- var _req = this.objController && this.objController._req || {};
- var _startTime = this.objController && this.objController._startTime;
- var data = {};
- data['call_id'] = this.getCallId();
- 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.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'] = '0.0.0.0';
- }
- var pushData = {
- 'message' : data,
- 'type' : TYPE_SELF_CALL,
- 'time' : php.date('Y-m-d H:i:s', curTime / 1000)
- };
- writeRedis(pushData);
- }
- CallLog.prototype.logModuleCall = function(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 (is_array(postData)) {
- postParam = 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);
- }
- module.exports = CallLog;
|