You are here

filelog.module in File Log 6

Writes logging messages into files.

File

filelog.module
View source
<?php

/**
 * @file
 * Writes logging messages into files.
 */

/**
 * Implementation of hook_watchdog().
 */
function filelog_watchdog($entry) {
  $f = _filelog_get_conf();
  if ($entry['severity'] <= $f['log_level']) {
    if ($dir = filelog_directory()) {
      $file = filelog_filename($entry);
      $retry = 0;
      while ($retry < 3) {
        if ($fp = @fopen($dir . '/' . $file, 'ab')) {
          fwrite($fp, theme('filelog_format', $entry) . "\n");
          fclose($fp);
          $retry = 3;
          break;
        }
        $retry++;
        usleep(250);
      }
    }
  }
}

/**
 * Implementation of hook_theme().
 */
function filelog_theme() {
  return array(
    'filelog_format' => array(
      'arguments' => array(
        'entry' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_cron().
 */
function filelog_cron() {
  $f = _filelog_get_conf();
  if ($f['daily_files'] && ($days = $f['delete_files'])) {
    if ($files = file_scan_directory(filelog_directory(), '.+')) {
      $limit = format_date(time() - $days * 24 * 3600, 'custom', 'Ymd');
      foreach ($files as $filename => $info) {
        $parts = explode('.', $filename);
        array_shift($parts);
        if (array_shift($parts) < $limit) {
          file_delete($filename);
        }
      }
    }
  }
}

/**
 * Format a file log entry.
 *
 * @ingroup themeable
 */
function theme_filelog_format($entry) {
  global $base_url;
  $f = _filelog_get_conf();

  //shortcuts
  $sep =& $f['field_separator'];
  $prefix =& $f['field_prefix'];
  $suffix =& $f['field_suffix'];
  $message = '';
  if ($f['log_base_url']) {
    $message .= $prefix . $base_url . $suffix . $sep;
  }
  if ($f['date_format']) {
    if (in_array($f['date_format'], array(
      'small',
      'large',
      'medium',
    ))) {
      $entry['timestamp'] = format_date($entry['timestamp'], $f['date_format'], '', NULL, 'en');
    }
    else {
      $entry['timestamp'] = format_date($entry['timestamp'], 'custom', $f['date_format'], NULL, 'en');
    }
  }
  $message .= $prefix . $entry['timestamp'] . $suffix;
  if ($f['grouped_severity_in_name'] || !$f['severity_in_name']) {
    $message .= $sep . $prefix . ($f['decode_severity'] ? _filelog_decode_severity($entry['severity']) : $entry['severity']) . $suffix;
  }
  if (!$f['type_in_name']) {
    $message .= $sep . $prefix . $entry['type'] . $suffix;
  }
  if ($f['log_username']) {
    $message .= $sep . $prefix . ($entry['user']->uid ? $entry['user']->name : variable_get('anonymous', t('Anonymous'))) . $suffix;
  }
  else {
    $message .= $sep . $prefix . $entry['user']->uid . $suffix;
  }
  $message .= $sep . $prefix . $entry['ip'] . $suffix;
  $message .= $sep . $prefix . $entry['request_uri'] . $suffix;
  $message .= $sep . $prefix . $entry['referer'] . $suffix;
  $message .= $sep . $prefix . ($f['skip_strip_tags'] ? $entry['link'] : strip_tags($entry['link'])) . $suffix;
  if (is_object($entry['variables'])) {
    $entry['variables'] = (array) $entry['variables'];
  }
  else {
    if (!is_array($entry['variables'])) {
      $entry['variables'] = array();
    }
  }
  if (!$f['wrap_message']) {
    $prefix = $suffix = '';
  }
  $text = is_null($entry['variables']) ? $entry['message'] : strtr($entry['message'], $entry['variables']);
  $message .= $sep . $prefix . ($f['skip_strip_tags'] ? $text : strip_tags($text)) . $suffix;
  return $message;
}
function filelog_directory($check = TRUE, $refresh = FALSE) {
  static $dir;
  $f = _filelog_get_conf();
  if (is_null($dir) || $refresh) {
    if (is_string($f['dir'])) {
      $dir = $f['dir'];
    }
    else {
      $dir = conf_path() . '/logs';
    }
    if ($check && !file_check_directory($dir, FILE_MODIFY_PERMISSIONS)) {
      $dir = FALSE;
    }
  }
  return $dir;
}
function filelog_filename($entry) {
  $f = _filelog_get_conf();
  $name = array();
  if ($f['site_in_name']) {
    $name[] = $f['site_in_name'] === TRUE ? array_pop(explode('/', conf_path())) : $f['site_in_name'];
  }
  $name[] = $f['type_in_name'] ? preg_replace('|\\s+|', '_', $entry['type']) : 'watchdog';
  if ($f['daily_files']) {
    $name[] = format_date(time(), 'custom', 'Ymd');
  }
  if ($f['severity_in_name']) {
    $name[] = _filelog_decode_severity($entry['severity']);
  }
  else {
    if ($f['grouped_severity_in_name']) {
      switch ($entry['severity']) {
        case WATCHDOG_DEBUG:
          $name[] = 'debug';
          break;
        case WATCHDOG_INFO:
        case WATCHDOG_NOTICE:
        case WATCHDOG_WARNING:
          $name[] = 'info';
          break;
        case WATCHDOG_ERROR:
        case WATCHDOG_CRITICAL:
        case WATCHDOG_ALERT:
        case WATCHDOG_EMERG:
          $name[] = 'error';
          break;
      }
    }
  }
  $name[] = 'log';
  return implode('.', $name);
}
function _filelog_get_conf() {
  global $conf;
  static $f;
  if (is_null($f)) {
    $f = !array_key_exists('filelog', $conf) || !is_array($conf['filelog']) ? array() : $conf['filelog'];
    $f += array(
      'dir' => FALSE,
      'log_level' => WATCHDOG_DEBUG,
      'site_in_name' => FALSE,
      'type_in_name' => FALSE,
      'severity_in_name' => FALSE,
      'grouped_severity_in_name' => FALSE,
      'daily_files' => FALSE,
      'delete_files' => FALSE,
      'field_separator' => '|',
      'field_prefix' => '',
      'field_suffix' => '',
      'wrap_message' => FALSE,
      'log_base_url' => FALSE,
      'log_username' => FALSE,
      'date_format' => FALSE,
      'decode_severity' => FALSE,
      'skip_strip_tags' => FALSE,
    );
  }
  return $f;
}
function _filelog_decode_severity($severity) {
  switch ($severity) {
    case WATCHDOG_DEBUG:
      return 'debug';
    case WATCHDOG_INFO:
      return 'info';
    case WATCHDOG_NOTICE:
      return 'notice';
    case WATCHDOG_WARNING:
      return 'warning';
    case WATCHDOG_ERROR:
      return 'error';
    case WATCHDOG_CRITICAL:
      return 'critical';
    case WATCHDOG_ALERT:
      return 'alert';
    case WATCHDOG_EMERG:
      return 'emergency';
    default:
      return 'unknown';
  }
}

Functions