'Recent log entries in MongoDB', 'description' => 'View events that have recently been logged in MongoDB.', 'page callback' => 'mongodb_watchdog_overview', 'access arguments' => array('access site reports'), 'file' => 'mongodb_watchdog.admin.inc', ); $items['admin/reports/mongodb-event/%mongodb_watchdog_event'] = array( 'title' => 'MongoDB log details', 'page callback' => 'mongodb_watchdog_event', 'page arguments' => array(3), 'access arguments' => array('access site reports'), 'type' => MENU_CALLBACK, 'file' => 'mongodb_watchdog.admin.inc', ); return $items; } /** * Load the MongoDB watchdog event. */ function mongodb_watchdog_event_load($id) { $result = mongodb_collection(variable_get('mongodb_collectionname', 'watchdog'))->findOne(array('_id' => $id)); return $result ? $result : FALSE; } /** * Implement hook_watchdog(). */ function mongodb_watchdog_watchdog(array $log_entry) { // Find the function that generated this error. $log_entry = (array) $log_entry; $variables = $log_entry['variables']; $variables['timestamp'] = $log_entry['timestamp']; unset($log_entry['variables'], $log_entry['timestamp']); _mongodb_watchdog_enhance_log_entry($log_entry, debug_backtrace()); $id = md5($log_entry['function'] . ':' . $log_entry['line'] . ':' . $log_entry['severity'] . ':' . $log_entry['type'] . ':' . $log_entry['message']); $collection = mongodb_collection('watchdog'); $account = $log_entry['user']; // No need to store the whole user object. $log_entry['user'] = l($account->name, "user/$account->uid"); $newobj = array( '$set' => $log_entry, '$inc' => array('count' => 1), ); $collection->update(array('_id' => $id), $newobj, array('upsert' => TRUE)); if ($variables) { $result = $collection->db->command(array('getlasterror' => 1)); $collection = $collection->db->selectCollection($id); if (empty($result['updatedExisting'])) { $max = variable_get('mongodb_watchdog_items', 10000); $collection->db->createcollection($collection->getName(), TRUE, $max * 1000, $max); } $collection->insert($variables); } } /** * Fill in the log_entry function, file, and line. */ function _mongodb_watchdog_enhance_log_entry(&$log_entry, $backtrace) { // Create list of functions to ignore in backtrace. static $ignore = array( 'mongodb_watchdog_watchdog' => 1, 'call_user_func_array' => 1, 'module_invoke' => 1, 'watchdog' => 1, '_drupal_log_error' => 1, '_drupal_error_handler' => 1, '_drupal_error_handler_real' => 1, 'theme_render_template' => 1, ); foreach ($backtrace as $bt) { if (isset($bt['function'])) { if (isset($bt['line']) && !isset($ignore[$bt['function']])) { if (isset($bt['file'])) { $log_entry['file'] = $bt['file']; } $log_entry['function'] = $bt['function']; $log_entry['line'] = $bt['line']; break; } elseif ($bt['function'] == '_drupal_exception_handler') { $e = $bt['args'][0]; _mongodb_watchdog_enhance_log_entry($log_entry, $e->getTrace()); } } } }