Several corrections

Created on 10 July 2023, about 1 year ago

It is great that the option exclude has added. Thank's.
1) After install module block ALL records to watchdog. This is because the system does not have settings (table=config, dblog_filter.settings) And the module code says to block $method = $config->get('method') ?: 'include';
That's why error with explode("\n", null) $values = array_map('trim', explode("\n", $entities_load)); https://www.drupal.org/project/dblog_filter/issues/3358495 🐛 Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated RTBC
2) Incorrect descriptions in the form /admin/reports/dblog-filter Select Severity Levels ()

Select Severity Levels +\AND Method (exclude/"Save all but selected") >>> The selected severity logs will be excluded

3) No need to work with null\empty data\values on every log record if user not set any data in settings-form.

PS.

  1. It would be nice to add an option-checkbox to enable/disable the module in the form /admin/reports/dblog-filter.
  2. It would be correct to create a configuration in the database when installing the module (1) with default method EXCLUDE, log_values='', severity_levels=[]).
  public function log($level, $message, array $context = []): void {
    $level_explode = [];
    $result = FALSE;
    // Get RFC LOg levels.
    $levels = RfcLogLevel::getLevels();
    // Get Log Filter Settings.
    $config = \Drupal::config("dblog_filter.settings");
    // Get Severity levels Configuration.
/*WT*/ /*Get&set default value. And solves the issue/error explode("\n", nul) dblog_filter/issues/3358495*/
    //$severity_levels = $config->get('severity_levels');
    if( ($severity_levels = $config->get('severity_levels')) === NULL ) {
    	\Drupal::service('config.factory')->getEditable('dblog_filter.settings')->set('severity_levels', [])->save();
    }
/*<<<*/
/*-*///$entities_load = $config->get('log_values');
/*WT 1)*/ /*After install nodule (by default) NOT block ALL records to watchdog*/
    //$method = $config->get('method') ?: 'include'; /*this settings block all records but not selected\nothing*/
    if( !$method = $config->get('method') ) { $method = 'exclude'; /*Get&Set (on first log) config exclude nothing for prevent block all*/
    	\Drupal::service('config.factory')->getEditable('dblog_filter.settings')->set('method', $method)->save();
    }
/*<<<*/
/*WT 3)*/ /*No need to work with null\empty data.*/ /*Getting the value $entities_load*/ /*And solves the issue/error explode("\n", nul) dblog_filter/issues/3358495*/
    if( ($entities_load = $config->get('log_values')) || !empty($severity_levels) ) { /*Check if config empty & Get $entities_load*/
/*<<<*/
	    $values = array_map('trim', explode("\n", $entities_load));
	    foreach ($levels as $key => $log_level) {
	      $level_array[$key] = strtolower($log_level->getUntranslatedString());
	    }
	    // Check for channel name and given values in log filter settings.
	    foreach ($values as $value) {
	      $explode_values = explode('|', $value);
	      if ($explode_values[0] == $context['channel']) {
	        $level_explode = explode(',', $explode_values[1]);
	      }
	    }
	    if ($level_explode) {
	      $result = in_array($level_array[$level], $level_explode);
	    }
	    if (!empty($severity_levels[$level_array[$level]])) {
	      $result = TRUE;
	    }
	    // If present the only log message.
	    if ($method == 'exclude') {
	      $result = !$result;
	    }
/*+/} else { $result = ($method == 'exclude') ? TRUE : FALSE /*( $method == 'include' ? FALSE : '' )*/; }
    if ($result) {
      parent::log($level, $message, $context);
    }
  }
Feature request
Status

Active

Version

3.2

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024