3 namespace Drupal\Tests\dblog\Functional;
5 use Drupal\Core\Logger\RfcLogLevel;
6 use Drupal\Core\Session\AnonymousUserSession;
9 * Provides methods to generate log entries.
11 * This trait is meant to be used only by test classes.
13 trait FakeLogEntries {
16 * Generates a number of random database log events.
19 * Number of watchdog entries to generate.
20 * @param array $options
21 * These options are used to override the defaults for the test.
22 * An associative array containing any of the following keys:
23 * - 'channel': String identifying the log channel to be output to.
24 * If the channel is not set, the default of 'custom' will be used.
25 * - 'message': String containing a message to be output to the log.
26 * A simple default message is used if not provided.
27 * - 'variables': Array of variables that match the message string.
28 * - 'severity': Log severity level as defined in logging_severity_levels.
29 * - 'link': String linking to view the result of the event.
30 * - 'user': String identifying the username.
31 * - 'uid': Int identifying the user id for the user.
32 * - 'request_uri': String identifying the location of the request.
33 * - 'referer': String identifying the referring url.
34 * - 'ip': String The ip address of the client machine triggering the log
36 * - 'timestamp': Int unix timestamp.
38 private function generateLogEntries($count, $options = []) {
41 $user = !empty($this->adminUser) ? $this->adminUser : new AnonymousUserSession();
43 // Prepare the fields to be logged.
45 'channel' => 'custom',
46 'message' => 'Dblog test log message',
48 'severity' => RfcLogLevel::NOTICE,
52 'request_uri' => $base_root . \Drupal::request()->getRequestUri(),
53 'referer' => \Drupal::request()->server->get('HTTP_REFERER'),
55 'timestamp' => REQUEST_TIME,
58 $logger = $this->container->get('logger.dblog');
59 $message = $log['message'] . ' Entry #';
60 for ($i = 0; $i < $count; $i++) {
61 $log['message'] = $message . $i;
62 $logger->log($log['severity'], $log['message'], $log);