PhpNoticesSensorPlugin.php in Monitoring 8
File
src/Plugin/monitoring/SensorPlugin/PhpNoticesSensorPlugin.php
View source
<?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\monitoring\Result\SensorResultInterface;
class PhpNoticesSensorPlugin extends WatchdogAggregatorSensorPlugin {
protected $configurableConditions = FALSE;
protected $configurableVerboseOutput = FALSE;
public function runSensor(SensorResultInterface $result) {
parent::runSensor($result);
if (!empty($this->fetchedObject->variables)) {
$variables = unserialize($this->fetchedObject->variables);
$variables['%file'] = $this
->shortenFilename($variables['%file']);
$result
->setMessage('@count times: @error', [
'@count' => (int) $this->fetchedObject->records_count,
'@error' => new FormattableMarkup('%type: @message in %function (Line %line of %file).', $variables),
]);
}
}
public function getAggregateQuery() {
$query = parent::getAggregateQuery();
$query
->addField('watchdog', 'variables');
$query
->condition('type', 'php', NULL);
$query
->groupBy('variables');
$query
->orderBy('records_count', 'DESC');
$query
->range(0, 1);
return $query;
}
public function getQuery() {
$query = parent::getQuery();
$query
->addField('watchdog', 'variables');
$this
->addAggregateExpression($query);
$query
->condition('type', 'php', NULL);
$query
->groupBy('variables');
$order =& $query
->getOrderBy();
$order = [];
$query
->orderBy('records_count', 'DESC');
$query
->range(0, 20);
return $query;
}
public function verboseResultUnaggregated(array &$output) {
parent::verboseResultUnaggregated($output);
$rows = [];
foreach ($output['verbose_sensor_result']['#rows'] as $delta => $row) {
$variables = unserialize($row['variables']);
$variables['%file'] = $this
->shortenFilename($variables['%file']);
$rows[$delta]['count'] = $row['records_count'];
$rows[$delta]['type'] = $variables['%type'];
$rows[$delta]['message'] = $variables['@message'];
$rows[$delta]['caller'] = $variables['%function'];
$rows[$delta]['file'] = $variables['%file'] . ':' . $variables['%line'];
}
$output['verbose_sensor_result']['#rows'] = $rows;
$output['verbose_sensor_result']['#header'] = $this
->buildTableHeader($rows);
}
protected function shortenFilename($filename) {
return str_replace(DRUPAL_ROOT . '/', '', $filename);
}
}