Photo by Kevin on Unsplash

Android Studio Pro Tip: go to source from logcat output

TL;DR

Use .([filename].java:[line]) in your log statements to make them clickable in Android Studio and Intellij IDEA.


Your good old logcat output

Have you ever looked at the log output of your app and wondered where exactly the log lines are generated? The process for me used to be:

  1. Press cmd+o to open the “Navigate to class..” prompt and type in the tag from the log line (I’m almost always using class names as log tags. I guess most of us do, right?) to open the class where the log line is printed.

2. Press cmd+f to open the find prompt and type in the relevant log line.

3. Hope that this log line is printed in only one place inside this class 🙏 …

But not anymore — my logcat output now looks like this:

Turbocharged logcat output :)

Pros:

  1. Precious milliseconds and keystrokes saved! Links are clickable and take you directly to the place where the log is printed. 🎉
  2. Log tags are freed up and can be used to indicate the context of the log messages instead of the class name.

Cons:

  1. Log lines are longer due to the .([filename].java:[line]) boilerplate.
  2. Getting the method name & line number via throwing a new exception for every log statement causes additional overhead so you don’t want to use this for release builds.

If you’re using Timber for logging then here’s a Tree that’ll prefix your messages with the correctly formatted links to source:

In case you’re using android.util.Log (or any other logging library) then run your log messages through LogUtil.prependCallLocation(message)

Example of generating the format understood by AS (ripped out relevant parts from Timber)
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.