FLogs 高级日志框架
FLog 是一个在 Flutter 中开发的先进日志框架,提供快速简便的日志解决方案。所有日志都保存在数据库中,之后可以导出为 zip 文件。
FLogs 使用 Dart 编写。它主要包含两种日志记录器(FLog 和 DataLog),并具有许多日志记录所需的高级功能。日志保存在数据库中,之后可以导出到 Android|iOS 设备的文档目录中。当开发人员想要分析应用内的用户活动时,这些日志会很有帮助。可以轻松地过滤和排序日志。可以根据过滤类型轻松地将日志导出为 zip 文件,然后将 zip 文件上传到服务器或本地使用。
很多时候我们需要记录一组数据来分析特定活动,例如位置(GPS 坐标)、设备信息、网络请求等,这有助于我们快速识别和修复在生产环境中难以调试的问题。FLogs 提供此类功能,将数据集记录到数据库中。然后可以通过应用不同的便捷过滤器来获取这些日志。
功能
- 使用 **“FLogs”** 日志记录器,每小时单独创建文件记录事件。(24 小时)
- 文件可以压缩并导出,用于时间段和日期过滤
- 轻松清除日志
- 将日志保存到自定义路径(仅 Android 支持)
- 将日志导出到自定义路径为 zip 文件(仅 Android 支持)
- 自定义日志格式
- 支持 CSV
- 自定义时间戳支持
- 使用 **“DataLogs”** 日志记录器支持自定义数据记录。
- 添加了加密支持
- 多个目录结构
- 将日志作为字符串打印
- 导出所有或单个类型的日志
- 自动删除日志的先进自动化
- 导出 HTML 格式的异常
- 日志级别支持
将此包用作库
1. 依赖它
将此添加到您的 package 的 pubspec.yaml 文件中
dependencies:
f_logs: ^1.0.x
2. 安装它
您可以从命令行安装包
使用 Flutter
$ flutter packages get
或者,您的编辑器可能支持 flutter packages get。请查看您编辑器的文档了解更多信息。
3. 导入它
现在,在您的 Dart 代码中,您可以使用
import 'package:f_logs/f_logs.dart';
如何使用
日志文件导出到存储目录,因此首先将这些权限添加到项目清单文件中非常重要。
Android
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
iOS
<key>NSPhotoLibraryAddUsageDescription</key>
<string>FLogs would like to save photos from the app to your gallery</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>FLogs would like to access your photo gallery for uploading images to the app</string>
要保存日志,只需调用下面提到的任何方法
1. 简单的跟踪日志
FLog.trace(
className: "HomePage",
methodName: "_buildRow1",
text: "My log");
2. 简单的调试日志
FLog.debug(
className: "HomePage",
methodName: "_buildRow1",
text: "My log");
3. 简单的信息日志
FLog.info(
className: "HomePage",
methodName: "_buildRow1",
text: "My log");
4. 简单的警告日志
FLog.warning(
className: "HomePage",
methodName: "_buildRow1",
text: "My log";
5. 简单的错误日志
FLog.error(
className: "HomePage",
methodName: "_buildRow1",
text: "My log");
6. 简单的严重日志
FLog.severe(
className: "HomePage",
methodName: "_buildRow1",
text: "My log");
7. 带异常和堆栈跟踪的严重日志
FLog.logThis(
className: "HomePage",
methodName: "_buildRow1",
text: "My log",
type: LogLevel.SEVERE,
exception: Exception("This is an Exception!"),
stacktrace: StackTrace.current);
8. 简单的致命日志
FLog.fatal(
className: "HomePage",
methodName: "_buildRow1",
text: "My log");
9. 数据类型日志
FLog.logThis(
className: "HomePage",
methodName: "_buildRow1",
text: "My log",
type: LogLevel.SEVERE,
dataLogType: DataLogType.DEVICE.toString());
可用方法
FLogs 提供许多便捷的方法将日志保存到数据库或从数据库中获取日志,以下是所有可用方法的列表
1. logThis
记录字符串数据以及类名、方法名、日志文本和日志类型(LogLevel.SEVERE、LogLevel.INFO)等。相同的方法可用于记录异常或数据日志。FLog 和 DataLogs 之间的区别已在上面描述,您也可以查看 wiki 以获取更多详细信息。如果未提供类名或方法名,它将通过获取调用类和方法来自动获取。
static logThis({
String className, // This is optional if not provided, then it will automatically be taken by getting calling class
String methodName, // This is optional if not provided, then it will automatically be taken by getting calling method
@required String text,
@required LogLevel type,
Exception exception,
String dataLogType,
StackTrace stacktrace,
}){}
2. trace
记录字符串数据以及类名、方法名、日志文本和日志类型(LogLevel.SEVERE、LogLevel.INFO)等。相同的方法可用于记录异常或数据日志。如果未提供类名或方法名,它将通过获取调用类和方法来自动获取。
static trace({
String className, // This is optional if not provided, then it will automatically be taken by getting calling class
String methodName, // This is optional if not provided, then it will automatically be taken by getting calling method
@required String text,
Exception exception,
String dataLogType,
StackTrace stacktrace,
}){}
3. debug
记录字符串数据以及类名、方法名、日志文本和日志类型(LogLevel.SEVERE、LogLevel.INFO)等。相同的方法可用于记录异常或数据日志。如果未提供类名或方法名,它将通过获取调用类和方法来自动获取。
static debug({
String className, // This is optional if not provided, then it will automatically be taken by getting calling class
String methodName, // This is optional if not provided, then it will automatically be taken by getting calling method
@required String text,
Exception exception,
String dataLogType,
StackTrace stacktrace,
}){}
4. info
记录字符串数据以及类名、方法名、日志文本和日志类型(LogLevel.SEVERE、LogLevel.INFO)等。相同的方法可用于记录异常或数据日志。如果未提供类名或方法名,它将通过获取调用类和方法来自动获取。
static info({
String className, // This is optional if not provided, then it will automatically be taken by getting calling class
String methodName, // This is optional if not provided, then it will automatically be taken by getting calling method
@required String text,
Exception exception,
String dataLogType,
StackTrace stacktrace,
}){}
5. warning
记录字符串数据以及类名、方法名、日志文本和日志类型(LogLevel.SEVERE、LogLevel.INFO)等。相同的方法可用于记录异常或数据日志。如果未提供类名或方法名,它将通过获取调用类和方法来自动获取。
static warning({
String className, // This is optional if not provided, then it will automatically be taken by getting calling class
String methodName, // This is optional if not provided, then it will automatically be taken by getting calling method
@required String text,
Exception exception,
String dataLogType,
StackTrace stacktrace,
}){}
6. error
记录字符串数据以及类名、方法名、日志文本和日志类型(LogLevel.SEVERE、LogLevel.INFO)等。相同的方法可用于记录异常或数据日志。如果未提供类名或方法名,它将通过获取调用类和方法来自动获取。
static error({
String className, // This is optional if not provided, then it will automatically be taken by getting calling class
String methodName, // This is optional if not provided, then it will automatically be taken by getting calling method
@required String text,
Exception exception,
String dataLogType,
StackTrace stacktrace,
}){}
7. severe
记录字符串数据以及类名、方法名、日志文本和日志类型(LogLevel.SEVERE、LogLevel.INFO)等。相同的方法可用于记录异常或数据日志。如果未提供类名或方法名,它将通过获取调用类和方法来自动获取。
static severe({
String className, // This is optional if not provided, then it will automatically be taken by getting calling class
String methodName, // This is optional if not provided, then it will automatically be taken by getting calling method
@required String text,
Exception exception,
String dataLogType,
StackTrace stacktrace,
}){}
8. fatal
记录字符串数据以及类名、方法名、日志文本和日志类型(LogLevel.SEVERE、LogLevel.INFO)等。相同的方法可用于记录异常或数据日志。如果未提供类名或方法名,它将通过获取调用类和方法来自动获取。
static fatal({
String className, // This is optional if not provided, then it will automatically be taken by getting calling class
String methodName, // This is optional if not provided, then it will automatically be taken by getting calling method
@required String text,
Exception exception,
String dataLogType,
StackTrace stacktrace,
}){}
9. printLogs
从数据库获取所有日志,并使用 StringBuffer() 将它们作为字符串打印
static printLogs() async {}
10. getAllLogsByCustomFilter
接受过滤器列表作为参数,并根据提供的过滤器返回日志列表。过滤器及其用法在 wiki 中进行了说明,请查看 wiki 以获取更多详细信息。
List<Filter> filters = [Filter.greaterThan('[FieldName]', '[Value]')]
static Future<List<Log>> getAllLogsByCustomFilter(
{List<Filter> filters}) async {}
11. getAllLogsByFilter
一个便捷的方法,可根据提供的过滤器参数过滤数据,例如 dataLogsType(DataLogType.DEVICE、DataLogType.NETWORK)、logLevels(LogLevel.SEVERE、LogLevel.INFO)、startTimeInMillis(您要从中获取日志的当日毫秒数)、endTimeInMillis(您要获取日志的当日截止毫秒数)和 filterType(FilterType.LAST_HOUR、FilterType.LAST_24_HOURS、FilterType.TODAY、FilterType.WEEK、FilterType.ALL)。FilterType 不能与 `startTimeInMillis`、`endTimeInMillis` 一起使用,如果使用,则 `startTimeInMillis`、`endTimeInMillis` 将具有优先权。为了完全控制过滤器,请使用上面提供的方法。
static Future<List<Log>> getAllLogsByFilter(
{List<String> dataLogsType,
List<String> logLevels,
int startTimeInMillis,
int endTimeInMillis,
FilterType filterType}}) async {}
12. getAllLogs
从数据库获取所有日志并返回日志列表。
static Future<List<Log>> getAllLogs() async {}
13. exportLogs
将日志导出到 FLog 目录下的外部存储。
static exportLogs() async {}
14. clearLogs
清除数据库中存储的所有日志。
static clearLogs() {}
15. applyConfigurations
将用户提供的配置应用于 FLogs。
static applyConfigurations(LogsConfig config) {}
16. deleteAllLogsByFilter
接受过滤器列表作为参数,并根据提供的过滤器删除日志。过滤器及其用法在 wiki 中进行了说明,请查看 wiki 以获取更多详细信息。
List<Filter> filters = [Filter.greaterThan('[FieldName]', '[Value]')]
static deleteAllLogsByFilter(
{List<Filter> filters}) async {}