Jump to content

Archived

This topic is now archived and is closed to further replies.

kevdoug

define error handling directory.

Recommended Posts

Hi I've got this error handling script but I am unsure how to define the directory for the log here is the full path where I have the logfile.txt  C:/log/logfile.txt I have tried [color=red]define ('logfile', C:/log/logfile.txt(__FILE__) . '/logfile.txt');[/color] I have tried other ways but with no luck.

and here is the full script.

<?php

// Location of the error log file
define ([color=green]'logfile'[/color], dirname([color=purple]__FILE__[/color]) . [color=green]'/logfile.txt'[/color]);

function handle_errors($errlevel, $errstr, $errfile='', $errline='', $errcontext='') {
  $errstr = htmlentities($errstr);
  $error = '[' . date('d/m/Y H:i:s') . '] ';

switch ($errlevel) {
  case E_USER_ERROR:
  $error .= "ERROR: ";

  echo 'Sorry, something unexpected happen, and it has been logged for further investigation. Please go back where you came from. Thank you for your understanding.';
  $die = true;
  break;
  case E_USER_WARNING:
  case E_WARNING:
  $error .= "WARNING: ";
  break;
  case E_USER_NOTICE:
  case E_NOTICE:
  $error .= "NOTICE: ";
  break;
  default:
  $error .= "UNKNOWN: ";
  break;
  }

  $error .= " $errstr in line $errline of file $errfile\n";

  // Log error
  $f = fopen(logfile, 'a');
  fwrite($f, $error);
  fclose($f);

  // Error -> stop script execution?
  if (isset($die) AND $die == true) {
          die();
  }
}

echo

set_error_handler('handle_errors');

// Throws a notice
$a = $b;

// Throws a warning:
$f = fopen('bla');

// Throws a user notice:
trigger_error ('Something went wrong!',  E_USER_NOTICE);

// Throws a user warning:
trigger_error ('Something went REALLY wrong!', E_USER_WARNING);

// Throws a user error:
trigger_error ('It\'s completely broken!', E_USER_ERROR);

?>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.