Logging

Project Url: iflove/Logging
Introduction: Android Log 史上最强大的.最易用的 Logcat 工具
More: Author   ReportBugs   DemoAPK   
Tags:
log-debug-

Logcat

CircleCI MyGet tenant

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

打印行号、函数调用、Json 解析、点击跳转 参照KLog of ZhaoKaiQiang. 推荐:logExtlibrary 作为相关子模块块 log 的打印,主模块实现 Logger 接口相关方法


Gradle

dependencies {
    implementation 'com.github.iflove:Logcat:2.1.2'
    #子模块用
    implementation 'com.github.iflove.Hunter:logExtlibrary:logExtlibrary-1.0'
}

1.开始使用 Logcat

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

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

配置更多信息

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

builder.topLevelTag(TAG_TOP_1);
//删除过了几天无用日志条目
builder.deleteUnusedLogEntriesAfterDays(7);
//输出到 Java 控制台服务端
if (isMainProcess) {
    builder.dispatchLog(new JLog("192.168.3.11", 5036));
}
//是否自动保存日志到文件中
builder.autoSaveLogToFile(true);
//是否显示打印日志调用堆栈信息
builder.showStackTraceInfo(true);
//是否显示文件日志的时间
builder.showFileTimeInfo(true);
//是否显示文件日志的进程以及 Linux 线程
builder.showFilePidInfo(true);
//是否显示文件日志级别
builder.showFileLogLevel(true);
//是否显示文件日志标签
builder.showFileLogTag(true);
//是否显示文件日志调用堆栈信息
builder.showFileStackTraceInfo(true);
//添加该标签,日志将被写入文件
builder.addTagToFile(TAG_APP_EVENT);
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 端口映射。

Logcat dispatchLog

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

6.Future

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

7.Sample Usage

超长的 Log 完美输出

文件 Log 输出格式

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

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools