Community Documentation

How to use watchdog() in your own code

Last updated May 6, 2009. Created by here on September 26, 2006.
Edited by Jupiter, Heine. Log in to edit this page.

This function is invaluable for debugging your own code.

In its simplest form, one might use the following code to insert new lines into the watchdog log.

<?php
watchdog
('error title', 'error message');
?>

The watchdog module in modules/watchdog.module has everything but the function itself. You can find the watchdog() function in includes/bootstrap.inc where the parameters are explained:

<?php
// snippet taken from 6.11 -- includes/bootstrap.inc
/**
* Log a system message.
*
* @param $type
*   The category to which this message belongs.
* @param $message
*   The message to store in the log. See t() for documentation
*   on how $message and $variables interact. Keep $message
*   translatable by not concatenating dynamic values into it!
* @param $variables
*   Array of variables to replace in the message on display or
*   NULL if message is already translated or not possible to
*   translate.
* @param $severity
*   The severity of the message, as per RFC 3164
* @param $link
*   A link to associate with the message.
*
* @see watchdog_severity_levels()
*/
function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) {
  global
$user, $base_root;

 
// Prepare the fields to be logged
 
$log_message = array(
   
'type'        => $type,
   
'message'     => $message,
   
'variables'   => $variables,
   
'severity'    => $severity,
   
'link'        => $link,
   
'user'        => $user,
   
'request_uri' => $base_root . request_uri(),
   
'referer'     => referer_uri(),
   
'ip'          => ip_address(),
   
'timestamp'   => time(),
    );

 
// Call the logging hooks to log/process the message
 
foreach (module_implements('watchdog', TRUE) as $module) {
   
module_invoke($module, 'watchdog', $log_message);
  }
}
?>

Comments

Severity Levels

I couldn't find a definition for the watchdog severity levels in the 6.x API, though I did finally find the definitions in the 7.x API which also references http://www.ietf.org/rfc/rfc3164.txt as the source.

WATCHDOG_EMERG -- Emergency: system is unusable.
WATCHDOG_ALERT -- Alert: action must be taken immediately.
WATCHDOG_CRITICAL -- Critical: critical conditions.
WATCHDOG_ERROR -- Error: error conditions.
WATCHDOG_WARNING -- Warning: warning conditions.
WATCHDOG_NOTICE -- Notice: normal but significant condition.
WATCHDOG_INFO -- Informational: informational messages.
WATCHDOG_DEBUG -- Debug: debug-level messages.

The secure way to include

The secure way to include variables in watchdog (example taken from actions module):

<?php
watchdog
('actions', 'Action %action created.', array('%action' => $desc));
?>

—Matt

About this page

Drupal version
Drupal 6.x

Develop for Drupal

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here