You are here

function jsonlog_test_filing in JSONlog 7.2

Same name and namespace in other branches
  1. 7 jsonlog.inc \jsonlog_test_filing()

Checks if log dir+file exists and is writable, and logs to watchdog.

Usable by drush script; prints messages via drush_log() instead drupal_set_message() if in CLI mode.

Parameters

string $file: Default: empty (~ use default file location algo)

Return value

boolean

See also

jsonlog_default_dir()

1 call to jsonlog_test_filing()
jsonlog_form_system_logging_settings_submit in ./jsonlog.module
Custom submit handler for the system logging settings form.

File

./jsonlog.inc, line 85
JSONlog module helper functions.

Code

function jsonlog_test_filing($file = '') {
  $is_drush = drupal_is_cli();
  if ($file) {
    $restore_dir_setting = TRUE;
    $dir = dirname($file);
    if (!$dir) {
      if (!$is_drush) {
        drupal_set_message(t('jsonlog: Log directory name is empty.'), 'error');
      }
      else {
        drush_log(dt('jsonlog: Log directory name is empty.'), 'error');
      }
      return FALSE;
    }
    $file_time_at_all = preg_match('/.+\\.\\d{6,8}\\.json\\.log$/', $file);
  }
  else {
    $restore_dir_setting = FALSE;
    if (!($dir = getenv('drupal_jsonlog_dir'))) {
      if (!($dir = variable_get('jsonlog_dir'))) {
        if (!($dir = jsonlog_default_dir())) {
          if (!$is_drush) {
            drupal_set_message(t('jsonlog: Failed to establish the server\'s default logging directory.'), 'error');
          }
          else {
            drush_log(dt('jsonlog: Failed to establish the server\'s default logging directory.'), 'error');
          }
          return FALSE;
        }
      }
    }

    // Make file name.
    if (!($site_id = getenv('drupal_jsonlog_siteid'))) {
      if (!($site_id = variable_get('jsonlog_siteid'))) {
        $site_id = jsonlog_default_site_id();
      }
    }
    if (!($file_time = getenv('drupal_jsonlog_file_time'))) {
      $file_time = variable_get('jsonlog_file_time', 'Ymd');
    }
    $file = $dir . '/' . $site_id . ($file_time == 'none' ? '' : '.' . date($file_time)) . '.json.log';
    $file_time_at_all = $file_time != 'none';
  }
  if (!file_exists($dir)) {
    if (!$is_drush) {
      drupal_set_message(t('jsonlog: The log directory doesn\'t exist.!breakPlease create the directory \'!dir\'!breakor change the \'Log file\' setting.!breakAnd if using \'Timestamped log file name\' then the directory must be writable for the web server user.', array(
        '!dir' => $dir,
        '!break' => '<br/>',
      )), 'error');
    }
    else {
      drush_log(dt('jsonlog: The log directory doesn\'t exist.!breakPlease create the directory \'!dir\'!breakor change the \'Log file\' setting.!breakAnd if using \'Timestamped log file name\' then the directory must be writable for the web server user.', array(
        '!dir' => $dir,
        '!break' => ', ',
      )), 'error');
    }
    return FALSE;
  }
  if (!file_exists($file)) {
    if (!touch($file)) {
      if (!$file_time_at_all) {
        if (!$is_drush) {
          drupal_set_message(t('jsonlog: The log file doesn\'t exist.!breakPlease create the file \'!file\'!breakor change the \'Log directory\' setting.', array(
            '!file' => $file,
            '!break' => '<br/>',
          )), 'error');
        }
        else {
          drush_log(dt('jsonlog: The log file doesn\'t exist.!breakPlease create the file \'!file\'!breakor change the \'Log directory\' setting.', array(
            '!file' => $file,
            '!break' => ', ',
          )), 'error');
        }
      }
      else {
        if (!$is_drush) {
          drupal_set_message(t('jsonlog: When using \'Timestamped log file name\' then the log directory !dir must be writable (and executable) for the web server user.', array(
            '!dir' => $dir,
            '!break' => '<br/>',
          )), 'error');
        }
        else {
          drush_log(dt('jsonlog: When using \'Timestamped log file name\' then the log directory !dir must be writable (and executable) for the web server user.', array(
            '!dir' => $dir,
            '!break' => ', ',
          )), 'error');
        }
      }
      return FALSE;
    }
  }
  elseif (!is_writable($file)) {
    if (!$file_time_at_all) {
      if (!$is_drush) {
        drupal_set_message(t('jsonlog: The log file is not writable for the webserver user.!breakPlease check permissions of the file \'!file\'!breakand make sure that it\'s parent directory is executable for the webserver user.', array(
          '!file' => $file,
          '!break' => '<br/>',
        )), 'error');
      }
      else {
        drush_log(dt('jsonlog: The log file is not writable for the webserver user.!breakPlease check permissions of the file \'!file\'!breakand make sure that it\'s parent directory is executable for the webserver user.', array(
          '!file' => $file,
          '!break' => ', ',
        )), 'error');
      }
    }
    else {
      if (!$is_drush) {
        drupal_set_message(t('jsonlog: When using \'Timestamped log file name\' then the log directory !dir must be writable (and executable) for the web server user.', array(
          '!dir' => $dir,
          '!break' => '<br/>',
        )), 'error');
      }
      else {
        drush_log(dt('jsonlog: When using \'Timestamped log file name\' then the log directory !dir must be writable (and executable) for the web server user.', array(
          '!dir' => $dir,
          '!break' => ', ',
        )), 'error');
      }
    }
    return FALSE;
  }

  // Temporarily set the conf var to test value
  // - otherwise jsonlog_watchdog() would still use the old value for this write
  $original_dir = NULL;
  if ($restore_dir_setting) {
    $original_dir = variable_get('jsonlog_dir', NULL);
    variable_set('jsonlog_dir', $dir);
  }
  try {
    watchdog('jsonlog', 'Testing watchdog logging - please check if this entry was written to file \'!file\'.', array(
      '!file' => $file,
    ), ($threshold = getenv('drupal_jsonlog_severity_threshold')) ? $threshold : variable_get('jsonlog_severity_threshold', WATCHDOG_WARNING));
  } catch (Exception $xc) {

    // Ignore; some watchdog implementation failed, and (probably) threw a
    // (database) PDOException.
  }

  // Restore original value.
  if ($restore_dir_setting) {
    if ($original_dir === NULL) {
      variable_del('jsonlog_dir');
    }
    else {
      variable_set('jsonlog_dir', $original_dir);
    }
  }
  if (!$is_drush) {
    drupal_set_message(t('jsonlog: Logging to \'!file\' seems to work,!breakbut please check if that file now contains an entry whose message starts with \'Testing watchdog logging\'.', array(
      '!file' => $file,
      '!break' => '<br/>',
    )), 'warning');
  }
  else {
    drush_log(dt('jsonlog: Logging to \'!file\' seems to work,!breakbut please check if that file now contains an entry whose message starts with \'Testing watchdog logging\'.', array(
      '!file' => $file,
      '!break' => ' ',
    )), 'warning');
  }
  return TRUE;
}