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

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...