only delete logs older than 30 days automatically

This commit is contained in:
julian 2023-02-13 15:08:38 -06:00
parent 4b1ff8fd09
commit 9f3ce454b5
5 changed files with 45 additions and 46 deletions

View file

@ -112,7 +112,7 @@ void main() async {
await DebugService.instance.init(isar);
// clear out all info logs on startup. No need to await and block
unawaited(DebugService.instance.purgeInfoLogs());
unawaited(DebugService.instance.deleteLogsOlderThan());
}
// Registering Transaction Model Adapters

View file

@ -85,7 +85,6 @@ class _DebugViewState extends ConsumerState<DebugView> {
@override
void initState() {
ref.read(debugServiceProvider).updateRecentLogs();
super.initState();
}
@ -181,10 +180,7 @@ class _DebugViewState extends ConsumerState<DebugView> {
await ref
.read(debugServiceProvider)
.deleteAllMessages();
await ref
.read(debugServiceProvider)
.updateRecentLogs();
.deleteAllLogs();
shouldPop = true;
@ -194,6 +190,8 @@ class _DebugViewState extends ConsumerState<DebugView> {
type: FlushBarType.info,
context: context,
message: 'Logs cleared!'));
setState(() {});
}
},
),
@ -313,7 +311,7 @@ class _DebugViewState extends ConsumerState<DebugView> {
_searchTerm)
.reversed
.toList(growable: false);
List errorLogs = [];
List<String> errorLogs = [];
for (var log in logs) {
if (log.logLevel == LogLevel.Error ||
log.logLevel == LogLevel.Fatal) {
@ -406,14 +404,14 @@ class _DebugViewState extends ConsumerState<DebugView> {
),
));
bool logssaved = true;
var filename;
bool logsSaved = true;
String? filename;
try {
filename = await ref
.read(debugServiceProvider)
.exportToFile(path, eventBus);
} catch (e, s) {
logssaved = false;
logsSaved = false;
Logging.instance
.log("$e $s", level: LogLevel.Error);
}
@ -428,7 +426,7 @@ class _DebugViewState extends ConsumerState<DebugView> {
showDialog(
context: context,
builder: (context) => StackOkDialog(
title: logssaved
title: logsSaved
? "Logs saved to"
: "Error Saving Logs",
message: "${path!}/$filename",
@ -440,7 +438,7 @@ class _DebugViewState extends ConsumerState<DebugView> {
showFloatingFlushBar(
type: FlushBarType.info,
context: context,
message: logssaved
message: logsSaved
? 'Logs file saved'
: "Error Saving Logs",
),

View file

@ -110,7 +110,7 @@ class HiddenSettings extends StatelessWidget {
onTap: () async {
await ref
.read(debugServiceProvider)
.deleteAllMessages();
.deleteAllLogs();
unawaited(showFloatingFlushBar(
type: FlushBarType.success,

View file

@ -8,7 +8,6 @@ import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/providers/global/debug_service_provider.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/flush_bar_type.dart';
import 'package:stackwallet/utilities/enums/log_level_enum.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
@ -73,7 +72,6 @@ class _DebugInfoDialog extends ConsumerState<DebugInfoDialog> {
searchDebugController = TextEditingController();
searchDebugFocusNode = FocusNode();
ref.read(debugServiceProvider).updateRecentLogs();
super.initState();
}
@ -109,7 +107,7 @@ class _DebugInfoDialog extends ConsumerState<DebugInfoDialog> {
],
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 32),
padding: const EdgeInsets.symmetric(horizontal: 32),
child: ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
@ -319,8 +317,8 @@ class _DebugInfoDialog extends ConsumerState<DebugInfoDialog> {
child: SecondaryButton(
label: "Clear logs",
onPressed: () async {
await ref.read(debugServiceProvider).deleteAllMessages();
await ref.read(debugServiceProvider).updateRecentLogs();
await ref.read(debugServiceProvider).deleteAllLogs();
setState(() {});
if (mounted) {
Navigator.pop(context);

View file

@ -15,8 +15,6 @@ class DebugService extends ChangeNotifier {
late final Isar isar;
// late final Stream<void> logsChanged;
final int numberOfRecentLogsToLoad = 500;
// bool _shouldPause = false;
//
// void togglePauseUiUpdates() {
@ -36,44 +34,49 @@ class DebugService extends ChangeNotifier {
// });
}
List<Log> _recentLogs = [];
List<Log> get recentLogs => _recentLogs;
List<Log> get recentLogs => isar.logs.where().limit(200).findAllSync();
Future<void> updateRecentLogs() async {
int totalCount = await isar.logs.count();
int offset = totalCount - numberOfRecentLogsToLoad;
if (offset < 0) {
offset = 0;
}
// Future<void> updateRecentLogs() async {
// int totalCount = await isar.logs.count();
// int offset = totalCount - numberOfRecentLogsToLoad;
// if (offset < 0) {
// offset = 0;
// }
//
// _recentLogs = (await isar.logs
// .where()
// .anyTimestampInMillisUTC()
// .offset(offset)
// .limit(numberOfRecentLogsToLoad)
// .findAll());
// notifyListeners();
// }
_recentLogs = (await isar.logs
.where()
.anyTimestampInMillisUTC()
.offset(offset)
.limit(numberOfRecentLogsToLoad)
.findAll());
notifyListeners();
}
Future<void> deleteAllMessages() async {
Future<bool> deleteAllLogs() async {
try {
await isar.writeTxn(() async => await isar.logs.clear());
notifyListeners();
} catch (e, s) {
//todo: come back to this
debugPrint("$e, $s");
return true;
} catch (_) {
return false;
}
}
Future<void> purgeInfoLogs() async {
final now = DateTime.now();
Future<void> deleteLogsOlderThan({
Duration timeframe = const Duration(days: 30),
}) async {
final cutoffDate = DateTime.now().subtract(timeframe).toUtc();
await isar.writeTxn(() async {
await isar.logs.filter().logLevelEqualTo(LogLevel.Info).deleteAll();
await isar.logs
.where()
.timestampInMillisUTCLessThan(cutoffDate.millisecondsSinceEpoch)
.deleteAll();
});
Logging.instance.log(
"Info logs purged in ${DateTime.now().difference(now).inMilliseconds} milliseconds",
level: LogLevel.Info);
"Logs older than $cutoffDate cleared!",
level: LogLevel.Info,
);
}
/// returns the filename of the saved logs file