waitForLogs function

Future<Duration> waitForLogs(
  1. WaitStrategyTarget container,
  2. Pattern predicate, {
  3. Duration? timeout,
  4. Duration interval = const Duration(seconds: 1),
  5. bool predicateStreamsAnd = false,
})

Convenience function that waits for container log output to match a pattern.

Creates a LogMessageWaitStrategy internally and invokes it immediately. Returns the elapsed wait time as a Duration.

Parameters:

  • container — the container to watch.
  • predicate — a String (compiled to RegExp), RegExp, or any Pattern to search for.
  • timeout — optional startup-timeout override. Defaults to TestcontainersConfiguration.timeout (120 seconds).
  • interval — poll interval. Default: 1 second.
  • predicateStreamsAnd — when true, predicate must match both stdout and stderr. When false (default) matching either stream is sufficient.

Example:

await waitForLogs(container, 'Server started on port');

Implementation

Future<Duration> waitForLogs(
  WaitStrategyTarget container,
  Pattern predicate, {
  Duration? timeout,
  Duration interval = const Duration(seconds: 1),
  bool predicateStreamsAnd = false,
}) async {
  final effectiveTimeout = timeout ??
      Duration(milliseconds: (testcontainersConfig.timeout * 1000).toInt());
  final strategy = LogMessageWaitStrategy(
    predicate,
    predicateStreamsAnd: predicateStreamsAnd,
  )
    ..withStartupTimeout(effectiveTimeout)
    ..withPollInterval(interval);
  final start = DateTime.now();
  await strategy.waitUntilReady(container);
  return DateTime.now().difference(start);
}