Setting up console debugging for PhoneGap and Android

This post is more than 2 years old.

In case you haven't figured it out yet, debugging in mobile is "sub-optimal". (Whatever you do - don't do a Google Images search on sub-optimal.) Brian Leroux has an epic presentation on the topic and I highly encourage taking a look through it. I thought I'd share how I'm debugging in PhoneGap and Android right now.

To be clear - this is a sucky way of doing it. "Sub-optimal" is being too nice. Whenever possible I do as much work as I can on the desktop. But when I get desperate and need to test something on a device, I end up resorting to console.log messages. I know this is only one step better than a bunch of alerts, but I thought it might be useful to show how this is done with Android and some tips to make it a bit more palatable.

First - I assume that you are actually using the Android SDK to create your APK files and install. What I'm going to show will work if you use Build to generate your APK, but the tool comes from the SDK. Open up your SDK folder, go into the tools subdirectory, and run "ddms". This stands for "Dalvik Debug Monitor." Here's a screen shot. Notice it has my device in the upper left hand corner.

You will also notice an ungodly amount of messages in the main log panel. This is everything your device is doing. Everything. We'll filter that in a second.

So start up your application, and you should notice the process is now listed below the device. My application had an ID of com.camden.bare, and this is what shows up when I run it.

The first thing you want to do is create a filter. In order to actually see your messages in all the noise your device is generating, you need to create a filter by hitting the pretty little green plus sign.

In the form that pops up, give it any name you want, but be sure to use the 'by Log Tag' field and specify "Web Console":

Ok, at this point, you can now see messages when your application makes use of console.log. Consider this simple PhoneGap/Camera application:

I've got a few console messages for my events. They aren't incredibly helpful, but they let me know things are progressing as I expect. Here's a quick example:

What's cool is that it will also log unexpected errors for you. I wish I could say I planned this, but I did make a mistake in my "simple" little application and I saw it immediately in ddms:

In general, that's it, but here are a few more tips to keep in mind:

  1. I find that I have to click on my filter to 'refresh' the list view. Remember that if you don't see the panel updating.

  2. console.dir "works" in that it prints out to the log, but it's not a nice interactive tree like you see in Firebug. If you use that on a complex object, or even console.log, you pretty much end up with [object Object]. Instead, use JSON: console.log(JSON.stringif(something));

  3. If you make use of the tip above and find that log message is super long and hard to read, then mouse over the log line and wait. A tooltip will appear with the message in a smaller, easier to read format.

I hope this helps. Again - I'm not putting this out as a great way to debug. It "works" now but could certainly be a heck of a lot better.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Mike posted on 5/10/2012 at 9:12 PM

I have never used this before :) I will have to give it a shot.

I usually end up using the debug in build with weinre it make it really simple to just build and test it our online.

Thanks for the info though I will have to give this a shot because weinre can be a little buggy with IOS for some reason

Comment 2 by Adam Tuttle posted on 5/10/2012 at 9:14 PM

If you use PhoneGap:Build there is an option to enable debugging, which embeds Weinre in your application. Then you simply click the "debug" button on your application's page (in PG:B) and that opens up the computer-side of the Weinre session. Boom, console'd.

Comment 3 by Raymond Camden posted on 5/10/2012 at 9:15 PM

I've had no luck with Weinre and Android on Windows, but I know it works wonders for others. I don't believe though that you can do console debugging, can you?

Comment 4 by Raymond Camden posted on 5/10/2012 at 9:15 PM

Ah so console does work? I thought just the DOM stuff worked.

Comment 5 by Raymond Camden posted on 5/10/2012 at 9:19 PM

Yeah, I thought I remembered testing this. You don't get console.log messages in it. You get a console, and I assume you can manually do things, but you don't get logging.

Comment 6 by Adam Tuttle posted on 5/11/2012 at 4:20 PM

I use the logging almost every day. You definitely get console.log messages in the console when you let PG:B inject Weinre into your app for you.

Maybe it's not "stock" Weinre (so if you're doing it manually), but whatever they're doing when I enable debugging in PG:B, I get console.log messages.

Comment 7 by Raymond Camden posted on 5/11/2012 at 5:47 PM

Ah, I was using Weinre via Shadow, not via the script include. I've only had luck with Weinre via Shadow, never via injection - or not reliably. Again though, I've heard on OSX it works perfectly fine.

Comment 8 by Adam Tuttle posted on 5/11/2012 at 5:59 PM

Maybe that's it, I'm on OSX. It's really easy to use in PG:B, you just check a box and it re-builds your app with weinre included, and then there's a link on the site to open up the debug console.

Comment 9 by kim_ras posted on 5/11/2012 at 7:37 PM

Yep.. i was struggeling alot when I started "porting"to iPhone..
It showed up that it failed on a message..

This is what I found:
// NOTE.. iOS only support Log,Error,warning
// Android supports Log,Error,Warning,Info,Debug

Comment 10 by Chuy posted on 3/24/2014 at 10:34 PM

I'm testing on a chrome-default-browser device (moto g) and the console tag is "chromium", not "Web Console".


Comment 11 by Rafael posted on 5/30/2014 at 4:27 PM

Thank you!

Comment 12 by Shivam01 posted on 10/14/2015 at 12:49 PM

If i want to get console.og on command prompt of mac, so is there any command for that.


Comment 13 (In reply to #12) by Raymond Camden posted on 10/14/2015 at 2:56 PM

Maybe via Android's command line tools. I know you can monitor it via the "monitor" app which probably has a CLI back end.

Comment 14 (In reply to #13) by Shivam01 posted on 10/15/2015 at 12:38 PM

'monitor' ,is this application ?

Comment 15 (In reply to #14) by Raymond Camden posted on 10/15/2015 at 12:57 PM

Oh it's just one of the Android SDK tools.