I assume most of you are aware of android.util.Log All logging methods accept \'String tag\' as a first argument.
And my question is How do you usually tag y
I usually create an App
class that sits in a different package and contains useful static methods. One of the method is a getTag()
method, this way I can get the TAG everywhere.
App
class looks like this:
EDIT: Improved per br mob comment ( Thanks :) )
public class App {
public static String getTag() {
String tag = "";
final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
for (int i = 0; i < ste.length; i++) {
if (ste[i].getMethodName().equals("getTag")) {
tag = "("+ste[i + 1].getFileName() + ":" + ste[i + 1].getLineNumber()+")";
}
}
return tag;
}
}
And when I want to use it:
Log.i(App.getTag(), "Your message here");
The output of the getTag
method is the name of the caller class (with the package name), and the line number where the getTag
is called from, for easy debuging.