Intercept matrix_sdk logging via console

This commit is contained in:
Hugh Nimmo-Smith 2024-09-11 12:20:30 +01:00
parent 8e38d66611
commit 55ea373cd0

View File

@ -467,6 +467,8 @@ declare global {
*/
export async function init(): Promise<void> {
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<void> {
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();
}