logging framework

This commit is contained in:
austinried
2025-12-14 10:09:32 +09:00
parent 7f6ba4776a
commit 2837d4576e
8 changed files with 223 additions and 42 deletions

View File

@@ -7,6 +7,7 @@ import '../sources/models.dart' as models;
import 'converters.dart';
import 'dao/library_dao.dart';
import 'dao/sources_dao.dart';
import 'log_interceptor.dart';
part 'database.g.dart';
@@ -27,14 +28,14 @@ class SubtracksDatabase extends _$SubtracksDatabase {
static QueryExecutor _openConnection() {
return driftDatabase(
name: 'my_database',
name: 'subtracks_database',
native: DriftNativeOptions(
databasePath: () async {
final directory = await getApplicationSupportDirectory();
return path.join(directory.absolute.path, 'subtracks.sqlite');
},
),
);
).interceptWith(LogInterceptor());
}
@override

View File

@@ -0,0 +1,120 @@
import 'dart:async';
import 'package:drift/drift.dart';
import 'package:logger/logger.dart';
import '../util/logger.dart';
/// https://drift.simonbinder.eu/examples/tracing/
class LogInterceptor extends QueryInterceptor {
Future<T> _run<T>(
String description,
FutureOr<T> Function() operation,
) async {
final trace = logger.level >= Level.trace;
final stopwatch = trace ? (Stopwatch()..start()) : null;
logger.t('Running $description');
try {
final result = await operation();
if (trace) {
logger.t(' => succeeded after ${stopwatch!.elapsedMilliseconds}ms');
}
return result;
} on Object catch (e, st) {
if (trace) {
logger.t(' => failed after ${stopwatch!.elapsedMilliseconds}ms');
}
logger.e('Query failed', error: e, stackTrace: st);
rethrow;
}
}
@override
TransactionExecutor beginTransaction(QueryExecutor parent) {
logger.t('begin');
return super.beginTransaction(parent);
}
@override
Future<void> commitTransaction(TransactionExecutor inner) {
return _run('commit', () => inner.send());
}
@override
Future<void> rollbackTransaction(TransactionExecutor inner) {
return _run('rollback', () => inner.rollback());
}
@override
Future<void> runBatched(
QueryExecutor executor,
BatchedStatements statements,
) {
return _run(
'batch with $statements',
() => executor.runBatched(statements),
);
}
@override
Future<int> runInsert(
QueryExecutor executor,
String statement,
List<Object?> args,
) {
return _run(
'$statement with $args',
() => executor.runInsert(statement, args),
);
}
@override
Future<int> runUpdate(
QueryExecutor executor,
String statement,
List<Object?> args,
) {
return _run(
'$statement with $args',
() => executor.runUpdate(statement, args),
);
}
@override
Future<int> runDelete(
QueryExecutor executor,
String statement,
List<Object?> args,
) {
return _run(
'$statement with $args',
() => executor.runDelete(statement, args),
);
}
@override
Future<void> runCustom(
QueryExecutor executor,
String statement,
List<Object?> args,
) {
return _run(
'$statement with $args',
() => executor.runCustom(statement, args),
);
}
@override
Future<List<Map<String, Object?>>> runSelect(
QueryExecutor executor,
String statement,
List<Object?> args,
) {
return _run(
'$statement with $args',
() => executor.runSelect(statement, args),
);
}
}