You are here

class WebformSubmissionLogLogger in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_submission_log/src/WebformSubmissionLogLogger.php \Drupal\webform_submission_log\WebformSubmissionLogLogger

Logger that listens for 'webform_submission' channel.

Hierarchy

Expanded class hierarchy of WebformSubmissionLogLogger

1 string reference to 'WebformSubmissionLogLogger'
webform_submission_log.services.yml in modules/webform_submission_log/webform_submission_log.services.yml
modules/webform_submission_log/webform_submission_log.services.yml
1 service uses WebformSubmissionLogLogger
logger.webform_submission_log in modules/webform_submission_log/webform_submission_log.services.yml
Drupal\webform_submission_log\WebformSubmissionLogLogger

File

modules/webform_submission_log/src/WebformSubmissionLogLogger.php, line 12

Namespace

Drupal\webform_submission_log
View source
class WebformSubmissionLogLogger implements LoggerInterface {
  use RfcLoggerTrait;

  /**
   * The webform submission log manager.
   *
   * @var \Drupal\webform_submission_log\WebformSubmissionLogManagerInterface
   */
  protected $logManager;

  /**
   * The message's placeholders parser.
   *
   * @var \Drupal\Core\Logger\LogMessageParserInterface
   */
  protected $parser;

  /**
   * WebformSubmissionLog constructor.
   *
   * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
   *   The log message parser service.
   * @param \Drupal\webform_submission_log\WebformSubmissionLogManagerInterface $log_manager
   *   The webform submission log manager.
   */
  public function __construct(LogMessageParserInterface $parser, WebformSubmissionLogManagerInterface $log_manager) {
    $this->parser = $parser;
    $this->logManager = $log_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {

    // Only log the 'webform_submission' channel.
    if ($context['channel'] !== 'webform_submission') {
      return;
    }

    // Make sure the context contains a webform submission.
    if (!isset($context['webform_submission'])) {
      return;
    }

    /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
    $webform_submission = $context['webform_submission'];

    // Make sure webform submission log is enabled.
    if (!$webform_submission
      ->getWebform()
      ->hasSubmissionLog()) {
      return;
    }

    // Set default values.
    $context += [
      'handler_id' => '',
      'operation' => '',
      'data' => [],
    ];

    // Cast message to string.
    $message = (string) $message;
    $message_placeholders = $this->parser
      ->parseMessagePlaceholders($message, $context);
    $this->logManager
      ->insert([
      'webform_id' => $webform_submission
        ->getWebform()
        ->id(),
      'sid' => $webform_submission
        ->id(),
      'handler_id' => $context['handler_id'],
      'operation' => $context['operation'],
      'uid' => $context['uid'],
      'message' => $message,
      'variables' => serialize($message_placeholders),
      'data' => serialize($context['data']),
      'timestamp' => $context['timestamp'],
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RfcLoggerTrait::alert public function
RfcLoggerTrait::critical public function
RfcLoggerTrait::debug public function
RfcLoggerTrait::emergency public function
RfcLoggerTrait::error public function
RfcLoggerTrait::info public function
RfcLoggerTrait::notice public function
RfcLoggerTrait::warning public function
WebformSubmissionLogLogger::$logManager protected property The webform submission log manager.
WebformSubmissionLogLogger::$parser protected property The message's placeholders parser.
WebformSubmissionLogLogger::log public function Logs with an arbitrary level. Overrides RfcLoggerTrait::log
WebformSubmissionLogLogger::__construct public function WebformSubmissionLog constructor.