Logging and Error Reporting

Logging is an important part of developing and managing technical systems. Logging and errors provide feedback to developers so they can effectively make changes to the system.

PHP Logging

PHP has native error and notice logging.

Logging level

You can set the log level depending on your environment. Production servers should hide notices and warnings, while development systems should show all messages.

PHP defines constants to refer to logging levels. For example E_ERROR=1 and E_USER_NOTICE=1024.

Constants can be used by name in php.ini, but must use the numeric value elsewhere (IE httpd.conf).

Messages

For convenience, you can use \Drupal::messenger() in hook functions.

\Drupal::messenger()->addMessage('hello');
\Drupal::messenger()->addError('bad!');
\Drupal::messenger()->addStatus('meh');
\Drupal::messenger()->addWarning('huh!?');