android.util.Log when publishing - what can I do / not do

前端 未结 3 530
终归单人心
终归单人心 2020-12-15 12:15

I\'ve got a hell of a lot of Log.i Log.d Log.e in my code for a recent app I\'ve done. I\'m about to publish this app and I don\'t really want people seeing it when they plu

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 12:33

    As Octavian points out inserting a logging constant would be the best way to do this. Writing a new class for this that calls the original logging methods if debugging is enabled is not a good idea.

    Good practice:

    if (C.D) { Log.d(C.T, "your log text here " + foo + bar); }
    

    Bad practice:

    YourLog.d("your log text here " + foo + bar);
    
    // and in YourLog.java's d() method:
    ... { if (debugging) Log.d(tag, text); }
    

    The first solution is very fast if the constant D of class C is false. If you have complex string operations for creating your logging string they will not be executed if debugging is deactivated. The compiler can even remove these operations at compile time if D is false, which may result in zero runtime overhead. The second (bad) solution will always build the whole string and call a method, which is overhead you don't need.

    In general the first solution would be best. And yes, I really call the class and members C, D and T (Constants/Debugging/Tag) - for performance reasons during typing. ;-)

提交回复
热议问题