Simple, pretty and powerful logger for android. 简单、漂亮、强大的安卓记录器。
GitHub 地址:https://github.com/orhanobut/logger
在 app/build.gradle 中添加如下依赖:
// logger
implementation 'com.orhanobut:logger:2.2.0'
功能
- 线程的信息
- 类的信息
- 方法的信息
- 格式打印
json
、xml
等 - 点击链接跳转到源码打印处。
1. 简单使用
Logger.addLogAdapter(new AndroidLogAdapter());
String name = "Kevin";
Logger.i(name);
Logger.clearLogAdapters();
打印结果: 可以看到上图打印的 TAG 是 PRETTY_LOGGER 。
2. 修改默认 TAG
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true
.methodCount(0) // (Optional) How many method line to show. Default 2
.methodOffset(3) // (Optional) Skips some method invokes in stack trace. Default 5
// .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
.tag("MainActivity") // (Optional) Custom tag for each log. Default PRETTY_LOGGER
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
Logger.addLogAdapter(new AndroidLogAdapter() {
@Override
public boolean isLoggable(int priority, String tag) {
return BuildConfig.DEBUG;
}
});
Logger.addLogAdapter(new DiskLogAdapter());
String name = "Kevin";
Logger.i(name);
Logger.clearLogAdapters();
打印结果:
3. 拼接成字符串
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true
.methodCount(0) // (Optional) How many method line to show. Default 2
.methodOffset(3) // (Optional) Skips some method invokes in stack trace. Default 5
// .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
.tag("MainActivity") // (Optional) Custom tag for each log. Default PRETTY_LOGGER
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
Logger.addLogAdapter(new AndroidLogAdapter() {
@Override
public boolean isLoggable(int priority, String tag) {
return BuildConfig.DEBUG;
}
});
Logger.addLogAdapter(new DiskLogAdapter());
String name = "Kevin";
int age = 20;
Logger.i("大家好,我叫%s,今年%d,很高兴大家来看我的文章!!!", name, age);
Logger.clearLogAdapters();
打印结果:
4. 打印 json 数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logger);
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false)
.methodCount(0)
.tag("MainActivity")
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
Logger.i("no thread info and method info");
Logger.t("tag").e("Custom tag for only one use");
String json = createJson().toString();
Logger.json(json);
Logger.clearLogAdapters();
}
// 创建json数据
private JSONObject createJson() {
try {
JSONObject person = new JSONObject();
person.put("phone", "13888888888");
JSONObject address = new JSONObject();
address.put("country", "China");
address.put("province", "GuangDong");
address.put("city", "ShenZhen");
person.put("address", address);
person.put("married", true);
return person;
} catch (JSONException e) {
Logger.e(e, "create json error occured");
}
return null;
}
打印结果:
5. 打印数组、List、Map 等对象数据
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false)
.methodCount(0)
.tag("MainActivity")
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
String[] names = {"Jerry", "Emily", "小五", "hongyang", "七猫"};
Logger.d(names);
List users = new ArrayList();
for (int i = 0; i
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?