Score:2

Is there a way to retain core search logging instead of having it get deleted with the watchdog logs?

ng flag

We're using the core search module on a D9 site and use the "Top Search Phrases" report for search tracking but the results get stored in the watchdog table so they get cleared out when the logs do.

I see there are some D7 modules to improve the core search logs but nothing that I could find for D8.

The client needs to retain the search word logging so how could we do this on our D9 site?

id flag
Does a contributed module provide the "Top Search Phrases" report? If so, which one?
unusedspoon avatar
aq flag
A possible option would be to use your analytics to track this. Then you'd be able to get breakdown by day, generate reports and things like that
quantumized avatar
ng flag
Analytics is an option but this is not my area and haven't been able to figure out how to track Drupal core searches in GA. If you have any info on how to do this that would be awesome.
id flag
You can ship logs outside of Drupal with the syslog module but of course that will not help with a report that according to you depends on parsing the database log rows.
quantumized avatar
ng flag
@cilefen - "Top Search Phrases" comes from the core Drupal search module.
id flag
It does not come from the core search module. It is provided by the core database logging module.
quantumized avatar
ng flag
@cilefen - ahhh, okay, that makes sense. Thank you for the clarification.
Score:1
cn flag

I don't know if there exists a module (off-topic anyway on this site), but you could try to implement your own cron hook. First disable the built-in clean up process by setting "Database log messages to keep" to "All" in the UI.

Then implement your own hook_cron(), similar to the core hook

/core/modules/dblog/dblog.module

/**
 * Implements hook_cron().
 *
 * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
 */
function dblog_cron() {
  // 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();
    }
  }
}

only that your hook keeps the search entries or moves them to a different table.

quantumized avatar
ng flag
Thank you, this is an interesting solution @4uk4. We're currently seeing if using Google Analytics will provide accurate search logging but, if not, we'll try your suggestion.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.