From d14b43487a8f2a4b10b637b5ba2ef1424b6fb4f1 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Mon, 23 Sep 2024 14:35:10 +0100 Subject: [PATCH] Intercept matrix_sdk logging via console and include in rageshake (#2623) Co-authored-by: Andrew Ferrazzutti --- src/settings/rageshake.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/settings/rageshake.ts b/src/settings/rageshake.ts index ea9805dfb..1ac9fba1c 100644 --- a/src/settings/rageshake.ts +++ b/src/settings/rageshake.ts @@ -467,6 +467,8 @@ declare global { */ export async function init(): Promise { global.mx_rage_logger = new ConsoleLogger(); + + // configure loglevel based loggers: setLogExtension(logger, global.mx_rage_logger.log); // these are the child/prefixed loggers we want to capture from js-sdk // there doesn't seem to be an easy way to capture all children @@ -474,6 +476,29 @@ export async function init(): Promise { setLogExtension(logger.getChild(loggerName), global.mx_rage_logger.log); }); + // intercept console logging so that we can get matrix_sdk logs: + // this is nasty, but no logging hooks are provided + [ + "trace" as const, + "debug" as const, + "info" as const, + "warn" as const, + "error" as const, + ].forEach((level) => { + const originalMethod = window.console[level]; + if (!originalMethod) return; + const prefix = `${level.toUpperCase()} matrix_sdk`; + window.console[level] = (...args): void => { + originalMethod(...args); + // args for calls from the matrix-sdk-crypto-wasm look like: + // ["DEBUG matrix_sdk_indexeddb::crypto_store: IndexedDbCryptoStore: opening main store matrix-js-sdk::matrix-sdk-crypto\n at /home/runner/.cargo/git/checkouts/matrix-rust-sdk-1f4927f82a3d27bb/07aa6d7/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs:267"] + if (typeof args[0] === "string" && args[0].startsWith(prefix)) { + // we pass all the args on to the logger in case there are more sent in future + global.mx_rage_logger.log(LogLevel[level], "matrix_sdk", ...args); + } + }; + }); + return tryInitStorage(); }