log_utils_plus
log_utils_plus 是一个基于 logger 封装的 Flutter 日志工具,保留简单的静态调用方式,同时补上了常用的级别控制、输出配置和耗时打点能力。
Features
- 直接使用
LogUtils.d/i/w/e/f输出日志 - 支持
setLevel、disable、enable控制日志级别 - 支持
configure自定义颜色、emoji、输出目标和时间提供器 - 内置
logTimeEvent,可记录同一 key 的间隔耗时 - 适合调试期和业务埋点式日志场景
Getting started
在 pubspec.yaml 中引入:
dependencies:
log_utils_plus:
git:
url: https://github.com/akumaCN/log_utils.git
然后在代码中导入:
import 'package:log_utils_plus/log_utils_lib.dart';
Usage
基础日志:
LogUtils.d('debug message');
LogUtils.i('user login success');
LogUtils.w('cache is stale');
LogUtils.e('request failed', error: exception, stackTrace: stackTrace);
控制日志级别:
LogUtils.setLevel(Level.warning);
LogUtils.d('this will be ignored');
LogUtils.w('this will still be printed');
LogUtils.disable();
LogUtils.enable();
自定义输出与样式:
LogUtils.configure(
colors: false,
printEmojis: false,
output: LogPrint(
writer: (line) {
// 写入本地文件、上报平台或自定义控制台
print(line);
},
),
);
记录耗时:
LogUtils.logTimeEvent('fetch profile start', key: 'profile');
// ... do something
final elapsed = LogUtils.logTimeEvent(
'fetch profile finish',
key: 'profile',
printTime: true,
);
print('elapsed: ${elapsed.inMilliseconds}ms');
API notes
logTimeEvent首次调用会返回Duration.zeroresetTime(key: ...)会清除单个计时 keyresetAllTimes()会清空全部计时状态resetConfiguration()会恢复默认日志配置
Libraries
- generated/assets
- This file is automatically generated. DO NOT EDIT, all your changes would be lost.
- log_utils_lib