Search code examples
androidandroid-logcatpublishingandroid-log

Android app publishing checklist


The android app publishing checklist says that I need to delete all Log statements from my code. Does that mean I need to delete them or would just commenting them out do? I would need those Log statements for updation and maintenance purpose later on so i don't want to delete them.Publishing checklist


Solution

  • You don't have to remove your logs from code, just disable them in RELEASE mode. The easiest way to achieve that, check mode before log something

     if (BuildConfig.DEBUG) {
        Log.i("TAG", "Log");
     }
    

    But it will be painful if you have a lot of logs in your code, so good idea is to write your custom wrapper above the Android Log class, with ability to turn on/off logging in one place, for example in Application class.

    Or you can use some smart loggers like Timber

    EDIT

    About log wrapper, to be honest I've never created such wrapper, I prefer to use Timber, but the simplest version may look like this

    public class Logger {
    
        public static void e(String tag, String body) {
            if (BuildConfig.DEBUG) {
                Log.e(tag, body);
            }
        }
    
        public static void v(String tag, String body) {
            if (BuildConfig.DEBUG) {
                Log.v(tag, body);
            }
    
        }
    
        ...
    }
    

    and instead of using

    Log.e("Tag", "Body");
    

    use

    Logger.e("Tag", "Body");