You are here

function flog_it in File logger 7

Same name and namespace in other branches
  1. 6 flog.module \flog_it()

Dump a variable to a file.

Parameters

$var: The variable you wish to dump. Formatted using print_r()

$label: An optional label to include with the output.

$filename: An optional filename to write to. If not provided uses the default.

Return value

TRUE: file write succeeded FALSE: file write failed

See also

print_r()

1 call to flog_it()
flog_stack in ./flog.module
Dump the php stack to a file using debug_backtrace().

File

./flog.module, line 46
File logger. Dump a variable to a configurable file.

Code

function flog_it($var, $label = NULL, $filename = NULL) {
  if (!variable_get('flog_enabled', 0)) {
    return FALSE;
  }
  if (!($path = variable_get('flog_path', '/var/log'))) {
    watchdog('flog', 'No path defined for log file.', array(), WATCHDOG_ERROR);
    return FALSE;
  }
  if (!isset($filename)) {
    if (!($filename = variable_get('flog_file', 'drupal.log'))) {
      watchdog('flog', 'No name defined for default log file.', array(), WATCHDOG_ERROR);
      return FALSE;
    }
  }
  $logfile = $path . '/' . $filename;
  $date_format = variable_get('flog_date', 'Y-m-d H:i:s');
  $formatted_var = print_r($var, TRUE);
  $output = $date_format ? '[' . date($date_format) . '] ' : '';
  $output .= $label ? '(' . $label . ') ' : '';
  $output .= $formatted_var . "\n";

  // Open the log file, or create it if it doesn't exist.
  if (!($handle = fopen($logfile, 'a'))) {
    watchdog('flog', 'Cannot open log file %file', array(
      '%file' => $logfile,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  if (fwrite($handle, $output) === FALSE) {
    watchdog('flog', 'Cannot write log file %file', array(
      '%file' => $logfile,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  fclose($handle);
  return TRUE;
}