debugMiddleware function
Debugging middleware
Implementation
Middleware debugMiddleware({int maxLogs = 100}) {
return (HttpRequest request, Function next) async {
final stopwatch = Stopwatch()..start();
try {
final requestLog = {
'method': request.method,
'uri': request.uri.toString(),
'headers': request.headers,
'body': await _readRequestBody(request),
'timestamp': DateTime.now().toIso8601String(),
};
await next();
stopwatch.stop();
requestLog['responseTimeMs'] = stopwatch.elapsedMilliseconds;
logs.add(requestLog);
if (logs.length > maxLogs) logs.removeAt(0);
} catch (e, stackTrace) {
logs.add({
'method': request.method,
'uri': request.uri.toString(),
'error': e.toString(),
'stackTrace': stackTrace.toString(),
'timestamp': DateTime.now().toIso8601String(),
});
rethrow;
}
};
}