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.
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:
- 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:
Pros:
- Precious milliseconds and keystrokes saved! Links are clickable and take you directly to the place where the log is printed. 🎉
- Log tags are freed up and can be used to indicate the context of the log messages instead of the class name.
Cons:
- Log lines are longer due to the
.([filename].java:[line])
boilerplate. - 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)