Browse Source

日志优化,增加文件日志,方便排查问题

gjh 1 week ago
parent
commit
df7a8e2b85
2 changed files with 57 additions and 8 deletions
  1. 32 6
      lib/funcs.dart
  2. 25 2
      lib/main.dart

+ 32 - 6
lib/funcs.dart

@@ -23,6 +23,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
23 23
 import 'package:logger/logger.dart';
24 24
 import 'package:network_info_plus/network_info_plus.dart';
25 25
 import 'package:package_info_plus/package_info_plus.dart';
26
+import 'package:path_provider/path_provider.dart';
26 27
 import 'package:permission_handler/permission_handler.dart';
27 28
 
28 29
 import 'global.dart';
@@ -30,7 +31,27 @@ import 'global.dart';
30 31
 ///国际化
31 32
 S getS() => S.of(navigatorKey.currentState!.context);
32 33
 
33
-Logger logger = Logger(printer: PrettyPrinter(methodCount: 5));
34
+late Logger logger;
35
+
36
+///初始化日志
37
+Future<dynamic> initLog() async {
38
+  LogOutput? output;
39
+  Directory? dir = await getDownloadsDirectory();
40
+  if (dir != null) {
41
+    dir = Directory("${dir.path}/logs");
42
+    if (!await dir.exists()) {
43
+      await dir.create(recursive: true);
44
+    }
45
+    File file =
46
+        File("${dir.path}/log_${DateTime.now().millisecondsSinceEpoch}.log");
47
+    output = MultiOutput(
48
+        [ConsoleOutput(), FileOutput(file: file, overrideExisting: true)]);
49
+  }
50
+  logger = Logger(
51
+      printer: PrettyPrinter(
52
+          methodCount: 5, dateTimeFormat: DateTimeFormat.dateAndTime),
53
+      output: output);
54
+}
34 55
 
35 56
 void logd(
36 57
   dynamic message, {
@@ -38,7 +59,8 @@ void logd(
38 59
   Object? error,
39 60
   StackTrace? stackTrace,
40 61
 }) {
41
-  logger.d(message, time: time, error: error, stackTrace: stackTrace);
62
+  logger.d(message,
63
+      time: time ?? DateTime.now(), error: error, stackTrace: stackTrace);
42 64
 }
43 65
 
44 66
 void logi(
@@ -47,7 +69,8 @@ void logi(
47 69
   Object? error,
48 70
   StackTrace? stackTrace,
49 71
 }) {
50
-  logger.i(message, time: time, error: error, stackTrace: stackTrace);
72
+  logger.i(message,
73
+      time: time ?? DateTime.now(), error: error, stackTrace: stackTrace);
51 74
 }
52 75
 
53 76
 void loge(
@@ -56,7 +79,8 @@ void loge(
56 79
   Object? error,
57 80
   StackTrace? stackTrace,
58 81
 }) {
59
-  logger.e(message, time: time, error: error, stackTrace: stackTrace);
82
+  logger.e(message,
83
+      time: time ?? DateTime.now(), error: error, stackTrace: stackTrace);
60 84
 }
61 85
 
62 86
 void logw(
@@ -65,7 +89,8 @@ void logw(
65 89
   Object? error,
66 90
   StackTrace? stackTrace,
67 91
 }) {
68
-  logger.w(message, time: time, error: error, stackTrace: stackTrace);
92
+  logger.w(message,
93
+      time: time ?? DateTime.now(), error: error, stackTrace: stackTrace);
69 94
 }
70 95
 
71 96
 void logf(
@@ -74,7 +99,8 @@ void logf(
74 99
   Object? error,
75 100
   StackTrace? stackTrace,
76 101
 }) {
77
-  logger.f(message, time: time, error: error, stackTrace: stackTrace);
102
+  logger.f(message,
103
+      time: time ?? DateTime.now(), error: error, stackTrace: stackTrace);
78 104
 }
79 105
 
80 106
 ///显示toast

+ 25 - 2
lib/main.dart

@@ -1,4 +1,7 @@
1
+import 'dart:io';
2
+
1 3
 import 'package:bot_toast/bot_toast.dart';
4
+import 'package:device_info_plus/device_info_plus.dart';
2 5
 import 'package:eitc_erm_dental_flutter/db_util.dart';
3 6
 import 'package:eitc_erm_dental_flutter/funcs.dart';
4 7
 import 'package:eitc_erm_dental_flutter/sp_util.dart';
@@ -10,6 +13,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
10 13
 import 'package:flutter_riverpod/flutter_riverpod.dart';
11 14
 import 'package:flutter_screenutil/flutter_screenutil.dart';
12 15
 import 'package:image/image.dart' as img;
16
+import 'package:package_info_plus/package_info_plus.dart';
13 17
 
14 18
 import 'app_router.dart';
15 19
 import 'generated/assets.dart';
@@ -20,8 +24,6 @@ import 'global.dart';
20 24
 final GlobalKey<NavigatorState> navigatorKey = GlobalKey();
21 25
 
22 26
 void main() async {
23
-  logd("====================app启动,flavor=$appFlavor====================");
24
-
25 27
   ///提前实例化WidgetsBinding
26 28
   WidgetsFlutterBinding.ensureInitialized();
27 29
 
@@ -39,6 +41,27 @@ void main() async {
39 41
 
40 42
 ///初始化设置
41 43
 Future _initSettings() async {
44
+  //初始化日志
45
+  await initLog();
46
+
47
+  PackageInfo packageInfo = await PackageInfo.fromPlatform();
48
+  BaseDeviceInfo? deviceInfo;
49
+  if (Platform.isAndroid) {
50
+    deviceInfo = await DeviceInfoPlugin().androidInfo;
51
+  } else if (Platform.isIOS) {
52
+    deviceInfo = await DeviceInfoPlugin().iosInfo;
53
+  }
54
+  logd("""
55
+    ====================app启动====================
56
+    appName=${packageInfo.appName}
57
+    flavor=$appFlavor
58
+    packageName=${packageInfo.packageName}
59
+    version=${packageInfo.version}
60
+    buildNumber=${packageInfo.buildNumber}
61
+    deviceInfo=${deviceInfo?.data ?? "none"}
62
+    ==============================================
63
+   """);
64
+
42 65
   //更新token
43 66
   updateToken(await SpUtil.getToken());
44 67
   //更新选择的咨询人ID