第三方-日志

https://mp.weixin.qq.com/s/nIItHxMWq8LtOPHXBz-PrA

Logger

build.gradle中:

1
implementation "com.orhanobut:logger:2.2.0"

Application中:

1
2
3
4
5
6
7
8
9
10
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()  
.showThreadInfo(true) // (Optional) Whether to show thread info or not. Default true
.methodCount(1) // (Optional) How many method line to show. Default 2
// .methodOffset(3) // (Optional) Skips some method invokes in stack trace. Default 0
.tag("My custom tag") // (Optional) Custom tag for each log. Default PRETTY_LOGGER
.build();

Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

Logger.addLogAdapter(new DiskLogAdapter());

log-logger

Timber

build.gradle中:

1
implementation 'com.jakewharton.timber:timber:5.0.1'

Application中:

1
2
3
4
5
6
7
if (BuildConfig.DEBUG) {  
Timber.plant(new DebugTree());
} else {
Timber.plant(new CrashReportingTree());
}

Timber.i("A button with ID %s was clicked to say '%s'.", button.getId(), button.getText());

log-timer

XLog

build.gradle中:

1
implementation "com.elvishew:xlog:1.10.1"

Application中:

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
35
36
LogConfiguration config = new LogConfiguration.Builder()
.logLevel(BuildConfig.DEBUG ? LogLevel.ALL // 指定日志级别,低于该级别的日志将不会被打印,默认为 LogLevel.ALL
: LogLevel.NONE)
.tag("MY_TAG") // 指定 TAG,默认为 "X-LOG"
.enableThreadInfo() // 允许打印线程信息,默认禁止
.enableStackTrace(2) // 允许打印深度为 2 的调用栈信息,默认禁止
.enableBorder() // 允许打印日志边框,默认禁止
.jsonFormatter(new MyJsonFormatter()) // 指定 JSON 格式化器,默认为 DefaultJsonFormatter
.xmlFormatter(new MyXmlFormatter()) // 指定 XML 格式化器,默认为 DefaultXmlFormatter
.throwableFormatter(new MyThrowableFormatter()) // 指定可抛出异常格式化器,默认为 DefaultThrowableFormatter
.threadFormatter(new MyThreadFormatter()) // 指定线程信息格式化器,默认为 DefaultThreadFormatter
.stackTraceFormatter(new MyStackTraceFormatter()) // 指定调用栈信息格式化器,默认为 DefaultStackTraceFormatter
.borderFormatter(new MyBoardFormatter()) // 指定边框格式化器,默认为 DefaultBorderFormatter
.addObjectFormatter(AnyClass.class, // 为指定类型添加对象格式化器
new AnyClassObjectFormatter()) // 默认使用 Object.toString()
.addInterceptor(new BlacklistTagsFilterInterceptor( // 添加黑名单 TAG 过滤器
"blacklist1", "blacklist2", "blacklist3"))
.addInterceptor(new MyInterceptor()) // 添加一个日志拦截器
.build();

Printer androidPrinter = new AndroidPrinter(true); // 通过 android.util.Log 打印日志的打印器
Printer consolePrinter = new ConsolePrinter(); // 通过 System.out 打印日志到控制台的打印器
Printer filePrinter = new FilePrinter // 打印日志到文件的打印器
.Builder("<日志目录全路径>") // 指定保存日志文件的路径
.fileNameGenerator(new DateFileNameGenerator()) // 指定日志文件名生成器,默认为 ChangelessFileNameGenerator("log")
.backupStrategy(new NeverBackupStrategy()) // 指定日志文件备份策略,默认为 FileSizeBackupStrategy(1024 * 1024)
.cleanStrategy(new FileLastModifiedCleanStrategy(MAX_TIME)) // 指定日志文件清除策略,默认为 NeverCleanStrategy()
.flattener(new MyFlattener()) // 指定日志平铺器,默认为 DefaultFlattener
.writer(new MyWriter()) // 指定日志写入器,默认为 SimpleWriter
.build();

XLog.init( // 初始化 XLog
config, // 指定日志配置,如果不指定,会默认使用 new LogConfiguration.Builder().build()
androidPrinter, // 添加任意多的打印器。如果没有添加任何打印器,会默认使用 AndroidPrinter(Android)/ConsolePrinter(java)
consolePrinter,
filePrinter);

log-xlog

其他

Log4j、logback 也是比较优秀的日志框架,不过他们更主要应用场景是在服务端。当然如果一定要在 Android 里使用它们也不是不行,就是需要比较深度的定制、封装。