Android 好用Log日誌庫分享
Logcat
ofollow,noindex">[圖片上傳失敗...(image-bc46fc-1543506136729)] [圖片上傳失敗...(image-23977d-1543506136729)] [圖片上傳失敗...(image-e20f0b-1543506136729)]
這是一個Android 上 效率極高的 Log 工具,主要功能為控制不同級別的Log輸出,Log資訊儲存到檔案、列印行號、函式呼叫、Json解析、點選跳轉、多標籤Tag 支援無限長字串列印,無Logcat4000字元限制等功能
列印行號、函式呼叫、Json解析、點選跳轉 參照 KLog of ZhaoKaiQiang .
Gradle
dependencies { implementation 'com.github.iflove:Logcat:latest' }
1.開始使用 Logcat
你只需要在 Application 裡面呼叫Logcat.initialize一次即可完成初始化
//初始化Logcat Logcat.initialize(this);
配置更多資訊
Builder builder = Logcat.newBuilder(); builder.topLevelTag("Root"); //設定Log 儲存的資料夾 builder.logSavePath(StorageUtils.getDiskCacheDir(this, "log")); //設定輸出日誌等級 if (BuildConfig.DEBUG) { builder.logCatLogLevel(Logcat.SHOW_ALL_LOG); } else { builder.logCatLogLevel(Logcat.SHOW_INFO_LOG | Logcat.SHOW_WARN_LOG | Logcat.SHOW_ERROR_LOG); } //設定輸出檔案日誌等級 builder.fileLogLevel(Logcat.NOT_SHOW_LOG); Logcat.initialize(this, builder.build());
2.示例
//控制檯 Logcat.v("The is verbose log"); Logcat.d("The is debug log"); Logcat.i("The is info log"); Logcat.w("The is warn log"); Logcat.e("The is error log");
3.LogTransaction 為Logcat 提供靈活的鏈式呼叫api
msg(@NonNull final Object msg);// 列印 msg msgs(@NonNull final Object... msg);// n ... msg tag(@NonNull final String tag);// 列印 tag tags(@NonNull final String... tags); //n ... tag file(); // log預設輸出到檔案 file(@NonNull final String fileName); //指定檔名 ln(); //換行 format(@NonNull final String format, Object... args); //格式化 out(); //輸出log
4.Logcat log 檔案
--預設log資料夾 sdcard/Android/data/you.pakeage/cache/logs 下 //檔案log 格式 11-29 22:25:32.363 5523-1/com.lazy.logging V/Logcat[ (MainActivity.java:104)#PrintLog ] output file msg
5.JLog
安卓開發者都知道,Android系統的單條日誌列印長度是有限的, 底層Logger Logger.h
檔案中限制了輸出日誌字元的大小,具體原因就不造了。
Logger.h
#define LOGGER_ENTRY_MAX_LEN(4*1024) #define LOGGER_ENTRY_MAX_PAYLOAD\\ (LOGGER_ENTRY_MAX_LEN - sizeof(struct logger_entry))
開發也沒法子通過API來改變這一值。只能採取分段列印的辦法輸出日誌資訊。但是我發現了Java 的 System.out.println()
是沒有限制的,但是在安卓平臺上這會轉變為安卓INFO 級的日誌,而且無需你分段列印的輸出日誌(是不是很:expressionless:)。在實際專案中,一般就也只有請求HTTP介面,而介面又是返回一個比較大JSON,超過4000 字元,安卓的Log API 就好截斷,就無法看到完整的響應資料了。採取分段輸出,雖然能看到完整的響應資料,但是每達4000字元時突然的換行以及不對齊,稍微不慎就會拷錯,拷多json 字串處理用工具解析檢視。當然每次除錯看請求資料也是可以的不過很是低效的。所以我做了個大膽的嘗試(加入JLog),通過 socket
把日誌傳送到Java的控制中列印。
JLogServer
Run JLogServer 步驟: 下載JLogServer.java 到你的專案,用as 直接run main(),JLogServer 可以配置相應的埠,也可以用adb 埠對映。

20181129-0.png
Logcat dispatchLog
... builder.dispatchLog(new JLog("192.168.3.15", 5036));
6.Future
JLogServer.java 是否能作為一個idea intellij plugin ?
7.Sample Usage

ScreenShot-2017-12-05.png
超長的Log完美輸出

20181129-1.png
檔案Log輸出格式

20181129-3.png
License
Copyright2016 Lazy Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
About meEmail: [email protected]