2021-02-04 19:15:23 +01:00
|
|
|
import log from 'loglevel';
|
2018-04-12 23:06:59 +02:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Returns a middleware that logs RPC activity
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2018-04-16 17:29:43 +02:00
|
|
|
* @param {{ origin: string }} opts - The middleware options
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function createLoggerMiddleware(opts) {
|
|
|
|
return function loggerMiddleware(
|
|
|
|
/** @type {any} */ req,
|
|
|
|
/** @type {any} */ res,
|
|
|
|
/** @type {Function} */ next,
|
|
|
|
) {
|
2018-04-16 17:29:43 +02:00
|
|
|
next((/** @type {Function} */ cb) => {
|
2017-09-14 00:17:26 +02:00
|
|
|
if (res.error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error('Error in RPC response:\n', res);
|
2017-09-14 00:17:26 +02:00
|
|
|
}
|
2019-11-20 01:03:20 +01:00
|
|
|
if (req.isMetamaskInternal) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
log.info(`RPC (${opts.origin}):`, req, '->', res);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
};
|
2017-10-21 21:06:39 +02:00
|
|
|
}
|