Want another way to debug Cordova/PhoneGap apps? This isn't new, but I tend to forget about this option and it came in handy yesterday so I thought I would share. When you send builds to the simulator/device via the command line, you may notice that at the end of all the output about building this and generating that, you get these two lines:
2014-07-14 17:23:36.846 ios-sim[1335:507] stderrPath: /Users/ray/readtextfile/platforms/ios/cordova/console.log<br/>
2014-07-14 17:23:36.847 ios-sim[1335:507] stdoutPath: /Users/ray/readtextfile/platforms/ios/cordova/console.log
What this is telling you is that you have a log file that will report on errors from your application. This also includes console.log output. (As well as console.dir.) If you simply tail -f
this file in another terminal tab (and yes, you get tail -f in Windows too with the right download), you get your console output as plain text right in your terminal. Here is an example from what I was working on yesterday.
Edit on July 16: As just an FYI, when I just tested this on a new app this morning, it did not work. I had to add the Console plugin. My initial test was with a Ionic app where this was added automatically.
Archived Comments
Hi Raymond,
do you know what is the difference between that and the adb logcat for Android purpose?
Nope, sorry.
Ray, the most useful snippet I found for debugging phonegap is:
window.onerror = function(msg,url,line){
console.log('window.onerror :',msg,url,line);
};
Nice one. If you do use remote debugging, errors show up in the console already, but this would work for the log based console.
Actually - let me confirm errors don't automatically log.
Ok, so yes, errors are NOT reported to the console log. As an interesting side note, in my first test I added an error to the event handler run by deviceready. I had,
console.log('pre bug');
doX();
console.log('post bug');
I assumed I'd see pre bug, and MAYBE an error. But I saw *nothing*. I had to move my code to another function called after the event handler to confirm what I found above.
So it seems like if something goes wrong in the eventhandler for deviceready, nothing is logged. May be worth my time to blog that just as an FYI.
The latest PG Developer App includes a feature to remotely log calls to console.log()/.warn()/.error() etc to the 'phonegap serve' server via socket.io .
https://github.com/phonegap...
I had some pain getting this to install on Windows as you need a version of Visual Studio installed to make socket.io work. Here's one place that I posted some help with this ...
http://stackoverflow.com/qu...
Thank you very much for posting this very useful tip! It came quite in handy in my day work!