| 6 core.php | hook_watchdog($log_entry) |
| 7 system.api.php | hook_watchdog(array $log_entry) |
| 8 system.api.php | hook_watchdog(array $log_entry) |
Log an event message
This hook allows modules to route log events to custom destinations, such as SMS, Email, pager, syslog, ...etc.
Parameters
$log_entry: An associative array containing the following keys:
- type: The type of message for this entry. For contributed modules, this is normally the module name. Do not use 'debug', use severity WATCHDOG_DEBUG instead.
- user: The user object for the user who was logged in when the event happened.
- request_uri: The Request URI for the page the event happened in.
- referer: The page that referred the use to the page where the event occurred.
- ip: The IP address where the request for the page came from.
- timestamp: The UNIX timetamp of the date/time the event occurred
- severity: One of the following values as defined in RFC 3164 http://www.faqs.org/rfcs/rfc3164.html 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
- link: an optional link provided by the module that called the watchdog() function.
- message: The text of the message to be logged.
Return value
None.
Related topics
2 functions implement hook_watchdog()
1 invocation of hook_watchdog()
File
- developer/
hooks/ core.php, line 2557 - These are the hooks that are invoked by the Drupal core.
Code
function hook_watchdog($log_entry) {
global $base_url, $language;
$severity_list = 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'),
);
$to = 'someone@example.com';
$params = array();
$params['subject'] = t('[@site_name] @severity_desc: Alert from your web site', array(
'@site_name' => variable_get('site_name', 'Drupal'),
'@severity_desc' => $severity_list[$log_entry['severity']],
));
$params['message'] = "\nSite: @base_url";
$params['message'] .= "\nSeverity: (@severity) @severity_desc";
$params['message'] .= "\nTimestamp: @timestamp";
$params['message'] .= "\nType: @type";
$params['message'] .= "\nIP Address: @ip";
$params['message'] .= "\nRequest URI: @request_uri";
$params['message'] .= "\nReferrer URI: @referer_uri";
$params['message'] .= "\nUser: (@uid) @name";
$params['message'] .= "\nLink: @link";
$params['message'] .= "\nMessage: \n\n@message";
$params['message'] = t($params['message'], array(
'@base_url' => $base_url,
'@severity' => $log_entry['severity'],
'@severity_desc' => $severity_list[$log_entry['severity']],
'@timestamp' => format_date($log_entry['timestamp']),
'@type' => $log_entry['type'],
'@ip' => $log_entry['ip'],
'@request_uri' => $log_entry['request_uri'],
'@referer_uri' => $log_entry['referer'],
'@uid' => $log_entry['user']->uid,
'@name' => $log_entry['user']->name,
'@link' => strip_tags($log_entry['link']),
'@message' => strip_tags($log_entry['message']),
));
drupal_mail('emaillog', 'log', $to, $language, $params);
}
Login or register to post comments
Comments
Don't forget the variables
The above code doesn't include the variables that the message needs, so you end up getting mails with message content like:
%message in %file on line %line.
So the @message line should be:
'@message' => strip_tags(t($log_entry['message'], $log_entry['variables'])),Also, the drupal_mail() on its own will send an empty email, so you need a mail hook like this:
function hook_mail($key, &$message, $params) {switch($key) {
case 'log':
$message['subject'] = $params['subject'];
$message['body'][] = $params['message'];
break;
}
}
Filter by log severity
Severity might be confusing. If you want to filter all messages which are at least at WARNING level, then use the "lower or equal" operator (not "higher or equal") :
<?phpfunction mymodule_watchdog($log_entry) {
// will apply to WARNINGs, ERRORs, CRITICALs, ALERTs, EMERGencies
if ($log_entry['severity'] <= WATCHDOG_WARNING) {
}
}
?>
For your information, WATCHDOG_EMERG equals 0, and WATCHDOG_DEBUG equals 7.