Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DblogHooks.php

function DblogHooks::cron

Implements hook_cron().

Controls the size of the log table, paring it to 'dblog_row_limit' messages.

File

core/modules/dblog/src/Hook/DblogHooks.php, line 63

Class

DblogHooks
Hook implementations for dblog.

Namespace

Drupal\dblog\Hook

Code

public function cron() : void {
    // Cleanup the watchdog table.
    $row_limit = \Drupal::config('dblog.settings')->get('row_limit');
    // For row limit n, get the wid of the nth row in descending wid order.
    // Counting the most recent n rows avoids issues with wid number sequences,
    // e.g. auto_increment value > 1 or rows deleted directly from the table.
    if ($row_limit > 0) {
        $connection = \Drupal::database();
        $min_row = $connection->select('watchdog', 'w')
            ->fields('w', [
            'wid',
        ])
            ->orderBy('wid', 'DESC')
            ->range($row_limit - 1, 1)
            ->execute()
            ->fetchField();
        // Delete all table entries older than the nth row, if nth row was found.
        if ($min_row) {
            $connection->delete('watchdog')
                ->condition('wid', $min_row, '<')
                ->execute();
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal