123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- "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;
|