By default you can log messages and debug information to the console, through functions like console.log. This has a big downside, which is that log messages are not preserved between reboots, and data might get lost if the debugger is not attached. This article shows how to log to a file instead.
We want to keep using the console functions, avoiding to change your current coding style. We just need to intercept the calls being made to the console, and besides showing it in the console, also log it to a file. This snippet (from tobyho.com) intercepts calls to the console:
function overwriteConsole() {
var console = window.console;
function intercept(method) {
var original = console[method];
console[method] = function() {
original.apply(console, arguments);
// we can do something with the data here...
};
}
var methods = [ 'log', 'warn', 'error' ];
for (var i = 0; i < methods.length; i++)
intercept(methods[i]);
}
Now that we have a way of intercepting logging calls it's time to log it to a file. In general you'd want to create a new /logs folder on the device and log there. So create a new directory before we intercept the calls using the filesystem API.
navigator.mozOs.createDirectory('/data/local/logs')
.then(overwriteConsole)
.catch(err => console.error('Could not create "/data/local/logs" folder'));
All is been set up now. Let's serialize all calls to console and store them in a file (per-day) in the new log folder.
console[method] = function() {
original.apply(console, arguments);
var logFolder = '/data/local/logs';
var logFile = logFolder + '/' + new Date().toISOString().substr(0, 10) + '.log';
var line = new Date() + ' ' + method.toUpperCase() + ' ' +
args.map((a, ix) => {
var v;
try {
v = JSON.stringify(a);
}
catch (ex) {
v = a + '';
}
return '[' + ix + '] ' + v;
}).join(', ') + '\n';
navigator.mozOs.appendFile(logFile, line, 'utf-8')
.catch(err => {
console.error('Appending to logFile failed', err);
});
};
And done. Log something to the console to test it out. To pull the files off of the device, run:
$ adb pull /data/local/logs/ .