1. Project background
logger is a log framework for printing various (Json, map, list) formatted texts and saving to local files under the android platform
2. Import (currently using version 2.1.1)
android studio下,在gradle.build文件下添加,
dependencies {
compile 'com.orhanobut:logger:x.x.x'
}
eclipse 导入logger.2.1.1包
3. API use framework structure
添加日志打印模式,可以添加多个模式,在LoggerPrinter时遍历每个打印模式。
Logger.addLogAdapter(LogAdapter adapter);
AndroidLogAdapter:正常日志打印模式
DiskLogAdapter: 日志保存到磁盘中不做打印日志到控制台
AndroidLogAdapter 添加的配置
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true
// 是否显示线程消息,默认为true
.methodCount(0) // (Optional) How many method line to show. Default 2
// 要显示多少个方法行。默认值为2
.methodOffset(3) // (Optional) Skips some method invokes in stack trace. Default 5
// 跳过堆栈跟踪中的某些方法调用。默认为 5
// .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
// 更改日志策略以打印出。默认的logcat
.tag("My custom tag") // (Optional) Custom tag for each log. Default PRETTY_LOGGER
// 每个日志的自定义标记。默认pretty_logger
.build();
DiskLogAdapter 添加配置
FormatStrategy formatStrategy = CsvFormatStrategy.newBuilder()
.date() // 设置保存的时间,默认data();
.dateFormat() // 设置保存的格式化时间,默认 yyyy.MM.dd HH:mm:ss.SSS
// .logStrategy() // 更改日志策略以打印出。默认的logcat
.tag("custom") // 自定义日志标记
.build();
日志模式配置是否生效回调方法,默认为true,打印日志
Logger.addLogAdapter(new AndroidLogAdapter() {
@Override public boolean isLoggable(int priority, String tag) {
return BuildConfig.DEBUG;
}
});
Logger.clearLogAdapters(); // 清理所有的日志配置
Logger.x(); 日志打印方法
x: 匹配的日志级别
d > log.d
i > log.i
w > log.w
e > log.e
4. Details
The address of the log file saved by DiskLogAdapter is the root directory/logger/
file name is named according to the format of logs_0.csv. When the file size exceeds the default setting value of 500k, the file logs_1.csv will be created.
Modifying the path needs to be modified in the source code CsvFormatStrategy.java.
The default tag for log printing is: PRETTY_LOGGER