Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use TAG in most of the Android logging code

Tags:

android

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");
    }
}  
like image 380
Cheok Yan Cheng Avatar asked Jun 28 '12 07:06

Cheok Yan Cheng


People also ask

What is a tag and priority associated with an Android log 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 ).

What is the use of logging in Android?

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.

What is logcat tag in Android?

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).

Why do so many Android apps have Log_Tag's?

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.


1 Answers

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"
like image 151
Arun George Avatar answered Sep 28 '22 04:09

Arun George