开发者

Custom watchdog logging

开发者 https://www.devze.com 2023-01-23 07:53 出处:网络
Here is the scenario I want to fix for a commerce store: Site admin logs in, creates orders for users, creates payments and/or applies credits on orders for users.

Here is the scenario I want to fix for a commerce store:

Site admin logs in, creates orders for users, creates payments and/or applies credits on orders for users.

Watchdog logs all of this, but as the admin user, when we need it to be as the customer user the action is taken against (so we can list out on that persons account all the actions the system did against their account or orders).

I see that watchdog has:

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, 
    'variab开发者_开发问答les' => $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);
  }
} 

Is there a way to override the global $user without hacking the core function and without duplicating it?

Can I pass $user as a variable somehow? Can I call $user = user_load($customer->uid) prior to watchdog() to trick it into logging that user? I would like the UID recorded to not be 0 or 1 if I can help it, because it is really hard to track logs for certain aspects of the system per user profile (account, billing, orders, etc).


You're probably better off logging this in a separate table. Watchdog is for temporary system messages, not user-facing permanent information. If nothing else, it gets periodically cleaned up on cron, which might freak users out.

If you're set on doing it this way, though, the watchdog function has a hook, hook_watchdog, that your module can change the user in. You'd have to do something a little clever with the message text - make it serialized information that you parse and unpack in the hook - but it's certainly possible.


try this code:

    global $user;
    $original_user = $user;
    session_save_session(FALSE);
    $user = user_load(array("uid" => NEW_USER_UID));      
    watchdog(HERE);
    $user = $original_user;   // @TODO: switch user: user back    
    session_save_session(TRUE);

I do not like this, but it works :)

0

精彩评论

暂无评论...
验证码 换一张
取 消