How do you usually Tag log entries? (android)

后端 未结 13 1618
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 21:54

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 y

13条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 22:05

    I have created a class of Static variables, methods and classes named as S.

    The following is the logging method:

    public static void L(Context ctx, Object s) {
        Log.d("CCC " + ctx.getClass().getName().replace(ctx.getPackageName(), ""), s.toString());
    }
    

    It is called in any class as S.L(this, whaterver_object); The getClass().getName() also appends the package name, hence, I am removing it out to avoid making the tag unnecessarily long.

    Advantages:

    1. Shorter than Log.d(TAG,
    2. No need to convert int values to their string. Infact no need to type toString
    3. Won't forget to delete Log.d ever as I just have to delete the method and the locations of all logs get marked red.
    4. No need to define TAG at the top of the activity as it takes the name of the class.
    5. The TAG has a prefix of CCC (a short, easy to type string) so that it is easy to list only your logs in android monitor in Android Studio. Sometimes you are running services or other classes simultaneously. If you have to search by activity name alone then you cannot see exactly when a service response was obtained and then an action from your activity has occurred. A prefix like CCC helps as it gives you logs chronologically with the activity in which it occured

提交回复
热议问题