Google Source Code--New Techniques for Log ging

Keywords: Android Google Programming Retrofit

Introducer

Recently, I started to read the Google source code to see which codes Daniel wrote are well written, which can be used for reference under their own projects. I found that Google engineers are typing logs in a unified way. Today, let's share how their Log code is written.

1. Upper Source Code First

    package com.android.music;
    import android.os.Debug;
    public class MusicLog {    
      private static final boolean DEBUG = true;    
      public static void v(String tag, String msg) {        
       if(DEBUG) {
            android.util.Log.v(tag, msg);
        }
    }    
       public static void v(String tag, String msg, Throwable tr) {        
       if(DEBUG) {
            android.util.Log.v(tag, msg, tr);
        }
    }    
       public static void d(String tag, String msg) {        
       if(DEBUG) {
            android.util.Log.d(tag, msg);
        }
    }    
       public static void d(String tag, String msg, Throwable tr) {        
       if(DEBUG) {
            android.util.Log.d(tag, msg, tr);
        }
    }    
       public static void i(String tag, String msg) {        
       if(DEBUG) {
            android.util.Log.i(tag, msg);
        }
    }    
       public static void i(String tag, String msg, Throwable tr) {        
       if(DEBUG) {
            android.util.Log.i(tag, msg, tr);
        }
    }    
       public static void w(String tag, String msg) {        
       if(DEBUG) {
            android.util.Log.w(tag, msg);
        }
    }    
       public static void w(String tag, String msg, Throwable tr) {        
       if(DEBUG) {
            android.util.Log.w(tag, msg, tr);
        }
    }    
       public static void w(String tag, Throwable tr) {        
       if(DEBUG) {
            android.util.Log.w(tag, tr);
        }
    }    
       public static void e(String tag, String msg) {        
       if(DEBUG) {
            android.util.Log.e(tag, msg);
        }
    }    
       public static void e(String tag, String msg, Throwable tr) {        
       if(DEBUG) {
            android.util.Log.e(tag, msg, tr);
        }
    }

}

2. How to Use

private static final String LOGTAG = "LyricListView";
MusicLog.d(LOGTAG, "mAdapter.getCount = " + mAdapter.getCount());
MusicLog.e(LOGTAG, "SET LYRIC ERROR:" + e.getMessage());

3. Analyzing the Benefits of Writing Like This

In the above code, we use private static final boolean DEBUG = true; we can control the output and closure of log uniformly. If we print the log information in this way, we can output the log in the debug version and close the log output in the release version. This prevents your software information from leaking out. Moreover, if this method is adopted, will the format of the printed log information of the code be simpler, more beautiful and more uniform? Now that you have a new skill in printing logs, don't you rush to use it for your project?

4, summary

Today's Google source code reading, let's start with this simple and practical method of printing logs, which I personally feel is very useful. Next time continue to share excellent source code, welcome to continue to pay attention to this public number.
This article belongs to the original, if reproduced, please mark the original author, the copyright belongs to this public number. If you like my articles, please pay attention to the IT circle. Welcome to continue to pay attention to the technical blog of this public number. If you think this article is helpful to you, please give me some praise or appreciation. Your support is my motivation to stick to originality.~~

Finally, if you want to write public numbers and friends who love programming, I have set up a technology Wechat group, which can be public numbers, program IT circle: reply to "plus group", welcome you to study in the group.~

Recommended reading
Welfare: Learn programming videos for free

Full Analysis of Common Annotations of Retrofit

Five Steps to Build Personal Websites Quickly

Posted by jrolands on Wed, 22 May 2019 10:46:37 -0700