-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathflutter_logs.dart
395 lines (362 loc) · 12.2 KB
/
flutter_logs.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import 'dart:async';
import 'package:flutter/services.dart';
enum DirectoryStructure { FOR_DATE, FOR_EVENT, SINGLE_FILE_FOR_DAY }
enum LogFileExtension { TXT, CSV, LOG, NONE }
enum LogLevel { INFO, WARNING, ERROR, SEVERE }
enum LogType {
Device,
Location,
Notification,
Network,
Navigation,
History,
Tasks,
Jobs,
Errors
}
enum TimeStampFormat {
DATE_FORMAT_1,
DATE_FORMAT_2,
TIME_FORMAT_FULL_JOINED,
TIME_FORMAT_FULL_1,
TIME_FORMAT_FULL_2,
TIME_FORMAT_24_FULL,
TIME_FORMAT_READABLE,
TIME_FORMAT_READABLE_2,
TIME_FORMAT_SIMPLE
}
enum ExportType { TODAY, LAST_HOUR, WEEKS, LAST_24_HOURS, ALL }
class FlutterLogs {
// 0 = no messages, 1 = only errors, 2 = all
static int _debugLevel = 2;
/// Set the message level value [value] for debugging purpose. 0 = no messages, 1 = errors, 2 = all
static void setDebugLevel(int value) {
_debugLevel = value;
}
// Send the message [msg] with the [msgDebugLevel] value. 1 = error, 2 = info
static void printDebugMessage(String msg, int msgDebugLevel) {
if (_debugLevel >= msgDebugLevel) {
print('flutter_logs: $msg');
}
}
static const MethodChannel channel = const MethodChannel('flutter_logs');
static Future<String> initLogs(
{List<LogLevel>? logLevelsEnabled,
List<String>? logTypesEnabled,
int logsRetentionPeriodInDays = 14,
int zipsRetentionPeriodInDays = 3,
bool autoDeleteZipOnExport = false,
bool autoClearLogs = true,
bool autoExportErrors = true,
bool encryptionEnabled = false,
String encryptionKey = "",
required DirectoryStructure directoryStructure,
bool logSystemCrashes = true,
bool isDebuggable = true,
bool debugFileOperations = true,
bool attachTimeStamp = true,
bool attachNoOfFiles = true,
required TimeStampFormat timeStampFormat,
required LogFileExtension logFileExtension,
bool zipFilesOnly = false,
String logsWriteDirectoryName = "",
String logsExportZipFileName = "",
String logsExportDirectoryName = "",
int singleLogFileSize = 2,
bool enabled = true}) async {
var directoryStructureString = _getDirectoryStructure(directoryStructure);
var timeStampFormatString = _getTimeStampFormat(timeStampFormat);
var logFileExtensionString = _getLogFileExtension(logFileExtension);
var logLevelsEnabledList =
logLevelsEnabled?.map((e) => _getLogLevel(e)) ?? <String>[];
final String result =
await channel.invokeMethod('initLogs', <String, dynamic>{
'logLevelsEnabled': logLevelsEnabledList.join(','),
'logTypesEnabled': logTypesEnabled?.join(',') ?? '',
'logsRetentionPeriodInDays': logsRetentionPeriodInDays,
'zipsRetentionPeriodInDays': zipsRetentionPeriodInDays,
'autoDeleteZipOnExport': autoDeleteZipOnExport,
'autoClearLogs': autoClearLogs,
'autoExportErrors': autoExportErrors,
'encryptionEnabled': encryptionEnabled,
'encryptionKey': encryptionKey,
'directoryStructure': directoryStructureString,
'logSystemCrashes': logSystemCrashes,
'isDebuggable': isDebuggable,
'debugFileOperations': debugFileOperations,
'attachTimeStamp': attachTimeStamp,
'attachNoOfFiles': attachNoOfFiles,
'timeStampFormat': timeStampFormatString,
'logFileExtension': logFileExtensionString,
'zipFilesOnly': zipFilesOnly,
'savePath': logsWriteDirectoryName,
'zipFileName': logsExportZipFileName,
'exportPath': logsExportDirectoryName,
'singleLogFileSize': singleLogFileSize,
'enabled': enabled,
});
printDebugMessage(result, 2);
return result;
}
static Future<String?> initMQTT(
{String topic = "",
String brokerUrl = "",
String certificate = "",
String clientId = "",
String port = "",
int qos = 0,
bool retained = false,
bool writeLogsToLocalStorage = true,
bool debug = true,
int initialDelaySecondsForPublishing = 30}) async {
if (brokerUrl.isNotEmpty && certificate.isNotEmpty) {
final ByteData bytes = await rootBundle.load('$certificate');
return await channel.invokeMethod('initMQTT', <String, dynamic>{
'topic': topic,
'brokerUrl': brokerUrl,
'certificate': bytes.buffer.asUint8List(),
'clientId': clientId,
'port': port,
'qos': qos,
'retained': retained,
'writeLogsToLocalStorage': writeLogsToLocalStorage,
'debug': debug,
'initialDelaySecondsForPublishing': initialDelaySecondsForPublishing
});
}
}
static Future<String> setMetaInfo({
String appId = "",
String appName = "",
String appVersion = "",
String language = "",
String deviceId = "",
String environmentId = "",
String environmentName = "",
String organizationId = "",
String organizationUnitId = "",
String userId = "",
String userName = "",
String userEmail = "",
String deviceSerial = "",
String deviceBrand = "",
String deviceName = "",
String deviceManufacturer = "",
String deviceModel = "",
String deviceSdkInt = "",
String deviceBatteryPercent = "",
String latitude = "",
String longitude = "",
String labels = "",
}) async {
return await channel.invokeMethod('setMetaInfo', <String, dynamic>{
'appId': appId,
'appName': appName,
'appVersion': appVersion,
'language': language,
'deviceId': deviceId,
'environmentId': environmentId,
'environmentName': environmentName,
'organizationId': organizationId,
'organizationUnitId': organizationUnitId,
'userId': userId,
'userName': userName,
'userEmail': userEmail,
'deviceSerial': deviceSerial,
'deviceBrand': deviceBrand,
'deviceName': deviceName,
'deviceManufacturer': deviceManufacturer,
'deviceModel': deviceModel,
'deviceSdkInt': deviceSdkInt,
'deviceBatteryPercent': deviceBatteryPercent,
'latitude': latitude,
'longitude': longitude,
'labels': labels
});
}
static Future<void> logThis(
{String tag = "",
String subTag = "",
String logMessage = "",
LogLevel level = LogLevel.INFO,
Exception? exception,
Error? error,
String errorMessage = ""}) async {
if (exception != null) {
final String result =
await channel.invokeMethod('logThis', <String, dynamic>{
'tag': tag,
'subTag': subTag,
'logMessage': "$logMessage , Error: ${error.toString()}",
'level': _getLogLevel(level),
'e': exception.toString()
});
printDebugMessage(result, 2);
} else if (error != null) {
final String result =
await channel.invokeMethod('logThis', <String, dynamic>{
'tag': tag,
'subTag': subTag,
'logMessage': "$logMessage , Error: ${error.toString()}",
'level': _getLogLevel(level),
'e': error.stackTrace.toString()
});
printDebugMessage(result, 2);
} else if (errorMessage != null && errorMessage.isNotEmpty) {
final String result =
await channel.invokeMethod('logThis', <String, dynamic>{
'tag': tag,
'subTag': subTag,
'logMessage': "$logMessage , Error: $errorMessage",
'level': _getLogLevel(level)
});
printDebugMessage(result, 2);
} else {
final String result =
await channel.invokeMethod('logThis', <String, dynamic>{
'tag': tag,
'subTag': subTag,
'logMessage': logMessage,
'level': _getLogLevel(level)
});
printDebugMessage(result, 2);
}
}
static Future<void> logInfo(
String tag, String subTag, String logMessage) async {
final String result =
await channel.invokeMethod('logThis', <String, dynamic>{
'tag': tag,
'subTag': subTag,
'logMessage': logMessage,
'level': _getLogLevel(LogLevel.INFO)
});
printDebugMessage(result, 2);
}
static Future<void> logWarn(
String tag, String subTag, String logMessage) async {
final String result =
await channel.invokeMethod('logThis', <String, dynamic>{
'tag': tag,
'subTag': subTag,
'logMessage': logMessage,
'level': _getLogLevel(LogLevel.WARNING)
});
printDebugMessage(result, 2);
}
static Future<void> logError(
String tag, String subTag, String logMessage) async {
final String result =
await channel.invokeMethod('logThis', <String, dynamic>{
'tag': tag,
'subTag': subTag,
'logMessage': logMessage,
'level': _getLogLevel(LogLevel.ERROR)
});
printDebugMessage(result, 2);
}
static Future<void> logErrorTrace(
String tag, String subTag, String logMessage, Error e) async {
final String result =
await channel.invokeMethod('logThis', <String, dynamic>{
'tag': tag,
'subTag': subTag,
'logMessage': logMessage,
'e': e.stackTrace.toString(),
'level': _getLogLevel(LogLevel.ERROR)
});
printDebugMessage(result, 2);
}
static Future<void> logToFile(
{String logFileName = "",
bool overwrite = false,
String logMessage = "",
bool appendTimeStamp = false}) async {
if (logFileName.isNotEmpty) {
final String result =
await channel.invokeMethod('logToFile', <String, dynamic>{
'logFileName': logFileName,
'overwrite': overwrite,
'logMessage': logMessage,
'appendTimeStamp': appendTimeStamp
});
printDebugMessage(result, 2);
} else {
print("Error: \'logFileName\' required.");
}
}
static Future<void> exportLogs(
{ExportType exportType = ExportType.ALL,
bool decryptBeforeExporting = false}) async {
final String result =
await channel.invokeMethod('exportLogs', <String, dynamic>{
'exportType': _getExportType(exportType),
'decryptBeforeExporting': decryptBeforeExporting
});
printDebugMessage(result, 2);
}
static Future<void> printLogs(
{ExportType exportType = ExportType.ALL,
bool decryptBeforeExporting = false}) async {
final String result =
await channel.invokeMethod('printLogs', <String, dynamic>{
'exportType': _getExportType(exportType),
'decryptBeforeExporting': decryptBeforeExporting
});
printDebugMessage(result, 2);
}
static Future<void> exportFileLogForName(
{String logFileName = "", bool decryptBeforeExporting = false}) async {
if (logFileName.isNotEmpty) {
final String result = await channel.invokeMethod(
'exportFileLogForName', <String, dynamic>{
'logFileName': logFileName,
'decryptBeforeExporting': decryptBeforeExporting
});
printDebugMessage(result, 2);
} else {
print("Error: \'logFileName\' required.");
}
}
static Future<void> exportAllFileLogs(
{bool decryptBeforeExporting = false}) async {
final String result = await channel.invokeMethod('exportAllFileLogs',
<String, dynamic>{'decryptBeforeExporting': decryptBeforeExporting});
printDebugMessage(result, 2);
}
static Future<void> printFileLogForName(
{String logFileName = "", bool decryptBeforeExporting = false}) async {
if (logFileName.isNotEmpty) {
final String result = await channel.invokeMethod(
'printFileLogForName', <String, dynamic>{
'logFileName': logFileName,
'decryptBeforeExporting': decryptBeforeExporting
});
printDebugMessage(result, 2);
} else {
print("Error: \'logFileName\' required.");
}
}
static Future<void> clearLogs() async {
final String result = await channel.invokeMethod('clearLogs');
printDebugMessage(result, 2);
}
static String _getDirectoryStructure(DirectoryStructure type) {
return type.toString().split('.').last;
}
static String _getLogFileExtension(LogFileExtension type) {
return type.toString().split('.').last;
}
static String _getLogLevel(LogLevel type) {
return type.toString().split('.').last;
}
static String _getLogType(LogType type) {
return type.toString().split('.').last;
}
static String _getTimeStampFormat(TimeStampFormat type) {
return type.toString().split('.').last;
}
static String _getExportType(ExportType type) {
return type.toString().split('.').last;
}
}