Backend and Infrastructure

Logging and Error Handling in Drush Commands for Drupal 8, 9, and 10

Adding logging and error handling are an important part of authoring Drush commands. Logs allow developers to get timely feedback from a command and inform users about potential alterations and flags, events to monitor, and the progress of long-running commands. Correct error handling allows for clean exits, meaningful error descriptions, and provides a path forward for developers to fix the errors and accomplish their goals.

In this tutorial we'll:

  • Explore the different types of logging messages Drush commands can output
  • Learn how to handle errors from within a Drush command

By the end of this tutorial you'll know what types of log messages you can use; how to log success messages, errors, or debug statements from a custom Drush command; and how to handle errors and exceptions.