Often you want to output debugging data from your app, but the space on the screen is limited! And perhaps you don't want to connect to the app with WebIDE and launch the debugger and etc, etc...
One solution is to use any of the console. functions in your code. They will be sent to the device's log. For example:
and you can also use console.error, console.info, etc.
Then, if you have the adb utility installed, you can get instant access to those logs when your device is connected to your computer with USB.
You can get adb from the Android Developer Toolkit or if you just want that binary, with package managers such as brew on a Mac:
brew install android-platform-tools
Once adb is installed, if you execute this in a terminal:
it will start to print everything that is sent to the device's log. This includes your app's messages AND EVERYTHING ELSE! It's like a huge kitchen sink where all the debug data goes.
I/HelloApp(21456): Content JS LOG: hello friends I/HelloApp(21456): at hello (app://5ae38330-dde0-11e4-9397-fd926d95d498/js/app.js:87:4) D/wpa_supplicant( 900): RX ctrl_iface - hexdump(len=11): 53 49 47 4e 41 4c 5f 50 4f 4c 4c D/wpa_supplicant( 900): wlan0: Control interface command 'SIGNAL_POLL'
Although sometimes you want to see the whole bunch of messages, more often than not you're just interested in your app's messages. You can use grep to filter the output of adb logcat. Stop the current process with CTRL+C and then type this:
adb logcat | grep HelloApp
I/HelloApp(21456): Content JS LOG: hello friends I/HelloApp(21456): at hello (app://5ae38330-dde0-11e4-9397-fd926d95d498/js/app.js:87:4)
What we're saying is: only show me lines that contain HelloApp. This depends on your app's name, so adjust accordingly---if you enter the wrong argument for grep, you won't see anything at all ;-)
And what if you connect multiple devices...?
When you connect multiple devices and run adb logcat, you get this message:
error: more than one device and emulator
adb doesn't know what do you actually want to see, so it just resorts to not showing you anything.
To tell it what you want to look at, you need to find the label for each device first:
This will produce a list similar to this:
List of devices attached 3739ce99 device 356cd099 device
Where the first column represents each device's label. Then you can use that to filter when calling adb logcat, like this:
adb -s 3739ce99 logcat | grep HelloApp
You could also open another terminal window and run logcat on it, but for another device, simultaneously.
Saving a log to a file
Often when you file a bug you're asked to produce a log. You can create one by redirecting the output of adb to a file! The way to do this is to go to a terminal, and type:
adb logcat > debug.txt # or also... adb -s 356cd099 logcat > debug.txt
Instead of outputting the logs to the screen, they will be stored in debug.txt until you press CTRL+C. You should run the steps to reproduce the bug while adb is logging to debug.txt, and then stop it afterwards, and then you can append said debug.txt file to the bug in bugzilla--it contains valuable debug information, specially if you don't filter by your app name!
And there are more useful adb bits and pieces on this MDN page.
Happy logging ;-)