From da138106f2a05834db242b1691cb86f12d7ba1c3 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Mon, 4 Sep 2023 10:35:23 -0230 Subject: [PATCH] Reduce logging in production builds (#20694) Our logger middleware can be quite noisy in production, logging all RPC requests. It has been updated to have more condensed logs in production builds, but preserving the existing logging for development builds. --- app/scripts/lib/createLoggerMiddleware.js | 15 ++++++++++++--- .../createMethodMiddleware.js | 8 ++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/scripts/lib/createLoggerMiddleware.js b/app/scripts/lib/createLoggerMiddleware.js index 0a6706842..86703c459 100644 --- a/app/scripts/lib/createLoggerMiddleware.js +++ b/app/scripts/lib/createLoggerMiddleware.js @@ -1,7 +1,8 @@ import log from 'loglevel'; /** - * Returns a middleware that logs RPC activity + * Returns a middleware that logs RPC activity. Logging is detailed in + * development builds, but more limited in production builds. * * @param {{ origin: string }} opts - The middleware options * @returns {Function} @@ -14,12 +15,20 @@ export default function createLoggerMiddleware(opts) { ) { next((/** @type {Function} */ cb) => { if (res.error) { - log.error('Error in RPC response:\n', res); + log.debug('Error in RPC response:\n', res); } if (req.isMetamaskInternal) { return; } - log.info(`RPC (${opts.origin}):`, req, '->', res); + if (process.env.METAMASK_DEBUG) { + log.info(`RPC (${opts.origin}):`, req, '->', res); + } else { + log.info( + `RPC (${opts.origin}): ${req.method} -> ${ + res.error ? 'error' : 'success' + }`, + ); + } cb(); }); }; diff --git a/app/scripts/lib/rpc-method-middleware/createMethodMiddleware.js b/app/scripts/lib/rpc-method-middleware/createMethodMiddleware.js index e0f1b87f4..842cee101 100644 --- a/app/scripts/lib/rpc-method-middleware/createMethodMiddleware.js +++ b/app/scripts/lib/rpc-method-middleware/createMethodMiddleware.js @@ -65,7 +65,9 @@ export function createMethodMiddleware(hooks) { selectHooks(hooks, hookNames), ); } catch (error) { - console.error(error); + if (process.env.METAMASK_DEBUG) { + console.error(error); + } return end(error); } } @@ -101,7 +103,9 @@ export function createSnapMethodMiddleware(isSnap, hooks) { selectHooks(hooks, hookNames), ); } catch (error) { - console.error(error); + if (process.env.METAMASK_DEBUG) { + console.error(error); + } return end(error); } }