Skip to main content

Creating a message log using Drupal watchdog.

 

Simple log

// Logs a notice
\Drupal::logger('my_module')->notice($message);
// Logs an error
\Drupal::logger('my_module')->error($message);

 

Adding a layer of complexity to the log

public function submitForm(array &$form, FormStateInterface $form_state) {
    $entity = $this->getEntity();
    $entity->delete();

    \Drupal::logger('content_entity_example')->notice('@type: deleted %title.',
        array(
            '@type' => $this->entity->bundle(),
            '%title' => $this->entity->label(),
        ));
    $form_state->setRedirect('entity.content_entity_example_contact.collection');
}

 

Variable type - which do I use

@variable – Use this placeholder as the default choice for anything displayed on the site, but not within HTML attributes, JavaScript, or CSS. Doing so is a security risk.

%variable – As with @variable, do not use this within HTML attributes, JavaScript, or CSS. Doing so is a security risk.

:variable – When ":variable" comes from arbitrary user input, the result is secure, but not guaranteed to be a valid URL. Remember to guarantee a valid URL, use Url::fromUri($user_input)->toString() before passing the result into a ":variable" placeholder.

 

@variable

When the placeholder replacement value is:

        A string, the replaced value in the returned string will be sanitized using \Drupal\Component\Utility\Html::escape().
        A MarkupInterface object, the replaced value in the returned string will not be sanitized.
        A MarkupInterface object cast to a string, the replaced value in the returned string be forcibly sanitized using \Drupal\Component\Utility\Html::escape().

$this->placeholderFormat('This will force HTML-escaping of the replacement value: @text', ['@text' => (string) $safe_string_interface_object));

      

%variable

Use when the replacement value is to be wrapped in <em> tags. A call like:

$string = "%output_text";
$arguments = [
  '%output_text' => 'text output here.',
];
$this
  ->placeholderFormat($string, $arguments);

makes the following HTML code:

<em class="placeholder">text output here.</em>

 

:variable

Return value is escaped with \Drupal\Component\Utility\Html::escape() and filtered for dangerous protocols using UrlHelper::stripDangerousProtocols(). Use this when using the "href" attribute, ensuring the attribute value is always wrapped in quotes:

// Secure (with quotes):
$this
  ->placeholderFormat('<a href=":url">@variable</a>', [
  ':url' => $url,
  '@variable' => $variable,
]);

// Insecure (without quotes):
$this
  ->placeholderFormat('<a href=:url>@variable</a>', [
  ':url' => $url,
  '@variable' => $variable,
]);

 

Types of logs

So far in the examples noted above, only two types have been used: info and error.

use Drupal\Core\Logger\RfcLogLevel;
$severity = RfcLogLevel::EMERGENCY;
$severity = RfcLogLevel::ALERT;
$severity = RfcLogLevel::CRITICAL;
$severity = RfcLogLevel::ERROR;
$severity = RfcLogLevel::WARNING;
$severity = RfcLogLevel::NOTICE;
$severity = RfcLogLevel::INFO;
$severity = RfcLogLevel::DEBUG;
$levels = RfcLogLevel::getLevels();
 

Related articles