Community Documentation

watchdog

5 bootstrap.inc watchdog($type, $message, $severity = WATCHDOG_NOTICE, $link = NULL)
6 bootstrap.inc watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
7 bootstrap.inc watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
8 bootstrap.inc watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)

Log a system message.

Parameters

$type: The category to which this message belongs. Can be any string, but the general practice is to use the name of the module calling watchdog().

$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!

$variables: Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.

$severity: The severity of the message, as per RFC 3164. Possible values are WATCHDOG_ERROR, WATCHDOG_WARNING, etc.

$link: A link to associate with the message.

See also

watchdog_severity_levels()

▾ 91 functions call watchdog()

actions_do in includes/actions.inc
Perform a given list of actions by executing their callback functions.
actions_save in includes/actions.inc
Save an action and its associated user-supplied parameter values to the database.
actions_synchronize in includes/actions.inc
Synchronize actions that are provided by modules.
aggregator_form_category_submit in modules/aggregator/aggregator.admin.inc
Process aggregator_form_category form submissions.
aggregator_form_feed_submit in modules/aggregator/aggregator.admin.inc
Process aggregator_form_feed form submissions.
aggregator_parse_feed in modules/aggregator/aggregator.module
Parse a feed and store its items.
aggregator_refresh in modules/aggregator/aggregator.module
Checks a news feed for new items.
blogapi_blogger_edit_post in modules/blogapi/blogapi.module
Blogging API callback. Modifies the specified blog node.
blogapi_blogger_new_post in modules/blogapi/blogapi.module
Blogging API callback. Inserts a new blog post as a node.
book_admin_edit_submit in modules/book/book.admin.inc
Handle submission of the book administrative page form.
comment_admin_overview_submit in modules/comment/comment.admin.inc
Process comment_admin_overview form submissions.
comment_publish_action in modules/comment/comment.module
Action to publish a comment.
comment_save in modules/comment/comment.module
Accepts a submission of new or changed comment content.
comment_unpublish_action in modules/comment/comment.module
Action to unpublish a comment.
comment_unpublish_by_keyword_action in modules/comment/comment.module
Action to unpublish a comment if it contains a certain string.
contact_admin_delete_submit in modules/contact/contact.admin.inc
Process category delete form submission.
contact_admin_edit_submit in modules/contact/contact.admin.inc
Process the contact category edit page form submission.
contact_mail_page_submit in modules/contact/contact.pages.inc
Process the site-wide contact page form submission.
contact_mail_user_submit in modules/contact/contact.pages.inc
Process the personal contact page form submission.
drupal_access_denied in includes/common.inc
Generates a 403 error if the request is not allowed.
drupal_convert_to_utf8 in includes/unicode.inc
Convert data to UTF-8
drupal_cron_cleanup in includes/common.inc
Shutdown function for cron cleanup.
drupal_cron_run in includes/common.inc
Executes a cron run when called
drupal_error_handler in includes/common.inc
Log errors as defined by administrator.
drupal_mail in includes/mail.inc
Compose and optionally send an e-mail message.
drupal_not_found in includes/common.inc
Generates a 404 error if the request can not be handled.
drupal_xml_parser_create in includes/unicode.inc
Prepare a new XML parser.
file_check_directory in includes/file.inc
Checks whether a directory exists and is writable.
file_copy in includes/file.inc
Copies a file to a new location.
file_save_upload in includes/file.inc
Saves a file upload to a new location.
filter_form_validate in modules/filter/filter.module
Validation callback for filter elements in a form.
forum_confirm_delete_submit in modules/forum/forum.admin.inc
Implementation of forms api _submit call. Deletes a forum after confirmation.
hook_ping in developer/hooks/core.php
Ping another server.
image_toolkit_invoke in includes/image.inc
Invokes the given method using the currently selected toolkit.
locale_add_language in includes/locale.inc
API function to add a language.
locale_languages_delete_form_submit in includes/locale.inc
Process language deletion submissions.
locale_translate_edit_form_validate in includes/locale.inc
Validate string editing form submissions.
locale_translate_import_form_submit in includes/locale.inc
Process the locale import form submission.
menu_delete_menu_confirm_submit in modules/menu/menu.admin.inc
Delete a custom menu and all items in it.
menu_item_delete_form_submit in modules/menu/menu.admin.inc
Process menu delete form submissions.
node_assign_owner_action in modules/node/node.module
Implementation of a configurable Drupal action. Assigns ownership of a node to a user.
node_delete in modules/node/node.module
Delete a node.
node_form_submit in modules/node/node.pages.inc
node_make_sticky_action in modules/node/node.module
Implementation of a Drupal action. Sets the sticky-at-top-of-list property of a node to 1.
node_make_unsticky_action in modules/node/node.module
Implementation of a Drupal action. Sets the sticky-at-top-of-list property of a node to 0.
node_promote_action in modules/node/node.module
Implementation of a Drupal action. Sets the promote property of a node to 1.
node_publish_action in modules/node/node.module
Implementation of a Drupal action. Sets the status of a node to 1, meaning published.
node_revision_delete_confirm_submit in modules/node/node.pages.inc
node_revision_revert_confirm_submit in modules/node/node.pages.inc
node_save_action in modules/node/node.module
Implementation of a Drupal action. Saves a node.
node_type_delete_confirm_submit in modules/node/content_types.inc
Process content type delete confirm submissions.
node_type_form_submit in modules/node/content_types.inc
Implementation of hook_form_submit().
node_unpromote_action in modules/node/node.module
Implementation of a Drupal action. Sets the promote property of a node to 0.
node_unpublish_action in modules/node/node.module
Implementation of a Drupal action. Sets the status of a node to 0, meaning unpublished.
node_unpublish_by_keyword_action in modules/node/node.module
Implementation of a configurable Drupal action. Unpublish a node if it contains a certain string.
openid_verify_assertion_nonce in modules/openid/openid.module
Verify that the nonce has not been used in earlier assertions from the same OpenID provider.
path_set_alias in modules/path/path.module
Set an aliased path for a given Drupal path, preventing duplicates.
ping_ping in modules/ping/ping.module
Implementation of hook_ping().
profile_field_delete_submit in modules/profile/profile.admin.inc
Process a field delete form submission.
profile_field_form_submit in modules/profile/profile.admin.inc
Process profile_field_form submissions.
search_view in modules/search/search.pages.inc
Menu callback; presents the search form and/or search results.
system_actions_delete_form_submit in modules/system/system.module
Process system_actions_delete form submissions.
system_cron in modules/system/system.module
Implementation of hook_cron().
system_send_email_action in modules/system/system.module
Implementation of a configurable Drupal action. Sends an email.
taxonomy_form_term_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler to insert or update a term.
taxonomy_form_vocabulary_submit in modules/taxonomy/taxonomy.admin.inc
Accept the form submission for a vocabulary and save the results.
taxonomy_term_confirm_delete_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler to delete a term after confirmation.
taxonomy_vocabulary_confirm_delete_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler to delete a vocabulary after confirmation.
taxonomy_vocabulary_confirm_reset_alphabetical_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler to reset a vocabulary to alphabetical order after confirmation.
throttle_exit in modules/throttle/throttle.module
Implementation of hook_exit().
trigger_unassign_submit in modules/trigger/trigger.admin.inc
user_authenticate in modules/user/user.module
Try to log in the user locally.
user_authenticate_finalize in modules/user/user.module
Finalize the login process. Must be called when logging in a user.
user_block_ip_action in modules/user/user.module
Implementation of a Drupal action. Adds an access rule that blocks the user's IP address.
user_block_user_action in modules/user/user.module
Implementation of a Drupal action. Blocks the current user.
user_delete in modules/user/user.module
Delete a user.
user_edit_validate in modules/user/user.pages.inc
user_external_login_register in modules/user/user.module
Helper function for authentication modules. Either login in or registers the current user, based on username. Either way, the global $user object is populated based on $name.
user_logout in modules/user/user.pages.inc
Menu callback; logs the current user out, and redirects to the home page.
user_pass_reset in modules/user/user.pages.inc
Menu callback; process one time login link and redirects to the user page on success.
user_pass_submit in modules/user/user.pages.inc
user_profile_form_validate in modules/user/user.pages.inc
Validation function for the user account and profile editing form.
user_register_submit in modules/user/user.module
Submit handler for the user registration form.
user_user_operations in modules/user/user.module
Implementation of hook_user_operations().
_comment_delete_thread in modules/comment/comment.admin.inc
Perform the actual deletion of a comment and all its replies.
_form_validate in includes/form.inc
Performs validation on form elements. First ensures required fields are completed, #maxlength is not exceeded, and selected options were in the list of options given to the user. Then calls user-defined validators.
_locale_export_po in includes/locale.inc
Write a generated PO or POT file to the output.
_locale_import_po in includes/locale.inc
Parses Gettext Portable Object file information and inserts into database
_locale_rebuild_js in includes/locale.inc
(Re-)Creates the JavaScript translation file for a language.
_menu_router_build in includes/menu.inc
Helper function to build the router table based on the data from hook_menu.
_update_refresh in modules/update/update.fetch.inc
Fetch project info via XML from a central server.

File

includes/bootstrap.inc, line 950
Functions that need to be loaded on every Drupal request.

Code

<?php
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') as $module) {
    module_invoke($module, 'watchdog', $log_message);
  }
}
?>

Comments

Watchdog Security Levels

For your own module development:

http://api.drupal.org/api/function/watchdog_severity_levels/6

<?php
function watchdog_severity_levels() {
  return array(
   
WATCHDOG_EMERG    => t('emergency'),
   
WATCHDOG_ALERT    => t('alert'),
   
WATCHDOG_CRITICAL => t('critical'),
   
WATCHDOG_ERROR    => t('error'),
   
WATCHDOG_WARNING  => t('warning'),
   
WATCHDOG_NOTICE   => t('notice'),
   
WATCHDOG_INFO     => t('info'),
   
WATCHDOG_DEBUG    => t('debug'),
  );
}
?>

This function is invaluable

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');
?>

An interesting quirk with

An interesting quirk with this function is the variables array and the text in the log message. If you have a $variables['user'] element for example and then attempt to put the word 'user' anywhere in the message, you will encounter one of these 2 errors:

warning: preg_match() expects parameter 2 to be string, array given in /yourInstallDir/includes/bootstrap.inc on line 842.
OR
warning: preg_match() expects parameter 2 to be string, object given in /yourInstallDir/includes/bootstrap.inc on line 842.

This isnt a bug. It is intended and thus you have to fix your msg or variable to use a different case such as $variables['User'] and then you can use 'user' in the message or vice versa.

If this happens your code is

If this happens your code is broken. Those variables are the same as the ones that are passed on to t() and they should start with an exclamation mark (!), an at sign (@) or a percent sign (%). Translatable string replacement variables should never start with another character for safety reasons.

$link should be an HTML link

Note that $link should be an actual HTML link generated via l() or some such function not a link array as used in theme_links().

Thanks for the heads-up :) I

Thanks for the heads-up :) I was just wondering if I should l() it.

no comprendo

It would be really helpful if someone could post an example of how to use watchdog with some text that includes a variable, like "user XXX has just created a node titled XXX" or something..

A more complete example

<?php
  watchdog
('example', 'Example module is unable to find the file @filename.', array('@filename' => $filename), WATCHDOG_ERROR);
?>

Example

In user.module:

watchdog('user', 'New user: %name (%email).', array('%name' => $name, '%email' => $mail), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit'));

Limitation of Watchdog

An unfortunate limitation of watchdog if you want to use it in custom applications that don't use all of drupal (ie, you want to use drupal's database and session layers so you do drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION)) is that it requires "module_implements" which means the "module.inc" file must be loaded, which means that it can't be used before the drupal phase of DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE (I also think this means that it'll fail with aggressive caching).

Drupal 7 fixes this by adding function_exists('module_implements') but drupal 6 doesn't have it

Beware of $type length limitation to 16 chars

"$type The category to which this message belongs. Can be any string, but the general practice is to use the name of the module calling watchdog()."

Calling watchdog with $type string greater than 16 chars will fail as it is stored in db as varchar(16).

Using module name as $type should therefore not be the recommended general practice as custom modules / sub-modules names may often exceed 16 chars...

Error in documentation: $variables MUST be array(), not NULL

The docs above state:

$variables Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.

If your message does not contain any tokens or string replacements, and you want to specify the severity, the quote above would indicate you should do this:

<?php
watchdog
('mymodule', $message, NULL, WATCHDOG_WARNING);
?>

However, doing so will result in this dread error message (bedeviling many different threads and irc chats):
preg_match() expects parameter 2 to be string, array given in docroot/includes/bootstrap.inc one line XXXX

So, don't follow the API docs. Use an empty array as your third parameter instead of NULL. ie, do this:

<?php
watchdog
('mymodule', $message, array(), WATCHDOG_WARNING);
?>

Of course, if the default WATCHDOG_NOTICE is good enough or you don't need to supply a link, just pass the first two parameters and don't worry about the rest as the default on the watchdog() end is to receive an array as the third param.

Suggestion: The watchdog() writers should specify with php that the third param is to be an array, like this
function watchdog($type, $message, array $variables=array(), .....

Log a system message.

Log a system message. Do u think that's enough explanation for this function??

Write the log into another DB

If I connect to another Database and use watchdog, it won't work as I'd expect.
For example:

db_set_active('db2');
user_save($user, array('name' => 'test',
'pass' => 'test',
'mail' => 'test@example.com',
'status' => 1)
); // create a new user in db2
watchdog("example module", $user->uid . " uid created", array(), WATCHDOG_INFO, null); // log the action in the default db
db_set_active('default');

It will create a new user into db2 as expected. The log, however, will be written in the default database.

Login or register to post comments