How do you usually Tag log entries? (android)
I use a TAG, but I initialise it like this:
private static final String TAG = MyActivity.class.getName();
This way when I refactor my code the tag will also change accordingly.
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.