DKLog class
日志工具类
日志格式说明:
- 时间戳:
<HH:MM:SS.mmm>使用尖括号 - 日志级别:
[LEVEL]使用方括号,如ERROR、INFO - 标签:
#TagName使用井号前缀 - 调用位置:
@file.dart:123使用@符号前缀 - 消息内容:
: message冒号后跟消息
完整示例:
<12:34:56.789> [INFO] #Network @api_service.dart:45: 请求成功
<12:34:56.790> [ERROR] #Database @db_helper.dart:123: 连接失败 {"error": "timeout"}
这种格式设计使得:
- 容易区分日志的各个部分
- 搜索时不会与消息内容(如JSON)中的[]混淆
- 可以使用正则表达式精确匹配特定部分
- 搜索时间戳:
<\d{2}:\d{2}:\d{2}\.\d{3}> - 搜索标签:
#TagName - 搜索位置:
@filename\.dart:\d+
- 搜索时间戳:
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- enableDeveloperLog ↔ bool
-
是否启用developer.log输出
getter/setter pair
- isWebSocketConnected → bool
-
获取 WebSocket 连接状态
no setter
- isWebSocketEnabled → bool
-
获取 WebSocket 是否已启用
no setter
Static Methods
-
addExcludeTag(
String tag) → void - 添加到排除列表
-
addIncludeTag(
String tag) → void - 添加到包含列表
-
clearAllLogs(
) → Future< void> - 清空所有日志文件
-
clearExcludeTags(
) → void - 清空排除列表
-
clearIncludeTags(
) → void - 清空包含列表(显示所有tag)
-
d(
dynamic message, {String? tag, Object? error, StackTrace? stackTrace}) → void - 调试日志
-
disableWebSocketLog(
) → Future< void> - 禁用 WebSocket 日志传输
-
e(
dynamic message, {String? tag, Object? error, StackTrace? stackTrace}) → void - 错误日志
-
enableWebSocketLog(
{DKLogLevel webSocketLogLevel = DKLogLevel.debug, bool autoDiscover = true, String? host, int? port, String? path, String? serviceName}) → Future< void> - 启用 WebSocket 日志传输
-
exportLogs(
{String? destinationPath}) → Future< String?> - 导出日志文件到公共目录(需要存储权限)
-
f(
dynamic message, {String? tag, Object? error, StackTrace? stackTrace}) → void - 严重错误日志
-
getExcludeTags(
) → Set< String> - 获取当前的排除 tag 列表
-
getIncludeTags(
) → Set< String> - 获取当前的包含 tag 列表
-
getLogFiles(
) → Future< List< File> > - 获取所有日志文件
-
hasStoragePermission(
) → Future< bool> - 检查是否有存储权限
-
i(
dynamic message, {String? tag, Object? error, StackTrace? stackTrace}) → void - 信息日志
-
initFileLog(
{bool enable = true, DKLogLevel fileLogLevel = DKLogLevel.info, int maxFileSize = 10 * 1024 * 1024, int maxFileCount = 5, bool requestPermission = false}) → Future< void> - 初始化日志文件写入
-
json(
dynamic jsonObject, {String? tag}) → void - 打印 JSON 格式化输出
-
openAppSettings(
) → Future< void> - 打开应用设置页面(用于手动授权)
-
reconnectWebSocket(
) → Future< void> - 手动重连 WebSocket(用于界面按钮)
-
removeExcludeTag(
String tag) → void - 从排除列表移除
-
removeIncludeTag(
String tag) → void - 从包含列表移除
-
requestStoragePermission(
) → Future< bool> - 请求存储权限(可选,应用私有目录不需要)
-
s(
dynamic message, {String? tag, Object? error, StackTrace? stackTrace}) → void - 成功日志
-
separator(
{String char = '-', int length = 80}) → void - 打印分隔线
-
setEnabled(
bool enabled) → void - 启用或禁用日志
-
setExcludeTags(
Set< String> tags) → void - 设置不显示这些 tag 的日志
-
setIncludeTags(
Set< String> tags) → void - 设置只显示这些 tag 的日志(为空则显示所有)
-
setLevel(
DKLogLevel level) → void - 设置日志级别
-
setShowLocation(
bool show) → void - 设置是否显示调用位置
-
setShowTimestamp(
bool show) → void - 设置是否显示时间戳
-
setUseColor(
bool use) → void - 设置是否使用颜色输出
-
setWebSocketConnectionCallback(
void callback(bool connected)?) → void - 设置 WebSocket 连接状态变化回调
- 分享日志文件(使用系统分享功能) 需要添加 share_plus 依赖
-
t(
dynamic message, {String? tag, Object? error, StackTrace? stackTrace}) → void - 临时调试日志(橙色)
-
title(
String title, {String char = '=', int length = 80, String? tag}) → void -
w(
dynamic message, {String? tag, Object? error, StackTrace? stackTrace}) → void - 警告日志