logger android日志框架使用

1.项目背景
logger是用于android 平台下打印各种(Json,map,list)格式文本和保存到本地文件的日志框架

2.导入(当前使用2.1.1版本)

    android studio下,在gradle.build文件下添加,
        dependencies {
            compile 'com.orhanobut:logger:x.x.x'
        }
    eclipse 导入logger.2.1.1包

3.API使用 框架结构 image

    添加日志打印模式,可以添加多个模式,在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.细节
DiskLogAdapter 保存的日志文件地址是 根目录/logger/
文件名称按照logs_0.csv 格式来命名的,当文件大小超过默认设定值500k时, 创建文件logs_1.csv。
修改路径需要在源代码CsvFormatStrategy.java中修改。
日志打印默认tag是:PRETTY_LOGGER

猜你喜欢

转载自my.oschina.net/u/3491516/blog/1551399