Skip to content
Snippets Groups Projects
Commit 74a33135 authored by Omar Mohamad - El Hassan Lopesino's avatar Omar Mohamad - El Hassan Lopesino Committed by Ricardo Sanz Ante
Browse files

Issue #3396890: Allow logging push params and push success

parent fb96e445
No related branches found
No related tags found
1 merge request!63Draft: Resolve #3396890 "Allow logging push"
......@@ -7,6 +7,7 @@ use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Utility\Error;
use Drupal\salesforce\Event\SalesforceEvents;
use Drupal\salesforce\Event\SalesforceExceptionEventInterface;
use Drupal\salesforce_mapping\Event\SalesforcePushParamsEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......@@ -51,6 +52,8 @@ class SalesforceLoggerSubscriber implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
$events = [
SalesforceEvents::PUSH_PARAMS => 'salesforcePushParams',
SalesforceEvents::PUSH_SUCCESS => 'salesforcePushSuccess',
SalesforceEvents::ERROR => 'salesforceException',
SalesforceEvents::WARNING => 'salesforceException',
SalesforceEvents::NOTICE => 'salesforceException',
......@@ -58,6 +61,66 @@ class SalesforceLoggerSubscriber implements EventSubscriberInterface {
return $events;
}
/**
* Log params pushed to salesforce.
*
* This functionality can be setup by configuration so that:
* - The sensitive fields are not saved into database.
* - When the data sent is too large, it can be truncated.
*
* @param \Drupal\salesforce_mapping\Event\SalesforcePushParamsEvent $event
* Salesforce push params event.
*/
public function salesforcePushParams(SalesforcePushParamsEvent $event) {
$log_push_params = (bool) $this->config->get('log_push_params');
if ($log_push_params) {
$fields_to_sanitize = $this->config->get('log_push_params_sanitized_fields') ?? [];
$params = $event->getParams()->getParams();
foreach ($params as $key => $value) {
if (in_array($key, $fields_to_sanitize)) {
$params[$key] = '****';
}
}
$max_length = $this->config->get('log_push_params_maxlength');
$params_json = json_encode($params);
if (!empty($max_length) && strlen($params_json) > $max_length) {
$params_json = substr($params_json, 0, $max_length) . '...';
}
$this->logger->debug(
sprintf('Entity of type "%s" is being pushed to salesforce "%s" entity. Drupal entity ID: %s. Data: %s',
$event->getEntity()->getEntityTypeId(),
$event->getMapping()->getSalesforceObjectType(),
$event->getEntity()->id(),
$params_json
)
);
}
}
/**
* Logs when a entity is mapped successfully to salesforce.
*
* @param \Drupal\salesforce_mapping\Event\SalesforcePushParamsEvent $event
* Push params event with salesforce ID.
*/
public function salesforcePushSuccess(SalesforcePushParamsEvent $event) {
$log_push_params = (bool) $this->config->get('log_push_success');
if ($log_push_params) {
$this->logger->info(
sprintf(
'Entity of type "%s" has been succesfully sent to salesforce as a "%s" entity. Drupal entity ID: %s. Salesforce entity ID: %s.',
$event->getEntity()->getEntityTypeId(),
$event->getMapping()->getSalesforceObjectType(),
$event->getEntity()->id(),
$event->getMappedObject()->sfid()
)
);
}
}
/**
* SalesforceException event callback.
*
......
......@@ -44,6 +44,30 @@ class SettingsForm extends ConfigFormBase {
'#default_value' => $config->get('log_level'),
];
$form['log_push_params'] = [
'#title' => $this->t('Log push params sent to salesforce'),
'#type' => 'checkbox',
'#default_value' => $config->get('log_push_params') ?? FALSE,
];
$form['log_push_params_maxlength'] = [
'#title' => $this->t('Log push params max length'),
'#type' => 'number',
'#default_value' => $config->get('log_push_params_maxlength') ?? NULL,
];
$form['log_push_params_sanitized_fields'] = [
'#title' => $this->t('Fields that must not be shown in logs'),
'#type' => 'textarea',
'#default_value' => implode("\r\n", $config->get('log_push_params_sanitized_fields') ?? []),
];
$form['log_push_success'] = [
'#title' => $this->t('Log push success when an entity is sent to salesforce'),
'#type' => 'checkbox',
'#default_value' => $config->get('log_push_success') ?? FALSE,
];
$form = parent::buildForm($form, $form_state);
return $form;
}
......@@ -54,6 +78,10 @@ class SettingsForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('salesforce_logger.settings');
$config->set('log_level', $form_state->getValue('log_level'));
$config->set('log_push_params', $form_state->getValue('log_push_params'));
$config->set('log_push_params_maxlength', (int) $form_state->getValue('log_push_params_maxlength'));
$config->set('log_push_params_sanitized_fields', explode("\r\n", $form_state->getValue('log_push_params_sanitized_fields') ?? ''));
$config->set('log_push_success', $form_state->getValue('log_push_success'));
$config->save();
parent::submitForm($form, $form_state);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment