-
Notifications
You must be signed in to change notification settings - Fork 375
/
rpcLog.ts
45 lines (41 loc) · 1.18 KB
/
rpcLog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Filter for rpc log.
* Record used time for remote process call.
*/
import { getLogger } from 'pinus-logger';
let rpcLogger = getLogger('rpc-log', __filename);
import * as utils from '../../util/utils';
import {IRpcFilter} from 'pinus-rpc';
export class RpcLogFilter implements IRpcFilter
{
name = 'rpcLog';
/**
* Before filter for rpc
*/
before(serverId: string, msg: any, opts: any, next: (target?: Error | string, message?: any, options?: any) => void)
{
opts = opts || {};
opts.__start_time__ = Date.now();
next();
};
/**
* After filter for rpc
*/
after(serverId: string, msg: any, opts: any, next: (target?: Error | string, message?: any, options?: any) => void)
{
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();
};
}