Search code examples
androidprintingconsolelogcat

How to print a string to logcat for testing?


I know this question has been asked before, but I can't find a clear answer. I'm new to Android Studio and the logcat console confuses me. I have a string date and I want to keep 'track' of it in the console so I know what value it has.

I tried commands like Log.i and console.log, but they don't seem to work for me.

  • Where should I write my print line code?
  • Where can I see it when it gets printed?
  • Do I have to run the whole app (on the emulator, or on a device) to print the line to the console?

Solution

  • Where do I write my print line code ?

    That is up to you.

    Where can I see it when it gets printed ?

    In LogCat:

    LogCat in Android Studio

    You will get this from the Android view (usually docked on the bottom edge on the left side). Note that I have sometimes encountered a bug where the automatically-applied filter for your app (right-hand drop-down list, above the output) seems to result in nothing showing up in the output. You may need to remove the filters, then adjust the output based on log level.

    You can also view LogCat at the command line via adb logcat and in the Android Device Monitor (Tools > Android > Android Device Monitor from the Android Studio main menu).

    Do I have to run the hole app (emulator or via a device) to print the line to the console?

    Yes.