Use a common Tag for all your activities’ logging

It is pretty common for any non-trivial Android app to be composed of several Activities which together interact with the user for the furtherance of whatever is the objective of the app itself.


It is also pretty common, especially during the development phase, to have a need to emit logs so that one can check certain events happen when they are supposed to, and invariants, for example, are satisfied.


The Android ADT (Eclipse plugin) has a very nice feature in the form of the LogCat View that shows all the system logs, including those coming from your applications: as there may well be tens of apps running (and logging) at the same time (especially on a live device — as to why you should use a device for your testing and debugging, you may want to read this) the LogCat enables one to ‘filter in’ a subset, based on a ‘tag’.


The tag used by the LogCat is the value of the string used for the tag parameter in the Log.x() calls:

Log.d("myApp.tag", "All is going well");

It is pretty obvious that, in any given Activity, we would want a constant to use as the tag:

private static final String TAG = "myApp.tag";

but this has the drawback that we would have to either duplicate that same statment across all Activities (and, even worse, keep them all in sync if we decide, for whatever reason to change them) or resign ourselves to use different tags for different activities.


Alternatively, we could use a common ‘Constants’ class to keep, amongst others, a constant Tag:

public interface Constants {

public static final String TAG = "myApp.tag";
// other constants...
}

This is not very friendly, however, as every log statement (and there may well be hundreds in your code) would look something like:

Log(Constants.TAG, "All is going well");

it’s only 10 extra characters, but multiply that for several hundrends, and you’ve a veritable case of carpal tunnel syndrome on your hands (well, ok…).

A much better option is to use Android’s mechanism to store common application strings, which also gives the additional benefit that it’s dead easy to change the tag in one full swoop, should that ever be necessary: in one of the XML files in the res/values/ directory, have a line of the form

myApp.tag

and in your code, you can do something like:

public class MyActivity extends Activity {

public static String TAG; // note no 'final' here

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TAG = getResources().getString(R.string.TAG);
Log.d(TAG, "Starting User Preferences activity...");
// other initialization follows...
}
}

This can be done in each and every of your App’s activities, and then you can easily create a new filtered view in LogCat so that you can see only your App’s logs, from all the activities, also with the reassurance that, should ever want to change that, it’s just a one minute edit of the XML file

Advertisements
Posted in UncategorizedTagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s