I can see this is common practice among Android developers.
public final class TasksSample extends ListActivity {
private static final String TAG = "TasksSample";
private void method() {
Log.i(TAG, "message");
}
}
Will it be easier, if I do it this way? I need not to declare TAG for every new class.
public final class TasksSample extends ListActivity {
private void method() {
Log.i(getClass().getName(), "message");
}
}
Every Android log message has a tag and a priority associated with it. The tag of a system log message is a short string indicating the system component from which the message originates (for example, ActivityManager ).
In Android, Logging works as a diagnostic technique used by developers. It basically provides an insight of what’s happening in your application. We can write Log messages in the code with the help of LOG class and the messages get displayed in the Logcat window on running the application.
Logcat message format Every Android log message has a tag and a priority associated with it. The tag of a system log message is a short string indicating the system component from which the message originates (for example, ActivityManager).
If you develop Android apps, you probably have tons of LOG_TAG's like: class MySuperClass { static final String LOG_TAG = "MySuperClass"; // ... } Because that's what you're supposed to do, and that's what you see in Android's own code.
Rather than writing getClass().getName()
at each place where a log is placed in a particular activity, it is always preferred to have a TAG
that would represent the name of the activity class.
Why use TAG?
When you are running your application there might be more than one Activity class in it. To distinguish which activity class has logged the information in logcat
we use a TAG
which of course represents the name of the class.
And the proper way (I am not saying what you have written is wrong) of writing the TAG is:
private static final String TAG = TasksSample.class.getSimpleName(); // and not "TasksSample"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With