UNPKG

@sex-pomelo/sex-pomelo

Version:

[![NPM version][npm-image-pomelo]][npm-url-pomelo] [![NPM version][npm-image-down]][npm-url-pomelo]

53 lines (45 loc) 1.17 kB
"use strict"; /** * Filter for rpc log. * Record used time for remote process call. * @access private */ const reFilename = __filename.substring(__filename.indexOf("node_modules")); const rpcLogger = require('@sex-pomelo/sex-pomelo-logger').getLogger('rpc-log', reFilename); const utils = require('../../util/utils'); module.exports = function() { return new FilterRpcLog(); }; /** * @class * @implements {Filter} */ let FilterRpcLog = function () { }; FilterRpcLog.prototype.name = 'rpcLog'; /** * Before filter for rpc */ FilterRpcLog.prototype.before = function(serverId, msg, opts, next) { opts = opts||{}; opts.__start_time__ = Date.now(); next(); }; /** * After filter for rpc */ FilterRpcLog.prototype.after = function(serverId, msg, opts, next) { if(!!opts && !!opts.__start_time__) { let start = opts.__start_time__; let end = Date.now(); let timeUsed = end - start; let log = { route: msg.service, args: msg.args, time: utils.format(new Date(start)), timeUsed: timeUsed }; rpcLogger.info(JSON.stringify(log)); } next(); };