Taming the LogCat
Written by Xavier Gouchet - 30 january 2013 - no comments
A couple of days ago, a question popped on StackOverflow, asking how to link a log line in LogCat to the source code, just like it happens when you Log an exception.
The first answers where to add a fake Exception. Naturally, there will be the line you want, but also a lot of noise in your LogCat.
Then someone proposed a method to simulated an exception Log, without the whole stacktrace (see the answer here).
The solution works fine, but can be quite verbose. With a little bit of try and error, I found out that the most important part is the "at (file.java:42)". It doesn't matter where it is in the message. You can even have mor than one, it only takes into account the last one.
So I worked out a LogLink class to use this systme anywhere and seamlessly. Here's an example of how it works :
// can be one of INSERT_FIRST, INSERT_LAST or INSERT_NONE
// default value is INSERT_LAST, and you can call it only once
LogUtils.setInsertMode(LogUtils.INSERT_LAST);
// also exist as v, d, w, e and wtf
LogUtils.i("MyClass", "A message");
The previous code will output the following output
MyClass A message at (MyClass.java:42)
You can see and download the LogUtils class here