How do you usually Tag log entries? (android)

后端 未结 13 1579
佛祖请我去吃肉
佛祖请我去吃肉 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

    You could use this.toString() to get a unique identifer for the specific class in which you print to the log.

    0 讨论(0)
  • 2020-12-22 22:08

    they use Timber for the IOsched app 2019 to show debug info:

    implementation 'com.jakewharton.timber:timber:4.7.1'

    class ApplicationController: Application() {
    
    override fun onCreate() {  
        super.onCreate()
        if(BuildConfig.DEBUG){
            Timber.plant(Timber.DebugTree())
        }
    }   
    // enables logs for every activity and service of the application
    // needs to be registered in manifest like:  
     <application
        android:label="@string/app_name"
        android:name=".ApplicationController"
        ... >
    

    usage

      Timber.e("Error Message") 
      // will print ->  D/MainActivity: Error Message
    
      Timber.d("Debug Message");
      Timber.tag("new tag").e("error message");
    

    note that this makes the Logs available only during DEBUG state and facilitates you the task of removing them manually for the launch on Google Play -

    when release the app on the play store, we need to remove all Log statement from the app, so that none of the application data such as user information, hidden application data, auth-tokens are available to user in logcat as plain text

    check out this article https://medium.com/mindorks/better-logging-in-android-using-timber-72e40cc2293d

    0 讨论(0)
  • 2020-12-22 22:14

    I usually use the method name as the tag but from Thread

    String TAG = Thread.currentThread().getStackTrace()[1].getMethodName();
    

    This avoids the new Exception.

    0 讨论(0)
  • 2020-12-22 22:15

    Go to Android Studio -> preference -> Live Templates -> AndroidLog then select Log.d(TAG, String).

    In Template text replace

    android.util.Log.d(TAG, "$METHOD_NAME$: $content$");

    with

    android.util.Log.d("$className$", "$METHOD_NAME$: $content$");

    Then click Edit variables and enter className() in the Expression column next to the className Name column.

    Now when you type the shortcut logd it will put

    Log.d("CurrentClassName", "currentMethodName: ");
    

    You dont need to define a TAG anymore.

    0 讨论(0)
  • 2020-12-22 22:16

    I like to improve Yaniv answer if you have the log in this format (filename.java:XX) xx line number you can link the shortcut the same way gets linked when there's an error, this way I can get direct to the line in question just by click on the logcat

    I put this inside my extended Application so i can use in every other file

    public static String getTag() {
        String tag = "";
        final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
        for (int i = 0; i < ste.length; i++) {
            if (ste[i].getMethodName().equals("getTag")) {
                tag = "("+ste[i + 1].getFileName() + ":" + ste[i + 1].getLineNumber()+")";
            }
        }
        return tag;
    }
    

    Screenshot:

    0 讨论(0)
  • 2020-12-22 22:19

    For those users that visit this question:

    private val TAG:String = this.javaClass.simpleName;
    
    0 讨论(0)
提交回复
热议问题