Part of the point of breaking out watchdog into a modularizable hook_watchdog() would be so that module developers could 1) create alternative methods for logging, and 2) have some input on the logging itself.

the code currently implements it this way:

  foreach (module_implements('watchdog', TRUE) as $module) {
    module_invoke($module, 'watchdog', $log_message);
  }

By doing it this way, the $log_message gets passed as a copy to each module's watchdog hook, which means that no module can actually interact with the $log_message in any meaningful way.

For example, say you wanted to implement some custom module that would simply eliminate certain kinds of log messages from the watchdog, you might write

function mymodule_watchdog(&$log_message) {
  if ($log_message['type']=='page not found') {
    $log_message = array();//
  }
}

which would see if the log message was of type page not found, and then set the $log_message to an empty array.

However, if we used module_invoke_all('watchdog', $log_message);
instead of

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

then we *CAN* modify the log_message, which gives us a lot more flexibility.

On the final end, all we need to do in any hook_watchdog that actually outputs the message, put a sanity check (which currently does NOT exist for some reason--another issue), like so:

if (empty($log_message)) return;

For example, dblog.module does this:

function dblog_watchdog($log = array()) {
  $current_db = db_set_active();
  db_query("INSERT INTO {watchdog}
    (uid, type, message, variables, severity, link, location, referer, hostname, timestamp)
    VALUES
    (%d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', %d)",
    $log['user']->uid,
    $log['type'],
    $log['message'],

with no sanity check. ie, it can write empty log messages to the database, or junk messages. why waste those cycles?

Anyway, the long and the short of it is, why not use the more powerful and flexible module_invoke_all() in the watchdog() function in bootstrap.inc instead of the klunkier and less flexible foreach/module_invoke code that's there now.

Comments

apotek’s picture

Category: task » feature
Status: Needs review » Active
q0rban’s picture

Version: 6.x-dev » 8.x-dev

Makes sense to me.

markdorison’s picture

This would be very useful.

apotek’s picture

Issue summary: View changes

Cleaned up some of my sample code.

jhedstrom’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs issue summary update

Error reporting has been substantially refactored--is this still relevant?

dawehner’s picture

Status: Postponed (maintainer needs more info) » Fixed

In Drupal 8 someone can totally change the logic by swapping out the services ...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.