recordLogs function

Stream<LogRecord> recordLogs(
  1. dynamic run(), {
  2. String name = '',
})

Executes run with a new Logger, returning the resulting log records.

The returned Stream is closed after the run function is executed. If run returns a Future, that future is awaited before the stream is closed.

test('should log "uh oh!"', () async {
  final logs = recordLogs(() => runBuilder());
  expect(logs, emitsInOrder([
    anyLogOf('uh oh!'),
  ]);
});

Implementation

Stream<LogRecord> recordLogs(dynamic Function() run, {String name = ''}) {
  final logger = Logger(name);
  Timer.run(() async {
    await scopeLogAsync(() => Future.value(run()), logger);
    logger.clearListeners();
  });
  return logger.onRecord;
}