Examples TOCexamples

Error_Log handler - part 1.

$Date: 2004/08/07 14:28:59 $

 Table of contents

Introduction

This example requires :


This example will show you how to handle file logging in production website, without any error output on browser.

An error was volontary made on call of setAnimSpeed method (line 27). It's an API exception, we have decided to keep in stack and logs (lines 4 to 10, and 24) into a flat file (line 20) called 'progress.log' (line 17).

A message is display for user-end to let him aware of the situation (lines 29 to 34).

display_errors

Print out errors (as a part of the output). For production web sites, you're strongly encouraged to turn this feature off, and use error logging instead. Keeping display_errors enabled on a production web site may reveal security information to end users, such as file paths on your Web server, your database schema or other information.

1 <?php ini_set('display_errors', false); ?>

log_errors

Log errors into a log file (server-specific log, stderr, syslog ...) As stated above, you're strongly advised to use error logging in place of error displaying on production web sites.

1 <?php ini_set('log_errors', true); ?>

 Render options

Default options below are used :

Only option changed is :

Legend for lineFormat: Legend for contextFormat:

[Top]

 Output

We are sorry, but this page could not be displayed because of internal error.The error has been recorded and will be fixed as soon as possible.Our apologies for the inconvenience.

Inside the progress.log file:
Aug 07 13:32:50 127.0.0.1 [exception] invalid input, parameter #1 "$delay" was expecting "integer", instead got "string" in html_progress::setanimspeed (file d:\php\pear\html_progress\examples\errorstack\secure\log_errors-p1.php at line 27)

[Top]

 PHP source syntax highlight

  1. <?php
  2. require_once 'HTML/Progress.php';
  3.  
  4. function _pushCallback($err)
  5. {
  6.     // now don't die if the error is an exception
  7.     if ($err['level'] == 'exception') {
  8.         return HTML_PROGRESS_ERRORSTACK_PUSHANDLOG;
  9.     }
  10. }
  11.  
  12. ini_set('display_errors', false);      // be sure to display nothing
  13. ini_set('log_errors', true);           // be sure to logs errors
  14.  
  15.  
  16. $options = array(
  17.     'destination' => 'progress.log',
  18.     'extra_headers' => ''
  19. );
  20. $logger['handler']['error_log'] = array('name' => HTML_PROGRESS_LOG_TYPE_FILE,
  21.                                         'ident' => $_SERVER['REMOTE_ADDR'],
  22.                                         'conf' => $options
  23.                                         );
  24. $logger['push_callback'] = '_pushCallback';
  25.  
  26. $bar = new HTML_Progress($logger);
  27. $bar->setAnimSpeed('100');   // < - - - will generate an API exception
  28.  
  29. if ($bar->hasErrors()) {
  30.     $msg  = "We are sorry, but this page could not be displayed because of internal error.";
  31.     $msg .= "The error has been recorded and will be fixed as soon as possible.";
  32.     $msg .= "Our apologies for the inconvenience.";
  33.     die($msg);
  34. }
  35.  
  36. echo "without error the process continue here ...";
  37. ?>

[Top]