Android 好用Log日志库分享

Logcat

这是一个Android 上 效率极高的 Log 工具,主要功能为控制不同级别的Log输出,Log信息保存到文件、打印行号、函数调用、Json解析、点击跳转、多标签Tag 支持无限长字符串打印,无Logcat4000字符限制等功能

打印行号、函数调用、Json解析、点击跳转 参照KLog of ZhaoKaiQiang.


Gradle

dependencies {
    implementation 'com.github.iflove:Logcat:v2.0.0'
}

1.开始使用 Logcat

你只需要在 Application 里面调用Logcat.initialize一次即可完成初始化

//初始化Logcat
Logcat.initialize(this);

配置更多信息

Builder builder = Logcat.newBuilder();
builder.topLevelTag("Root");
//设置Log 保存的文件夹
builder.logSavePath(StorageUtils.getDiskCacheDir(this, "log"));
//设置输出日志等级
if (BuildConfig.DEBUG) {
  builder.logCatLogLevel(Logcat.SHOW_ALL_LOG);
} else {
  builder.logCatLogLevel(Logcat.SHOW_INFO_LOG | Logcat.SHOW_WARN_LOG | Logcat.SHOW_ERROR_LOG);
}
//设置输出文件日志等级
builder.fileLogLevel(Logcat.NOT_SHOW_LOG);
Logcat.initialize(this, builder.build());

2.示例

//控制台
Logcat.v("The is verbose log");
Logcat.d("The is debug log");
Logcat.i("The is info log");
Logcat.w("The is warn log");
Logcat.e("The is error log");

3.LogTransaction 为Logcat 提供灵活的链式调用api

msg(@NonNull final Object msg);// 打印 msg
msgs(@NonNull final Object... msg);// n ... msg
tag(@NonNull final String tag);// 打印 tag
tags(@NonNull final String... tags); //n ... tag
file(); // log默认输出到文件
file(@NonNull final String fileName); //指定文件名
ln(); //换行
format(@NonNull final String format, Object... args); //格式化
out(); //输出log

4.Logcat log 文件

--默认log文件夹 sdcard/Android/data/you.pakeage/cache/logs 下

//文件log 格式
11-29 22:25:32.363 5523-1/com.lazy.logging V/Logcat[ (MainActivity.java:104)#PrintLog ] output file msg 

5.JLog

安卓开发者都知道,Android系统的单条日志打印长度是有限的, 底层Logger Logger.h 文件中限制了输出日志字符的大小,具体原因就不造了。

Logger.h

#define LOGGER_ENTRY_MAX_LEN        (4*1024)  
#define LOGGER_ENTRY_MAX_PAYLOAD    \\  
    (LOGGER_ENTRY_MAX_LEN - sizeof(struct logger_entry))

开发也没法子通过API来改变这一值。只能采取分段打印的办法输出日志信息。但是我发现了Java 的System.out.println() 是没有限制的,但是在安卓平台上这会转变为安卓INFO 级的日志,而且无需你分段打印的输出日志(是不是很😑)。在实际项目中,一般就也只有请求HTTP接口,而接口又是返回一个比较大JSON,超过4000 字符,安卓的Log API 就好截断,就无法看到完整的响应数据了。采取分段输出,虽然能看到完整的响应数据,但是每达4000字符时突然的换行以及不对齐,稍微不慎就会拷错,拷多json 字符串处理用工具解析查看。当然每次调试看请求数据也是可以的不过很是低效的。所以我做了个大胆的尝试(加入JLog),通过 socket 把日志传送到Java的控制中打印。

JLogServer

Run JLogServer 步骤: 下载JLogServer.java 到你的项目,用as 直接run main(),JLogServer 可以配置相应的端口,也可以用adb 端口映射。

20181129-0.png

Logcat dispatchLog

...
builder.dispatchLog(new JLog("192.168.3.15", 5036));

6.Future

JLogServer.java 是否能作为一个idea intellij plugin ?

7.Sample Usage

ScreenShot-2017-12-05.png

超长的Log完美输出

20181129-1.png

json format 输出格式


Jietu20181129-223652.jpg

文件Log输出格式


20181129-3.png

License

Copyright  2016 Lazy

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About me
Email: 13532605287@163.com

欢迎扫一扫!


11547476583_.pic.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,717评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,135评论 2 59
  • 作者简介:ASCE1885, 《Android 高级进阶》作者。本文由于潜在的商业目的,未经授权不开放全文转载许可...
    asce1885阅读 2,088评论 0 38
  • 一:引用类型有哪些?非引用类型有哪些 引用类型值(对象、数组、函数、正则):指的是那些保存在堆内存中的对象,变量中...
    osborne阅读 208评论 0 0
  • 今天是身为引渡人的第二天 起初还是会有一些与人有关的小问题 一边工作一边辅导一边解决其他问题 我居然还能这样做 现...
    ku123oop阅读 239评论 0 1