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 your logs in your applications? I've seen some hardcode like this:
public class MyActivity extends Activity {
private static final String TAG = "MyActivity";
//...
public void method () {
//...
Log.d(TAG, "Some logging");
}
}
This doesn't look nice because of many reasons:
- You can tell me this code doesn't have hardcode, but it does.
- My application could have any number of classes in different packages with the same name. So it would be hard to read the log.
- It isn't flexible. You always have put a private field TAG into your class.
Is there any neat way to get a TAG for a class?
AndroidStudio has a
logt
template by default (you can typelogt
and press tab to have it expand to a sinppet of code) . I recommend using this to avoid copy pasting the TAG definition from another class and forgetting to change the class you're referring to. The template expands by default toprivate static final String TAG = "$CLASS_NAME$"
To avoid using the old class name after refactoring you could change that to
private static final String TAG = $CLASS_NAME$.class.getSimpleName();
Remember to check the "Edit variables" button and make sure that the
CLASS_NAME
variable is defined to use theclassName()
Expression and has "Skip if defined" checked.I use a TAG, but I initialise it like this:
This way when I refactor my code the tag will also change accordingly.
At the expense of updating these strings when I move code between methods or rename methods, I like doing the following. Philosophically it also seems to be better to keep "location" or "context" in the tag, not the message.
The benefit here is that you can filter out a single method even if the content isn't static, e.g.
The only drawback is that when I rename
f()
tog()
I need to keep that string in mind. Also, automatic IDE refactoring won't catch these.For a while I was a fan of using the short class name, I mean
LOG_TAG = MyClass.class.getSimpleName()
. I found them harder to filter in the logs because there was less to go on.You could use
this.toString()
to get a unique identifer for the specific class in which you print to the log.It is a very old question, but even thought an updated answer for July 2018 it is more preferable to use Timber. In order to Log the correct logging, errors and warns can be send to third party crash libraries, such as Firebase or Crashlytics.
In the class that implements Application you should add this:
Do not forget Timber dependency.
Go to Android Studio -> preference -> Live Templates -> AndroidLog then select Log.d(TAG, String).
In Template text replace
android.util.Log.d(TAG, "$METHOD_NAME$: $content$");
with
android.util.Log.d("$className$", "$METHOD_NAME$: $content$");
Then click Edit variables and enter className() in the Expression column next to the className Name column.
Now when you type the shortcut
logd
it will putYou dont need to define a TAG anymore.