debug_backtrace and _drupal_error_handler_real with very high mem usage, can I disable? I was profiling some of my code with xhprof, and I noticed that debug_backtrace() and _drupal_error_handler_real() have a very high memory usage.



I set "Error message display" to "None" at admin/config/development/logging, and I also have database logging module disabled. Still, these functions seem to collect error information. Do you have any idea why it happens?

My code does generate some PHP warnings. If I fix them, would these error calls disappear?
_drupal_error_handler_real() is the function called from the error handler installed from Drupal (_drupal_error_handler()). A PHP error handler is called every time there is a not-handled runtime error (not an exception), including a warning.

The code execute from _drupal_error_handler_real() is the following one. debug_backtrace() is always called, if the error level is one set in PHP.

  if ($error_level &amp; error_reporting()) {
    $types = drupal_error_levels();
    list($severity_msg, $severity_level) = $types[$error_level];
    $caller = _drupal_get_last_caller(debug_backtrace());

    if (!function_exists('filter_xss_admin')) {
      require_once DRUPAL_ROOT . '/includes/common.inc';
    }

    // We treat recoverable errors as fatal.
    _drupal_log_error(array(
      '%type' =&gt; isset($types[$error_level]) ? $severity_msg : 'Unknown error',
      // The standard PHP error handler considers that the error messages
      // are HTML. We mimick this behavior here. 
      '!message' =&gt; filter_xss_admin($message), 
      '%function' =&gt; $caller['function'], 
      '%file' =&gt; $caller['file'], 
      '%line' =&gt; $caller['line'], 
      'severity_level' =&gt; $severity_level,
    ), $error_level == E_RECOVERABLE_ERROR);
  }


Also _drupal_log_error() is called when debug_backtrace() is called, but without any module recording the error, the error is not effectively recorded. In fact, the error is recorded if there are modules implementing hook_watchdog(), such as the Database logging module, and the Syslog module. The task of watchdog(), the function called by _drupal_log_error() is pass the information about the error to the implementations of hook_watchdog() which are then free to record the error.

// Prepare the fields to be logged
$log_entry = array(
  'type' =&gt; $type, 
  'message' =&gt; $message, 
  'variables' =&gt; $variables, 
  'severity' =&gt; $severity, 
  'link' =&gt; $link, 
  'user' =&gt; $user, 
  'uid' =&gt; $user_uid, 
  'request_uri' =&gt; $base_root . request_uri(), 
  'referer' =&gt; isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', 
  'ip' =&gt; ip_address(),
  // Request time isn't accurate for long processes, use time() instead. 
  'timestamp' =&gt; time(),
);

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