Skip to content

Instantly share code, notes, and snippets.

Last active March 14, 2023 19:06
What would you like to do?
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 (! {
const logFile = `${plugin.manifest.dir}/logs.txt`;
const logs: string[] = [];
const logMessages = (prefix: string) => (...messages: unknown[]) => {
for (const message of messages) {
}, logs.join(" "));
console.debug = logMessages("debug");
console.error = logMessages("error"); = logMessages("info");
console.log = logMessages("log");
console.warn = logMessages("warn");
Copy link

@AndrewNatoli Since this script doesn't run until after the plugin is loaded, it won't help you track down initialization issues. For those, I recommend taking a look at your package dependencies and make sure that all the packages can run in a browser environment (as opposed to a node environment).

If you're still having trouble, another option would be loading this script from a separate plugin. You can do this either by creating an entirely barebones plugin that just calls this script, or making this a startup script with any of the existing plugins that allow for running arbitrary javascript (Templater, Dataview, CustomJS all come to mind).

That way, you can have this script run before trying to enable the plugin you're trying to debug.

Copy link

@liamcain That suggestion was a huge help, thanks!

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:
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