版权声明:本文为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