Why use TAG in most Android registration codes

I see that this is a common practice among Android developers.

public final class TasksSample extends ListActivity { private static final String TAG = "TasksSample"; private void method() { Log.i(TAG, "message"); } } 

Would it be easier if I do it like this? I do not need to declare a TAG for each new class.

 public final class TasksSample extends ListActivity { private void method() { Log.i(getClass().getName(), "message"); } } 
+4
source share
4 answers

Instead of writing getClass().getName() in every place where the log is placed in a specific activity, it is always preferable to have a TAG that will represent the name of the activity class.

Why use TAG?

When you run the application, it can have more than one Activity class. To distinguish which activity class has logged information, we use TAG , which of course represents the name of the class.

And the correct way (I'm not saying that you spelled incorrectly) about writing TAG:

 private static final String TAG = TasksSample.class.getSimpleName(); // and not "TasksSample" 
+12
source

a function call every time it is paid, and getClass().getName() calls 2 functions every time you log in (already a long process).

Therefore, it is better to save the tag instead of calling the same function again and again.

+1
source

Each previous answer is right, but I just want to add a little comment.

 private static final String TAG = TasksSample.class.getSimpleName(); 

or

 private static final String TAG = "TasksSample" 

The latter is used when you use proguard . As you know, proguard obfuscates class names and also affects logs.

+1
source

Yes, this is common practice and is maintained by Google for logging and debugging. If you use getClass().getName() , then you should call getClass().getName() each time, so its best approach uses TAG.

In fact, getClass().getName() returns the name of the class, where TAG is the easily understandable name / identity of your class.

0
source

All Articles