Android開源日志框架xlog


版權聲明:本文為xing_star原創文章,轉載請注明出處!

本文同步自http://javaexception.com/archives/144

xlog的優點

在開發過程中,避免不了要使用日志組件,用來記錄程序執行過程中一些關鍵節點的日志,出了問題,我們可以根據日志信息,快速定位問題。

對了本文所說的xlog不是指的微信mars下的xlog日志。本文中的xlog是國人開發的一個開源日志框架,github上的地址是 https://github.com/elvishew/XLog
它的優點包括,用法簡單,日志格式美觀,日志輸出可以顯示所在類的行數,可以擴展Android和java的日志庫,可以在多個通道打印日志,如console,File,Logcat等等。除了這些外,他支持打印各種對象或是自定義對象。包括數組,xml,json數據。日志信息包含線程信息,調用棧信息(堆棧信息,方法名,文件名,行號等等)。還支持保存日志文件,日志備份等。對開發者而言相當友好,用法也很簡單。

xlog的用法

先添加依賴

implementation 'com.elvishew:xlog:1.6.1'

接着在Application中初始化

XLog.init(BuildConfig.DEBUG ? LogLevel.ALL : LogLevel.NONE);

這是最簡單的配置,如果想要添加一些自定義的操作,比如在release下采集日志到文件中,在debug下都顯示日志可以這樣配置

LogConfiguration config = new LogConfiguration.Builder()
                .logLevel(BuildConfig.DEBUG ? LogLevel.ALL             // Specify log level, logs below this level won't be printed, default: LogLevel.ALL
                        : LogLevel.ALL)
                .tag("MY_TAG")                                         // Specify TAG, default: "X-LOG"
                .t()                                                   // Enable thread info, disabled by default
                .st(2)                                                 // Enable stack trace info with depth 2, disabled by default
                .b()                                                   // Enable border, disabled by default
//                .jsonFormatter(new MyJsonFormatter())                  // Default: DefaultJsonFormatter
//                .xmlFormatter(new MyXmlFormatter())                    // Default: DefaultXmlFormatter
//                .throwableFormatter(new MyThrowableFormatter())        // Default: DefaultThrowableFormatter
//                .threadFormatter(new MyThreadFormatter())              // Default: DefaultThreadFormatter
//                .stackTraceFormatter(new MyStackTraceFormatter())      // Default: DefaultStackTraceFormatter
//                .borderFormatter(new MyBoardFormatter())               // Default: DefaultBorderFormatter
//                .addObjectFormatter(AnyClass.class,                    // Add formatter for specific class of object
//                        new AnyClassObjectFormatter())                     // Use Object.toString() by default
//                .addInterceptor(new BlacklistTagsFilterInterceptor(    // Add blacklist tags filter
//                        "blacklist1", "blacklist2", "blacklist3"))
//                .addInterceptor(new MyInterceptor())                   // Add a log interceptor
                .build();
 
        String xlogPath = getFilesDir().getAbsolutePath();
        Printer androidPrinter = new AndroidPrinter();             // Printer that print the log using android.util.Log
//        Printer consolePrinter = new ConsolePrinter();             // Printer that print the log to console using System.out
        Printer filePrinter = new FilePrinter                      // Printer that print the log to the file system
                .Builder(xlogPath)                              // Specify the path to save log file
//                .fileNameGenerator(new ChangelessFileNameGenerator("log"))        // Default: ChangelessFileNameGenerator("log")
                .backupStrategy(new NeverBackupStrategy())             // Default: FileSizeBackupStrategy(1024 * 1024)
//                .cleanStrategy(new FileLastModifiedCleanStrategy(MAX_TIME))     // Default: NeverCleanStrategy()
//                .flattener(new MyFlattener())                          // Default: DefaultFlattener
                .build();
 
        if (BuildConfig.DEBUG) {
            XLog.init(                                                 // Initialize XLog
                    config,                                                // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
                    androidPrinter,                                        // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
//                consolePrinter,
                    filePrinter);
        } else {
            XLog.init(                                                 // Initialize XLog
                    config,                                                // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
//                    androidPrinter,                                        // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
//                consolePrinter,
                    filePrinter);
        }

預覽效果

集成成功后,我們在項目中運行下,看看效果如何,如下圖所示。分別是Logcat下的格式化的日志(顯示日志堆棧信息,線程名稱,類名,方法名,行號等),日志文件中的日志信息

 

 

使用xlog后,日志文件可以收集到所有的日志信息,方便了定位問題,格式化的日志,線程名,堆棧信息,對開發者也是相當友好的。

在最近的一個app中,添加了一個日志上傳功能,就是將xlog收集到的日志文件,上傳給開發者,這個功能對於開發者定位一些用戶反饋的問題很有用處,下一篇我將分享下是如何實現日志上傳功能的。

參考資料

如果想深入了解源碼設計的話,可以看這篇文章, android開源日志框架解析 https://www.jianshu.com/p/d039fb0484f0

xlog項目地址 https://github.com/elvishew/XLog


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM