只需要输入logi 就可出现以下代码
/// tag(类名.函数名)
LogUtil.i(index, tag: '_MyHomePageState.onItemClick:');
打印日志效果如下:
实现上面效果步骤如下:
一、封装log_util.dart
1 /// 2 /// Log工具类:打印日志相关 3 /// 4 /// @author zony 5 /// @time 2022/4/7 14:49 6 class LogUtil { 7 /// 默认日志TAG 8 static const String _TAG_DEF = "LogUtil: "; 9 10 /// 是否打开输出日志,true:log输出 11 static bool isOpenLogDef = true; 12 13 /// 日志TAG 14 static String TAG = _TAG_DEF; 15 16 /// 运行在Release环境时,inProduction为true; 17 /// 当App运行在Debug和Profile环境时,inProduction为false。 18 static const bool inProduction = bool.fromEnvironment("dart.vm.product"); 19 20 /// 21 /// 初始化log 22 /// 23 /// [isOpenLog] 是否打开日志 24 /// [tag] tag标识 25 /// @author zony 26 /// @time 2022/4/7 14:45 27 static void init({bool isOpenLog = false, String tag = _TAG_DEF}) { 28 isOpenLogDef = isOpenLog; 29 TAG = tag; 30 } 31 32 /// 33 /// 打印INFO日志 34 /// 35 /// [object] 打印object内容 36 /// [tag] tag标识 37 /// @author zony 38 /// @time 2022/4/7 14:47 39 static void i(Object object, {String tag = _TAG_DEF}) { 40 _printLog(tag, '[I]', object); 41 } 42 43 /// 44 /// 打印警告日志 45 /// 46 /// [object] 打印object内容 47 /// [tag] tag标识 48 /// @author zony 49 /// @time 2022/4/7 14:47 50 static void w(Object object, {String tag = _TAG_DEF}) { 51 _printLog(tag, '[W]⚠️', object); 52 } 53 54 /// 55 /// 打印错误日志 56 /// 57 /// [object] 打印object内容 58 /// [tag] tag标识 59 /// @author zony 60 /// @time 2022/4/7 14:47 61 static void e(Object object, {String tag = _TAG_DEF}) { 62 _printLog(tag, '[E]⛔', object); 63 } 64 65 /// 66 /// 打印debug日志 67 /// 68 /// [object] 打印object内容 69 /// [tag] tag标识 70 /// @author zony 71 /// @time 2022/4/7 14:47 72 static void d(Object object, {String tag = _TAG_DEF}) { 73 _printLog(tag, "[D]", object); 74 } 75 76 /// 77 /// 输出日志 78 /// 79 /// [tag] tag标识 80 /// [stag] stag标识,比如e、i、v等 81 /// [object] 输出object内容 82 /// @author zony 83 /// @time 2022/4/7 14:48 84 static void _printLog(String tag, String stag, Object object) { 85 if (!isOpenLogDef || inProduction) { 86 print('LogUtil._printLog Log returen! [because isOpenLog: ' + 87 isOpenLogDef.toString() + 88 ', TAG: ' + 89 TAG + 90 ', inProduction: ' + 91 inProduction.toString()+']'); 92 return; 93 } 94 StringBuffer stringBuffer = StringBuffer(); 95 stringBuffer.writeln( 96 '┌-----------------------------------------------------------------------------------------'); 97 stringBuffer.write('│-> '); 98 stringBuffer.write(stag); 99 stringBuffer.write(" "); 100 stringBuffer.write((tag == null || tag.isEmpty) ? TAG : tag); 101 stringBuffer.write(": "); 102 stringBuffer.write(object); 103 print(stringBuffer.toString()); 104 print( 105 '└-----------------------------------------------------------------------------------------'); 106 } 107 }
二、创建Live Templates
1、File->Setting->Editor->Live Templates 2、点击最右边+,创建一个Template Group 3、填写group名,任意填写 4、选中你刚刚创建的group,创建Live Template 5、填写Abbreviation,方便输入和记忆可输入logi,即这个日志输出的快捷方式,在输出日志的地方logi加回车即可
6、上图5编辑界面及value如下图