你通常如何tag的日志? (机器人)机器人、日志、tag

2023-09-04 10:49:24 作者:萌了个兔

我想大部分人都知道android.util.Log的 所有日志记录方法接受字符串变量作为第一个参数。

I assume most of you are aware of android.util.Log All logging methods accept 'String tag' as a first argument.

我的问题是如何你通常标记您的日志在你的应用程序? 我已经看到了一些硬code是这样的:

And my question is How do you usually tag your logs in your applications? I've seen some hardcode like this:

public class MyActivity extends Activity {
    private static final String TAG = "MyActivity";
    //...
    public void method () {
        //...
        Log.d(TAG, "Some logging");
    }
}

这看起来并不因为诸多原因很好的:

This doesn't look nice because of many reasons:

您可以告诉我这code没有硬code,但它的作用。 在我的应用程序可能有许多类在不同的包用相同的名字。因此,这将是很难读取日志 这是不灵活。你总是有把私有字段标签插入你的类

有什么巧妙的方法来获得一个TAG的一类?

Is there any neat way to get a TAG for a class?

推荐答案

我用一个标签,但我初始化它是这样的:

I use a TAG, but I initialise it like this:

private static final String TAG = MyActivity.class.getName();

当我修改我的$ C C标记$这种方式也将发生相应的变化。

This way when I refactor my code the tag will also change accordingly.