1.前言
在所有开发中,日志输出极为重要。Android 为我们提供简单便捷的Log 工具类,在项目开发中,无论代码中还是控制台,Log 处处可见,难免造成代码混乱,Log 究竟从哪里打印的。更有些设备比如电视盒子,车载系统,贩卖机机台,adb 权限不开放,但boss 却让你为其适配,开发时,出现问题时,只能眼巴巴看着代码!!!
Logcat 能干什么?
- 全局控制Log 是否输出, 显示那个日志等级输出
- 能将Log 信息保存到文件中查看
- 打印行号、函数调用、Json解析、点击跳转、多标签Tab 等功能
- 没了,只不过是Log 包装的工具类
Logcat
这是一个Android 上 效率极高的 Log 工具,主要功能为控制不同级别的Log输出,Log信息保存到文件、打印行号、函数调用、Json解析、点击跳转、多标签Tag 支持无限长字符串打印,无Logcat4000字符限制等功能
打印行号、函数调用、Json解析、点击跳转 参照KLog of ZhaoKaiQiang.
Gradle
dependencies {
compile 'com.lazy.logging:library:1.3.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);
} 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 格式
V/Logcat->newTag 2017-04-15_21:10:17
fileName:MainActivity.java
className:com.lazy.logging.MainActivity
methodName:OnCreate
lineNumber:78
output file msg result = 4
5.Sample Usage
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