From 55ea373cd03cedcb9651055772bd084658343fe5 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Wed, 11 Sep 2024 12:20:30 +0100 Subject: [PATCH] Intercept matrix_sdk logging via console --- src/settings/rageshake.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/settings/rageshake.ts b/src/settings/rageshake.ts index ea9805df..ffd9f333 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,28 @@ 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", "debug", "info", "warn", "error"] as ( + | "trace" + | "debug" + | "info" + | "warn" + | "error" + )[] + ).forEach((level) => { + if (!window.console[level]) return; + const prefix = `${level.toUpperCase()} matrix_sdk`; + const originalMethod = window.console[level]; + window.console[level] = (...args): void => { + originalMethod(...args); + if (typeof args[0] === "string" && args[0].startsWith(prefix)) { + global.mx_rage_logger.log(LogLevel[level], "matrix_sdk", ...args); + } + }; + }); + return tryInitStorage(); }