How do you usually Tag log entries? (android)

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

    I usually create an App class that sits in a different package and contains useful static methods. One of the method is a getTag() method, this way I can get the TAG everywhere.
    App class looks like this:

    EDIT: Improved per br mob comment ( Thanks :) )

    public class App {
    
        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;
        }
    
    }
    

    And when I want to use it:

    Log.i(App.getTag(), "Your message here");
    

    The output of the getTag method is the name of the caller class (with the package name), and the line number where the getTag is called from, for easy debuging.

    0 讨论(0)
提交回复
热议问题