android logger 简单,漂亮和强大的记录器 7097星

时间:2021-09-25 06:50:17

https://github.com/orhanobut/logger

github 7097星

android logger 简单,漂亮和强大的记录器 7097星


记录仪

简单,漂亮和强大的记录器为Android

安装

下载

    compile 'com.orhanobut:logger:2.1.1'

初始化

    Logger.addLogAdapter(new AndroidLogAdapter());

使用

Logger.d("hello");

输出

android logger 简单,漂亮和强大的记录器 7097星

选项

Logger.d("debug");
Logger.e("error");
Logger.w("warning");
Logger.v("verbose");
Logger.i("information");
Logger.wtf("wtf!!!!");

支持 字符串格式化参数

    Logger.d("hello %s", "world");

支持集合 (仅限于 Debug)

    Logger.d(MAP);
Logger.d(SET);
Logger.d(LIST);
Logger.d(ARRAY);

支持 Json 、Xml (输出在Debug级别)

    Logger.json(JSON_CONTENT);
Logger.xml(XML_CONTENT);

高级 [进阶]

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(7) // (Optional) Hides internal method calls up to offset. Default 5
.logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
.tag("My custom tag") // (Optional) Global tag for every log. Default PRETTY_LOGGER
.build();


Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

可记录的

日志适配器通过检查此功能来检查日志是否应打印出来。如果要禁用/隐藏日志的输出,重写 isLoggable() 方法并放置条件。

Logger.addLogAdapter(new AndroidLogAdapter() {
@Override public boolean isLoggable(int priority, String tag) {
return BuildConfig.DEBUG;
}
});

保存日志到文件

// TODO:稍后会添加更多信息

Logger.addLogAdapter(new DiskLogAdapter());

将自定义标签添加到Csv格式策略

FormatStrategy formatStrategy = CsvFormatStrategy.newBuilder()
.tag("custom")
.build();

Logger.addLogAdapter(new DiskLogAdapter(formatStrategy));

怎么工作的

android logger 简单,漂亮和强大的记录器 7097星

更多

  • 使用过滤得到更好的结果。PRETTY_LOGGER 或你自己定义的Tag
  • 确保禁用包装选项
  • 您还可以通过更改设置来简化输出。

android logger 简单,漂亮和强大的记录器 7097星

  • 木材整合
// Set methodOffset to 5 in order to hide internal method calls
Timber.plant(new Timber.DebugTree() {
@Override protected void log(int priority, String tag, String message, Throwable t) {
Logger.log(priority, tag, message, t);
}
});

突破变化

  • 初始化更改。没有向后兼容性支持。使用Logger.addLogAdapter
  • LogLevel被删除。使用新的isLoggable方法

执照

Copyright 2017 Orhan Obut

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.

end