Flutter 打印日誌封裝及創建Live Templates快捷打印日誌

只需要輸入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 }

View Code

二、創建Live Templates

1、File->Setting->Editor->Live Templates

2、點擊最右邊+,創建一個Template Group  

3、填寫group名,任意填寫

4、選中你剛剛創建的group,創建Live Template

5、填寫Abbreviation,方便輸入和記憶可輸入logi,即這個日誌輸出的快捷方式,在輸出日誌的地方logi加回車即可

 


 6、上圖5編輯界面及value如下圖