WatchdogPhp.php in Site Audit 8.3
File
src/Plugin/SiteAuditCheck/WatchdogPhp.php
View source
<?php
namespace Drupal\site_audit\Plugin\SiteAuditCheck;
use Drupal\site_audit\Plugin\SiteAuditCheckBase;
use Drupal\Core\Logger\RfcLogLevel;
class WatchdogPhp extends SiteAuditCheckBase {
public function getResultFail() {
}
public function getResultInfo() {
$counts = [];
foreach ($this->registry->php_counts as $severity => $count) {
$counts[] = $severity . ': ' . $count;
}
$ret_val = implode(', ', $counts);
$ret_val .= ' - total ' . $this->registry->percent_php . '%';
return $ret_val;
}
public function getResultPass() {
return $this
->t('No PHP warnings, notices or errors.');
}
public function getResultWarn() {
return $this
->getResultInfo();
}
public function getAction() {
if ($this->score == SiteAuditCheckBase::AUDIT_CHECK_SCORE_WARN) {
return $this
->t('Every time Drupal logs a PHP notice, warning or error, PHP executes slower and the writing operation locks the database. By eliminating the problems, your site will be faster.');
}
}
public function calculateScore() {
$this->registry->php_counts = [];
$this->registry->php_count_total = 0;
$this->registry->percent_php = 0;
$query = \Drupal::database()
->select('watchdog');
$query
->addExpression('COUNT(*)', 'count');
$query
->addField('watchdog', 'severity');
$query
->groupBy('severity');
$query
->orderBy('severity', 'ASC');
$result = $query
->execute();
$severity_levels = $this
->watchdog_severity_levels();
while ($row = $result
->fetchObject()) {
$row->severity = $severity_levels[$row->severity];
if (!isset($this->registry->php_counts[$row->severity])) {
$this->registry->php_counts[$row->severity] = 0;
}
$this->registry->php_counts[$row->severity]++;
$this->registry->php_count_total++;
}
$this->registry->percent_php = round($this->registry->php_count_total / $this->registry->count_entries * 100, 2);
if ($this->registry->percent_php >= 10) {
return SiteAuditCheckBase::AUDIT_CHECK_SCORE_WARN;
}
return SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO;
}
public function watchdog_severity_levels() {
return [
RfcLogLevel::EMERGENCY => 'emergency',
RfcLogLevel::ALERT => 'alert',
RfcLogLevel::CRITICAL => 'critical',
RfcLogLevel::ERROR => 'error',
RfcLogLevel::WARNING => 'warning',
RfcLogLevel::NOTICE => 'notice',
RfcLogLevel::INFO => 'info',
RfcLogLevel::DEBUG => 'debug',
];
}
}