关闭

android日志打印库,可以配置打印到本地,可以配置打印格式化的日志

标签: android框架格式化日志
637人阅读 评论(1) 收藏 举报
分类:

LazyLogger

  • android日志打印库,可以配置打印到本地,打印格式化的日志
  • 项目地址 LazyLogger

LoggerLibrary

  • LoggerLibrary是一个为android设计的日志打印库,使用方法如下:

android project 引入此库方式:

``compile 'com.robin.lazy.logger:LoggerLibrary:1.0.0'``

首先要进行初始化,建议只初始化一次,可以在AppLication中进行初始化化操作:

       @Override
       public void onCreate() {
           super.onCreate();
           initLogger();
       }

       /***
        * 初始化日志系统
        *
        * @throws
        * @see [类、类#方法、类#成员]
        */
       protected void initLogger() {
           LazyLogger.init(/* PrinterType.FORMATTED */PrinterType.ORDINARY) // 打印类型
                   .methodCount(3) // default 2
                   .hideThreadInfo() // default shown
                   .logLevel(LogLevel.ALL) // default LogLevel.ALL(设置全局日志等级)
                   .methodOffset(2) // default 0
                   .logTool(/* new AndroidLogTool() *//*new SLF4JTool()*/new Log4JTool(Level.ERROR)); // Log4j中的Level与本框架的LogLevel是分开设置的(Level只用来设置log4j的日志等级)
       }

Api说明:

PrinterType为打印日子类型枚举,目前有两种类型:

  • PrinterType.ORDINARY 普通的日志打印类型,打印出来的日志就是一般的日志 普通日志打印类型的实现类LoggerOrdinaryPrinter.java

  • PrinterType.FORMATTED 为格式化的日志打印类型,打印出来的日志如下 格式化的日子打印实现类LoggerFormattedPrinter.java

    04-15 14:19:14.149 9240-9240/com.robin.lazy.sample I/LazyLogger: ╔════════════════════════════════════════════════════════════════════════════════════════
        04-15 14:19:14.150 9240-9240/com.robin.lazy.sample I/LazyLogger:ActivityThread.handleLaunchActivity  (ActivityThread.java:2701)
        04-15 14:19:14.150 9240-9240/com.robin.lazy.sample I/LazyLogger:ActivityThread.performLaunchActivity  (ActivityThread.java:2590)
        04-15 14:19:14.150 9240-9240/com.robin.lazy.sample I/LazyLogger:Instrumentation.callActivityOnCreate  (Instrumentation.java:1125)
        04-15 14:19:14.151 9240-9240/com.robin.lazy.sample I/LazyLogger: ╟────────────────────────────────────────────────────────────────────────────────────────
        04-15 14:19:14.152 9240-9240/com.robin.lazy.sample I/LazyLogger:com.robin.lazy.sample.MainActivity
        04-15 14:19:14.153 9240-9240/com.robin.lazy.sample I/LazyLogger: ╚════════════════════════════════════════════════════════════════════════════════════════
        04-15 14:19:14.155 9240-9240/com.robin.lazy.sample D/LazyLogger: ╔════════════════════════════════════════════════════════════════════════════════════════
        04-15 14:19:14.157 9240-9240/com.robin.lazy.sample D/LazyLogger:ActivityThread.handleLaunchActivity  (ActivityThread.java:2701)
        04-15 14:19:14.158 9240-9240/com.robin.lazy.sample D/LazyLogger:ActivityThread.performLaunchActivity  (ActivityThread.java:2590)
        04-15 14:19:14.159 9240-9240/com.robin.lazy.sample D/LazyLogger:Instrumentation.callActivityOnCreate  (Instrumentation.java:1125)
        04-15 14:19:14.160 9240-9240/com.robin.lazy.sample D/LazyLogger: ╟────────────────────────────────────────────────────────────────────────────────────────
        04-15 14:19:14.161 9240-9240/com.robin.lazy.sample D/LazyLogger: ║ {
        04-15 14:19:14.161 9240-9240/com.robin.lazy.sample D/LazyLogger:"province": [
        04-15 14:19:14.161 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.162 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "100000",
        04-15 14:19:14.162 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "全国"
        04-15 14:19:14.163 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.163 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.164 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "110000",
        04-15 14:19:14.164 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "北京市"
        04-15 14:19:14.165 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.166 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.166 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "120000",
        04-15 14:19:14.166 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "天津市"
        04-15 14:19:14.167 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.167 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.168 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "130000",
        04-15 14:19:14.169 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "河北省"
        04-15 14:19:14.170 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.171 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.171 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "140000",
        04-15 14:19:14.172 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "山西省"
        04-15 14:19:14.173 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.174 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.175 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "150000",
        04-15 14:19:14.176 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "内蒙古自治区"
        04-15 14:19:14.177 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.178 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.178 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "210000",
        04-15 14:19:14.179 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "辽宁省"
        04-15 14:19:14.179 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.179 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.180 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "220000",
        04-15 14:19:14.180 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "吉林省"
        04-15 14:19:14.181 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.181 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.182 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "230000",
        04-15 14:19:14.182 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "黑龙江省"
        04-15 14:19:14.183 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.183 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.184 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "310000",
        04-15 14:19:14.184 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "上海市"
        04-15 14:19:14.185 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.186 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.186 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "320000",
        04-15 14:19:14.187 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "江苏省"
        04-15 14:19:14.188 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.189 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.191 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "330000",
        04-15 14:19:14.192 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "浙江省"
        04-15 14:19:14.193 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.193 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.194 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "340000",
        04-15 14:19:14.194 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "安徽省"
        04-15 14:19:14.195 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.195 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.196 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "350000",
        04-15 14:19:14.196 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "福建省"
        04-15 14:19:14.196 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.197 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.197 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "360000",
        04-15 14:19:14.197 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "江西省"
        04-15 14:19:14.198 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.198 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.199 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "370000",
        04-15 14:19:14.200 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "山东省"
        04-15 14:19:14.200 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.201 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.201 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "410000",
        04-15 14:19:14.201 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "河南省"
        04-15 14:19:14.202 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.202 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.203 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "420000",
        04-15 14:19:14.203 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "湖北省"
        04-15 14:19:14.203 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.204 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.204 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "430000",
        04-15 14:19:14.205 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "湖南省"
        04-15 14:19:14.205 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.205 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.206 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "440000",
        04-15 14:19:14.207 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "广东省"
        04-15 14:19:14.207 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.208 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.208 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "450000",
        04-15 14:19:14.209 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "广西壮族自治区"
        04-15 14:19:14.209 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.210 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.210 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "460000",
        04-15 14:19:14.211 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "海南省"
        04-15 14:19:14.211 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.212 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.212 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "500000",
        04-15 14:19:14.213 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "重庆市"
        04-15 14:19:14.213 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.213 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.214 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "510000",
        04-15 14:19:14.214 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "四川省"
        04-15 14:19:14.215 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.216 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.216 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "520000",
        04-15 14:19:14.217 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "贵州省"
        04-15 14:19:14.217 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.218 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.218 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "530000",
        04-15 14:19:14.219 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "云南省"
        04-15 14:19:14.219 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.220 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.220 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "540000",
        04-15 14:19:14.221 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "西藏自治区"
        04-15 14:19:14.221 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.222 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.222 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "610000",
        04-15 14:19:14.223 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "陕西省"
        04-15 14:19:14.223 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.224 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.224 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "620000",
        04-15 14:19:14.225 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "甘肃省"
        04-15 14:19:14.225 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.225 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.226 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "630000",
        04-15 14:19:14.226 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "青海省"
        04-15 14:19:14.227 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.227 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.227 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "640000",
        04-15 14:19:14.227 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "宁夏回族自治区"
        04-15 14:19:14.228 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         },
        04-15 14:19:14.228 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         {
        04-15 14:19:14.229 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaid": "650000",
        04-15 14:19:14.229 9240-9240/com.robin.lazy.sample D/LazyLogger:"areaname": "新疆维吾尔自治区"
        04-15 14:19:14.229 9240-9240/com.robin.lazy.sample D/LazyLogger: ║         }
        04-15 14:19:14.230 9240-9240/com.robin.lazy.sample D/LazyLogger: ║     ]
        04-15 14:19:14.230 9240-9240/com.robin.lazy.sample D/LazyLogger: ║ }
        04-15 14:19:14.231 9240-9240/com.robin.lazy.sample D/LazyLogger: ╚════════════════════════════════════════════════════════════════════════════════════════

LogLevel设置全局日志打印级别的枚举,用来过滤不需要的日志

     /**
         * 输出所有类型日志
         */
        ALL(0),

        /**
         * trace类型日志
         */
        TRACE(1),

        /**
         * debug类型日志
         */
        DEBUG(2),

        /**
         * info类型日志
         */
        INFO(3),

        /**
         * warn类型日志
         */
        WARN(4),

        /**
         * error类型日志
         */
        ERROR(5),

        /**
         * fatal类型日志
         */
        FATAL(6),

        /**
         * 关闭日志输出
         */
        OFF(7);

logTool日志打印器接口,可以自定义日志打印器,目前已实现的有三种:

  • (AndroidLogTool.java) android普通类形的日子打印器

  • (Log4JTool.java) Log4J日志打印器,可以输入日志到本地文件, public Log4JTool(Level level, String dirName, String fileName) 可以自己设置输入的文件目录及文件名,firName为文件目录,fileName文件名

  • (SLF4JTool.java) SLF4日志打印器,可以输入日志到本地文件, public SLF4JTool(Level level, String dirName, String fileName) 可以自己设置输入的文件目录及文件名,firName为文件目录,fileName文件名

  • 其中后面两种日志打印器会输出日志文件到本地,所以需要配置文件读写权限,负责抛错

初始化完,一切工作就绪后就可以直接打印日子了,如下:

     LazyLogger.i(MainActivity.this.getClass().getName(),"onCreate");
             LazyLogger.json(area_strs);

关于作者Robin

1
0
查看评论

【Android 开发入门】android studio 控制台打印输出日志

有些情况下,不方便使用断点的方式来调试,而是希望在控制台打印输出日志,使用过Eclipse的同学都知道Java可以使用 System.out.println(""); 来在控制台打印输出日志,但是在android studio中却是不行的,还是有差别的,那应该用什么呢?andro...
  • testcs_dn
  • testcs_dn
  • 2016-12-17 22:59
  • 22753

Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件

在开发中,我们常常用打印log的方式来调试我们的应用。在Java中我们常常使用方法System.out.println()来在控制台打印日志,以便我们的调试。Android中有一个专门的类Log来实现在Android系统下日志的打印,更加方便我们定位程序出现问题的地方。
  • finddreams
  • finddreams
  • 2015-05-08 00:01
  • 5578

Android 如何优雅的打印日志

1.介绍今天来介绍Android平台上一款简单、漂亮、强大的开源日志库Logger。Logger GitHub地址Logger提供了以下方法: 打印线程的信息 打印类的信息 打印方法的信息 优雅的打印JSON数据 优雅的打印换行符 打印简洁的信息 点击日志跳转至源码 感受下Logger打印的日志:2...
  • kong_gu_you_lan
  • kong_gu_you_lan
  • 2016-12-27 09:40
  • 1531

Android log 日志打印简单封装

日志打印log在调试的时候基本都会用到,像这种使用比较频繁的方法应该尽量去简化它
  • qq_28108951
  • qq_28108951
  • 2016-09-22 01:58
  • 1455

android的Log输出

<br /><br />一个android应用程序运行后 并不会在 ide的控制台内输出任何信息. 不能在控制台输出。但是android提供的Log类。<br /> <br />在程序中输出日志, 使用 android.util.Log 类....
  • alley_zhang
  • alley_zhang
  • 2010-07-05 16:07
  • 88008

Android 在屏幕上打印log

代码链接
  • u010083695
  • u010083695
  • 2017-05-04 18:37
  • 474

Android json数据Log格式化打印

问题: Android项目中的网络请求返回的数据一般都是json格式,而且可能很长,在log中就是长长的一大串堆在那里,还需要复制出来进行格式化处理,很麻烦。 看到ios人家返回的数据直接就是json格式化后的样式,看起来方便多了,羡慕ing 解决: 偶然看到了有人处理的log样式是这样的 这不...
  • adarcy
  • adarcy
  • 2017-08-03 01:24
  • 813

Android平台Log输出规范

1、  目的: 为了规范软件工程师在android代码编写过程中输出Log的行为,使得发布的产品中打印的Log是必须的,打印的Log的级别是能真实反映此Log对应的级别,标签、Log内容具有很好的可读性。 2、  适用范围 android平台java、c++、...
  • liuxd3000
  • liuxd3000
  • 2013-10-31 10:50
  • 27940

android log输出控制

1. java层java层打印log由类android.util.Log类实现,该类定义于文件:frameworks/base/core/java/android/util/Log.java (APP 框架层)对应的JNI层代码在文件:frameworks/base/core/j
  • lizhiguo0532
  • lizhiguo0532
  • 2011-07-25 18:43
  • 9485

Android开发中调试日志的输出方法

一个android应用程序运行后 并不会在 ide的控制台内输出任何信息. 不能在控制台输出。但是android提供的Log类。   在程序中输出日志, 使用 android.util.Log 类.  该类提供了若干静态方法 Log.v(String tag,...
  • rainday0310
  • rainday0310
  • 2014-04-17 18:37
  • 20128
    个人资料
    • 访问:7956次
    • 积分:131
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:1篇
    • 译文:0篇
    • 评论:5条
    文章分类
    文章存档