Is there a way to print to the console in an Android app?
Asked Answered
L

2

28

Can I run an Android app through the emulator and make it print strings to my computer's console? By console I mean the standard place you would expect to see a System.out.println() in a normal java application. So if you ran the java application from the command prompt then you will see the println()s in the command prompt or if you ran the program in eclipse you will see it in the Console tab at the bottom.

Licit answered 23/6, 2010 at 14:18 Comment(1)
LogCat is available to you in Eclipse via the DDMS perspective.Smitt
W
36

Use Log.d("YourTag", "YourOutput");

see http://developer.android.com/reference/android/util/Log.html

Weese answered 23/6, 2010 at 14:30 Comment(3)
There are various methods for various importance of logs: Log.v(), Log.d(), Log.i(), etc... Note also that API Level 8 (Android 2.2 Froyo) introduced brand new method Log.wtf() - What a Terrible Failure ;) LOLDaguerre
To avoid having to keep track of a tag I like: Log.d(getClass().getSimpleName(), "YourOutput");Joeljoela
@Joeljoela Yes of course. I just wanted to give a code example without any dependency on any other variable / code, etc. Since it's more about the Log class itself. I would even go further and use getClass.getName() instead of getSimpleName(), since, often you want to filter by package (your app) as well, not just one class. With getName(), you can do both - that's why I use getName() instead. +1 yours.Weese
L
16

By default, the Android system sends stdout and stderr (System.out and System.err) output to /dev/null. In processes that run the Dalvik VM, you can have the system write a copy of the output to the log file. In this case, the system writes the messages to the log using the log tags stdout and stderr, both with priority I.

To route the output in this way, you stop a running emulator/device instance and then use the shell command setprop to enable the redirection of output. Here's how you do it:

$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start

The system retains this setting until you terminate the emulator/device instance. To use the setting as a default on the emulator/device instance, you can add an entry to /data/local.prop on the device.

You can find more information on this in the Android Debug Bridge document.

You could also create your own class to print to the console http://tech.chitgoks.com/2008/03/17/android-showing-systemout-messages-to-console/

I think this question has been answered on StackOverflow already How to output LogCat to Console?

Leventis answered 23/6, 2010 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.