drupal 8

Similar to drupal 7, Drupal 8 also provide the logging functionality but little different to use it because the drupal 8 is symphony base architecture, so can use the logger service of drupal 8 to reload your logs on dblog

For Notice type:

<?php

\Drupal::logger('module_name')->notice($message_string);

?>

For Error Type:

<?php

\Drupal::logger('module_name')->error($message_string);

?>

But, If you want to print a array or object using logger, you can use following code also -

\Drupal::logger('module_name')->notice('<pre><code>' . print_r($array, TRUE) . '</code></pre>' );

Note- please use this logger in class function to avoid any further error - see example below:

<?php
/**
 * @file
 * Contains \Drupal\module_name\Controller\FirstController.
 */
 
namespace Drupal\module_name\Controller;
 
use Drupal\Core\Controller\ControllerBase;
 
class FirstController extends ControllerBase {
  public function content() {
     $array_to_print = array(
      'abc' => 'markup',
      'cdg' => 134654,
    );

    \Drupal::logger('module_name')->notice('<pre><code>' . print_r($array_to_print, TRUE) . '</code></pre>'    );

    return array(
      '#type' => 'markup',
      '#markup' => t('This is test example'),
    );
  }
}


Please Comment your thoughts and feedback below and add something if you found good in anywhere to help others

Hit a like Button If you like the Post.

Many Thanks