Zensations

Development

Drupal logging – logging arrays or objects using watchdog()

Andreas Tasch

The logging function included in the Drupal core can output simple data types such as strings or integers. However, sometimes it is necessary to write arrays or objects to the log to gain insights into runtime behaviour, especially in cases where a complete development environment, including XDebug and/or the Devel module, is not available. A possible use case would be monitoring and investigating a payment module (e.g. PayPal transactions) in live operation of a Drupal Commerce installation.

Enable database logging

  • Enable database logging under Administration >> Configuration >> Logging and errors (admin/config/development/logging)
  • Set the Error messages to display option to All messages and save
  • Log entries can then be found under Administration >> Reports >> Recent log messages (admin/reports/dblog)

Using watchdog(), strings and simple data types can be written to the database log out of the box.

Logging strings and simple data types:

watchdog("My Module", "Variable content: %stringdebug", array('%stringdebug' => $string));

With the help of print_r(), arrays or objects can also be logged. The following construct is useful:

watchdog("My Module", "Array content: %arraydebug", array( '%arraydebug' =>'<pre>' . print_r( $array, true) . '</pre>'));

Should the log entry be difficult to read due to the size of the array/object, the array/object can be copied and pasted from the log entry, for example, into an Array Beautifier and output in a readable format.

Share

More on this topic