developmentLogger static method
Middleware
developmentLogger(
{ - bool logRequests = true,
- bool logErrors = true,
- bool includeClientIp = true,
})
Implementation
static Middleware developmentLogger({
bool logRequests = true,
bool logErrors = true,
bool includeClientIp = true,
}) {
return (request, next) async {
if (!logRequests) {
return await next();
}
final stopwatch = Stopwatch()..start();
final clientIp = includeClientIp ? (request.clientIp ?? 'unknown') : null;
try {
final response = await next();
stopwatch.stop();
final shelfResponse = response.toShelfResponse();
DevLogger.logRequest(
method: request.method,
path: request.url.path + (request.url.query.isNotEmpty ? '?${request.url.query}' : ''),
statusCode: shelfResponse.statusCode,
responseTimeMs: stopwatch.elapsedMilliseconds,
clientIp: clientIp,
);
return response;
} catch (e, stackTrace) {
stopwatch.stop();
if (logErrors) {
DevLogger.logError(
e,
stackTrace,
context: '${request.method} ${request.url.path}',
);
}
DevLogger.logRequest(
method: request.method,
path: request.url.path + (request.url.query.isNotEmpty ? '?${request.url.query}' : ''),
statusCode: 500,
responseTimeMs: stopwatch.elapsedMilliseconds,
clientIp: clientIp,
);
rethrow;
}
};
}