Thursday, March 24, 2016

How to Log Messages in Drupal 8

How to Log Messages in Drupal 8

<?php
// Logs a notice
\Drupal::logger('my_module')->notice('@type: deleted %title.', [
  '@type' => $this->entity->bundle(),
  '%title' => $this->entity->label(),
]);

// Logs an error
\Drupal::logger('my_module')->error('@type: deleted %title.', [
  '@type' => $this->entity->bundle(),
  '%title' => $this->entity->label(),
]);
?>

<?php
try {
  // some code
}
catch (\Exception $e) {
  watchdog_exception('test', $e);
}
?>

Then, go to http://mydomain.com/admin/reports/dblog

Placeholder types

@variable — Use this style of placeholder for most use-cases. Special characters in the text will be converted to HTML entities.

%variable — Use this style of placeholder to pass text through drupal_placeholder() which will result in HTML escaped text, then wrapped with <em> tags.

:variable — Use this style of placeholder when substituting the value of an href attribute. Values will be HTML escaped, and filtered for dangerous protocols.

Reference:

https://drupalize.me/blog/201510/how-log-messages-drupal-8

No comments: