Skip to content

Instantly share code, notes, and snippets.

@liamcain
Last active April 22, 2024 05:57
Show Gist options
  • Save liamcain/3f21f1ee820cb30f18050d2f3ad85f3f to your computer and use it in GitHub Desktop.
Save liamcain/3f21f1ee820cb30f18050d2f3ad85f3f to your computer and use it in GitHub Desktop.
Save console messages to logfile for mobile debugging
declare module "obsidian" {
interface App {
isMobile: boolean;
}
}
// Call this method inside your plugin's `onLoad` function
function monkeyPatchConsole(plugin: Plugin) {
if (!plugin.app.isMobile) {
return;
}
const logFile = `${plugin.manifest.dir}/logs.txt`;
const logs: string[] = [];
const logMessages = (prefix: string) => (...messages: unknown[]) => {
logs.push(`\n[${prefix}]`);
for (const message of messages) {
logs.push(String(message));
}
plugin.app.vault.adapter.write(logFile, logs.join(" "));
};
console.debug = logMessages("debug");
console.error = logMessages("error");
console.info = logMessages("info");
console.log = logMessages("log");
console.warn = logMessages("warn");
}
@AndrewNatoli
Copy link

@liamcain That suggestion was a huge help, thanks!

@velebit
Copy link

velebit commented Mar 14, 2023

Thank you for this! I needed to debug a loading issue on mobile, so I did package this as a plugin:
https://github.com/velebit/obsidian-save-console-log
The plugin eliminates the check for isMobile, since you can just not enable the plugin if you don't want hot patching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment