public function DatabaseLogger::save in Ultimate Cron 8.2
Saves a log entry.
Parameters
\Drupal\ultimate_cron\Logger\LogEntry $log_entry: The log entry to save.
Overrides LoggerInterface::save
File
- src/
Plugin/ ultimate_cron/ Logger/ DatabaseLogger.php, line 312
Class
- DatabaseLogger
- Database logger.
Namespace
Drupal\ultimate_cron\Plugin\ultimate_cron\LoggerCode
public function save(LogEntry $log_entry) {
if (!$log_entry->lid) {
return;
}
try {
$this->connection
->insert('ultimate_cron_log')
->fields([
'lid' => $log_entry->lid,
'name' => $log_entry->name,
'log_type' => $log_entry->log_type,
'start_time' => $log_entry->start_time,
'end_time' => $log_entry->end_time,
'uid' => $log_entry->uid,
'init_message' => Unicode::truncate((string) $log_entry->init_message, static::MAX_TEXT_LENGTH, FALSE, TRUE),
'message' => Unicode::truncate((string) $log_entry->message, static::MAX_TEXT_LENGTH, FALSE, TRUE),
'severity' => $log_entry->severity,
])
->execute();
} catch (IntegrityConstraintViolationException $e) {
// Row already exists. Let's update it, if we can.
$updated = $this->connection
->update('ultimate_cron_log')
->fields([
'name' => $log_entry->name,
'log_type' => $log_entry->log_type,
'start_time' => $log_entry->start_time,
'end_time' => $log_entry->end_time,
'init_message' => Unicode::truncate((string) $log_entry->init_message, static::MAX_TEXT_LENGTH, FALSE, TRUE),
'message' => Unicode::truncate((string) $log_entry->message, static::MAX_TEXT_LENGTH, FALSE, TRUE),
'severity' => $log_entry->severity,
])
->condition('lid', $log_entry->lid)
->condition('end_time', 0)
->execute();
if (!$updated) {
// Row was not updated, someone must have beaten us to it.
// Let's create a new log entry.
$lid = $log_entry->lid . '-' . uniqid('', TRUE);
$log_entry->message = (string) t('Lock #@original_lid was already closed and logged. Creating a new log entry #@lid', [
'@original_lid' => $log_entry->lid,
'@lid' => $lid,
]) . "\n" . $log_entry->message;
$log_entry->severity = $log_entry->severity >= 0 && $log_entry->severity < RfcLogLevel::ERROR ? $log_entry->severity : RfcLogLevel::ERROR;
$log_entry->lid = $lid;
$this
->save($log_entry);
}
} catch (\Exception $e) {
// In case the insert statement above results in a database exception.
// To ensure that the causal error is written to the log,
// we try once to open a dedicated connection and write again.
if (($e instanceof DatabaseException || $e instanceof \PDOException) && $this->connection
->getTarget() != 'ultimate_cron' && !\Drupal::config('ultimate_cron')
->get('bypass_transactional_safe_connection')) {
$key = $this->connection
->getKey();
$info = Database::getConnectionInfo($key);
Database::addConnectionInfo($key, 'ultimate_cron', $info['default']);
$this->connection = Database::getConnection('ultimate_cron', $key);
// Now try once to log the error again.
$this
->save($log_entry);
}
else {
throw $e;
}
}
}