3

I have a functional test that is failing, and I think that there could be some useful information in the database log, but unfortunately the test suite only runs in containers on GitLab CI so I don't have any way to get to the database log while the environment is running.

Is there any easy way to get a dump of the contents of the watchdog log in a test artifact?

3

The Watchdog to File module is handy for this use case. You can initialize it in a test setup, like so:

<?php

namespace Drupal\Tests\my_module\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * My browser tests.
 */
class MyBrowserTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->enableFileLogging();
  }

  /**
   * Enables logging of ALL watchdog events to a file.
   */
  protected function enableFileLogging() {
    $installer = \Drupal::service('module_installer');

    $installer->install([
      'watchdog_file',
    ]);

    $config_factory = \Drupal::configFactory();
    $config         = $config_factory->getEditable('watchdog_file.settings');

    $log_path =
      sprintf(
        '%s/artifacts/watchdog-%s.log',
        dirname(DRUPAL_ROOT),
        $this->databasePrefix
      );

    $config->set('log_filename', $log_path);
    $config->save(TRUE);
  }

}

Then, just configure GitLab CI to capture the test artifacts folder:

  artifacts:
    when: always
    reports:
      junit: artifacts/junit/*.xml
    paths:
      - artifacts/*
      - web/sites/simpletest/*

As a bonus, the above snippet also captures browser output produced by browser tests, in case you need to review them as well.

Each watchdog log will be named according to the Simple Test database ID that is used for the corresponding test run. For example, if you have five cases in your test, you should end up with five watchdog file logs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.