目录

Android第三方log库:xlog使用记录

目录

第一步:由于xlog发布在jitpack 仓库,所以如果android studio没有在project目下的的build.gradle下配置jitpack仓库的话需要配置下

1
2
3
4
5
6
7
allprojects {
    repositories {
        jcenter()
        maven { url 'https://jitpack.io' }

    }
}

第二步:配置xlog参数(推荐在Application的生命周期方法中进行配置)

根据官方文档,xlog有便捷配置和详细配置两种,个人可根据具体需要而决定采用哪一种:

最简单配置:LogLevel.ALL为日志的输出级别,跟自由调整

1
XLog.init(LogLevel.ALL);根据编译模式调整,实际开发常用:发布apk时为了节约资源,也没有输出日志的必要,通常我们都会把log关闭XLog.init(BuildConfig.DEBUG ? LogLevel.ALL : LogLevel.NONE);详细配置:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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.NONE)
    .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();

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("/sdcard/xlog/")                              // Specify the path to save log file
    .fileNameGenerator(new DateFileNameGenerator())        // Default: ChangelessFileNameGenerator("log")
    .backupStrategy(new NeverBackupStrategy())             // Default: FileSizeBackupStrategy(1024 * 1024)
    .logFlattener(new MyFlattener())                       // Default: DefaultFlattener
    .build();

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);接下来就可以愉快的使用了,感谢开源作者的无私奉献