IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » errorhandler The latest public errorhandler snipts.

showing 1-1 of 1 snipts for errorhandler
  • Log errors in diffent folders/files reflecting the application folder structure.
    <?php
    /**
     * Log PHP's error into separate files. Recommended for use in production site.
     * Should be used in set_error_handler() to handle certain PHP errors (i.e. E_STRICT).
     * @link http://www.php.net/manual/en/function.set-error-handler.php Reference
     */
    function errorHandler($errno, $errstr, $errfile=NULL, $errline=NULL, $errcontext=NULL) {
      // Log uncategorized errors
      if (is_null($errfile)) {
        $errfile = 'errors.log';
      }
    
      // Reconstruct the path
      // define('APP_DIR', '/my/application/')
      // define('LOG_DIR', '/log_dir/')
      // E.g. /my/application/controller/index.php -> /log_dir/controller/index.php
      $pos = strpos($errfile, APP_DIR);
      if ($pos !== FALSE) {
        // Strip off the application directory and attach log directory
        $errfile = LOG_DIR.substr($errfile, $pos + strlen(CMS_ROOT));
      }
      else {
        $errfile = LOG_DIR.$errfile; // E.g. /log_dir/errors.log
      }
      $dir = dirname($errfile);
      // Create dir if necessary
      if (! file_exists($dir)) {
        mkdir($dir, 0775, TRUE);
      }
      
      $logFile = fopen($errfile, 'a');
      fwrite($logFile, date('Y-m-d H:i:s ').$errstr);
      if ($errline) fwrite($logFile, " on line $errline");
      fwrite($logFile, "\n");
      fclose($logFile);
      
      if (PRODUCTION_SITE) // Prevent err msg from being printed out (log errors silently)
        return TRUE;
    
      return FALSE;
    }
    ?>
    

    copy | embed

    0 comments - tagged in  posted by hongster on Jan 21, 2010 at 2:46 a.m. EST
Sign up to create your own snipts, or login.