介紹一款安卓開發優雅的日誌log框架_Logger
阿新 • • 發佈:2019-01-24
實現功能:
- 執行緒的資訊
- 類的資訊
- 方法的資訊
- 格式列印json、xml等
- 點選連結跳轉到原始碼列印處
實現效果:
初始化和關閉以及儲存日誌到SD卡
在Application的OnCreate方法中:
// 以下是列印自定義日誌到控制檯
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
// .showThreadInfo(false) // 是否選擇顯示執行緒資訊,預設為true
// .methodCount(0) // 方法數顯示多少行,預設2行
// .methodOffset(7) // 隱藏方法內部呼叫到偏移量,預設5
// .logStrategy(customLog) // 列印日誌的策略,預設LogCat
.tag("MyCustomTag") // 自定義TAG全部標籤,預設PRETTY_LOGGER
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy){
@Override
public boolean isLoggable(int priority, String tag) {
return BuildConfig.DEBUG;
// return LogUtil.isDebug;
}
});
// 以下是儲存自定義日誌
FormatStrategy formatStrateg = CsvFormatStrategy.newBuilder()
.tag("custom")
.build();
Logger.addLogAdapter(new DiskLogAdapter(formatStrateg));
具體使用就常規呼叫,參考作者寫的文件就行。
這裡需要說明的是,CsvFormatStrategy儲存日誌到本地sd卡中是在根目錄下一個logger檔案,是.cvs檔案,這個檔案的生成並不受專案中logger關閉的影響,就是說你關閉了log,只是控制檯不再列印,但是日誌檔案還是會列印儲存在手機的sd卡中的。