Skip to main content

Writing Messages into your Watchdog log in Drupal 10

DrupalVIP Support

Drupal 8 and on, has service called logger, no need to use the watchdog function

The logger method is as follow:

\Drupal::logger(__FUNCTION__)->debug("Any Message" )

 

All logger inputs goes into table: watchdog

You can view the logger at <domain>/admin/reports/dblog

You can update the dblog view at: <domain>/admin/structure/views/view/watchdog

The logger method has the following severity options:

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();

Whilst $logger->log($level, $message); 
works fine, you can directly use the functions enumerated in the Drupal\Core\Logger\RfcLoggerTrait:

  • emergency($message, $context)
  • alert($message, $context)
  • critical($message, $context)
  • error($message, $context)
  • warning($message, $context)
  • notice($message, $context)
  • info($message, $context)
  • debug($message, $context)