Bonsai
功能
一个微小的 Dart 日志包。
大致基于“Android 风格”的调试日志,并受到 Square 的 logcat 的一定影响
使用 dart:developer 包中的日志功能。
入门
只需导入包并在某处调用 init 方法,例如在 main() 的开头。
import 'package:bonsai/bonsai.dart';
void main() {
const debug = true;
if (debug) {
Log.init();
}
//...
}
用法
Bonsai 为 Object 添加了一个静态扩展方法,使得基本用法非常简单。
class MyClass {
void doSomething() {
log("hello I'm doing something");
}
}
当然,也提供更多自定义用法。
Log.d('Main', 'custom tags are also available as it sending custom data:', data: myObj);
// there is also a specific method for errors:
try {
throw UnsupportedError('soemthing went wrong');
} catch (e, st) {
Log.e('some error happened', e, st);
}
在 Flutter 应用中,日志将发送到常规的应用控制台输出。但是,如果你将其用于 Dart 命令行应用程序,或者希望在 Flutter 应用的单元测试运行时输出,你可能需要启用输出到 stderr。
// pass true to enable output to stderr
Log.init(true);
示例 stdout/stderr 命令行输出如下所示:
FINE: 2021-10-01 16:29:54.573106: [MyClass] hello I'm doing something
FINE: 2021-10-01 16:29:54.576928: [Main] custom tags are also available
SEVERE: 2021-10-01 16:29:54.577526: some error happened
FormatException: something went very wrong
#0 main (file:///home/maks/work/bonsai_dart/example/bonsai_example.dart:16:5)
#1 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
名字?
继承 Android 中日志包命名的优良传统:日志、树、微型日志包…?