java 在 Android Studio/Intellij idea 中自动生成 Android Log TAG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29378605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto-generate Android Log TAG in Android Studio/Intellij idea
提问by Sergey Shustikov
In Intellij Idea when i typing psfs
and then press Ctrl+JIDE was getting me a dialog :
在 Intellij Idea 中,当我输入psfs
然后按Ctrl+JIDE 时,我得到了一个对话框:
And when i press Enteri get an
当我按下Enter我得到一个
I know where i can customize my own output
我知道在哪里可以自定义我自己的输出
But i can't any doc's how i can write my own live template.
但我无法了解如何编写自己的实时模板。
In the end i want to get next result :
最后我想得到下一个结果:
Typing : psfst
-> press Ctrl+J-> press Enter
输入:psfst
-> 按Ctrl+J-> 按Enter
Result :
结果 :
public static final String TAG = <currentClassName>.class.getSimpleName();
public static final String TAG = <currentClassName>.class.getSimpleName();
It will be so helpfull, because i have a habit to log my classes.
这会很有帮助,因为我有记录课程的习惯。
回答by Sergey Shustikov
I find a solution
我找到了解决办法
1)Create a new live template in plain group
2)In template text :
1)在普通组中创建一个新的实时模板
2)在模板文本中:
private static final String TAG = $CLASS_NAME$.class.getSimpleName();
3)Define a usage scope :
3)定义使用范围:
4)Choose a shortcut :
4)选择快捷方式:
finally click on Edit variablesand change expression value to className()
最后单击Edit variables并将表达式值更改为 className()
Click Ok, Apply, Okand use.
点击Ok,Apply,Ok和使用。
回答by Ofek Ron
回答by Jorge Casariego
For those using Android Studio 3and Kotlinit is necessary to change how Live Templatesset:
对于使用Android Studio 3和Kotlin 的用户,有必要更改Live Templates 的设置方式:
Editor >> Live Templates >> AndroidLog
编辑器 >> 实时模板 >> AndroidLog
By default it is just for Java
默认情况下,它仅适用于 Java
Add Kotlin pressing "change" button and check Kotlin
按“更改”按钮添加 Kotlin 并检查 Kotlin
and after that it will work again!
之后它会再次工作!
Updated:2020
更新时间:2020
Android Studio:3.6.2
安卓工作室:3.6.2
Steps:
脚步:
- Add
- Call logtk are whatever you want to call
- Use this code as template text
- 添加
- 呼叫 logtk 是您想呼叫的任何内容
- 将此代码用作模板文本
private val TAG = this::class.java.simpleName
- Add a description
- Do it applicable to Kotlin: in my case I used just for Class
- 添加描述
- 是否适用于 Kotlin:就我而言,我仅用于 Class
回答by elabi3
Another solution is not use a default TAG for each class and use this method to get the TAG:
另一种解决方案是不对每个类使用默认 TAG 并使用此方法获取 TAG:
public class Utils {
public static String getTAG(Object o) {
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
int position = 0;
for (int i = 0; i < elements.length; i++) {
if (elements[i].getFileName().contains(o.getClass().getSimpleName())
&& !elements[i+1].getFileName().contains(o.getClass().getSimpleName())){
position = i;
break;
}
}
StackTraceElement element = elements[position];
String className = element.getFileName().replace(".java", "");
return "[" + className + "](" + element.getMethodName() + ":" + element.getLineNumber() + ")";
}
}
Example call from MainActivity - onResume:
来自 MainActivity 的示例调用 - onResume:
Log.v(Utils.getTAG(this), "hello world");
Log output:
日志输出:
[MainActivity](onResume:79): hello world