PEAR logo

HTML_CSS : The Definitive Guide

Error Context Display

In some cases, you may want to customize error generation. For instance, for each error (basic/exception), it is useful to include file, line number, and class/function context information in order to trace it. The default option will be sufficient for most cases but you want perhaps customize the output format (render) of context information.

With this example we will change display and log renders.

  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. $displayConfig = array(
  5.     'lineFormat' => '<b>%1$s</b>: %2$s<br />%3$s',
  6.     'contextFormat' =>   '<b>File:</b> %1$s <br />'
  7.                        . '<b>Line:</b> %2$s <br />'
  8.                        . '<b>Function:</b> %3$s '
  9. );
  10. $logConfig = array(
  11.     'lineFormat' => '%1$s %2$s [%3$s] %4$s',
  12.     'timeFormat' => '%b'
  13. );
  14.  
  15. $prefs = array(
  16.     'handler' => array('display' => $displayConfig,
  17.                        'log'     => $logConfig
  18. ));
  19.  
  20. $css = new HTML_CSS(null, $prefs);
  21. // ...
  22. $result = $css->setStyle('div', 'color', 5);
  23. ?>

Display render will give something like:

Exception1: invalid input, parameter #3 "$value" was expecting "string", instead got "integer"2
File: [path_to]\[filename] 3
Line: 22 3
Function: html_css->setstyle 3
     
1

error level

2

message body with context informations

3

call context (file, line, function)

Log render will give something like:

Jun 127.0.0.11 [exception2] invalid input, parameter #3 "$value" was expecting "string", instead got "integer"3
     
1

client ip address and execution date

2

error level

3

message body with context informations

[Note] Note

To have both display and log output, check the php.ini display_errors and log_errors values : must be set to TRUE.

Let review, step by step, how to get such results.

Remember that with default classes, there are two drivers : display and log that have both their own configuration parameters. You can override these parameters values with the handler entry in the hash of second argument of the HTML_CSS class constructor.

We did it here with the $prefs variable; it's a two keys associative array. First key display defines the display driver values, and the second key log defines the log driver values.

Review the display driver custom values. Only two keys: lineFormat, contextFormat are redefined, thats means remains key eol keep its default value. See table below.

Table 10.2. Display driver configuration parameters

Parameter Type Default Description
eol string <br />\n The end-on-line character sequence
lineFormat string <b>%1$s</b>: %2$s %3$s Log line format specification:
  • 1$ = error level
  • 2$ = error message (body)
  • 3$ = error context
contextFormat string in <b>%3$s</b> (file <b>%1$s</b> on line <b>%2$s</b>) Context format (class, file, line) specification:
  • 1$ = script file name
  • 2$ = line in script file
  • 3$ = class/method names
[Tip] Tip

If you don't wish to see context information in the error message, then remove the parameter %3$ in the lineFormat option even if contextFormat is set.

Review now the log driver custom values. Only two keys lineFormat, timeFormat are redefined, thats means six remains keys eol, contextFormat, ident, message_type, destination, extra_headers keep their default values. See table below.

Table 10.3. Log driver configuration parameters

Parameter Type Default Description
eol string \n The end-on-line character sequence
lineFormat string %1$s %2$s [%3$s] %4$s %5$s Log line format specification:
  • 1$ = time error
  • 2$ = ident (client ip)
  • 3$ = error level
  • 4$ = error message (body)
  • 5$ = error context
contextFormat string in %3$s (file %1$s on line %2$s) Context format (class, file, line) specification:
  • 1$ = script file name
  • 2$ = line in script file
  • 3$ = class/method names
timeFormat string %b %d %H:%M:%S Time stamp format used by strftime
ident string REMOTE_ADDR Client IP
message_type string 3 Destination type used by error_log
destination string html_css_error.log Destination name used by error_log
extra_headers string NULL Extra headers depending of destination type
[Tip] Tip

If you don't wish to see context information in the error message, then remove the parameter %5$ in the lineFormat option even if contextFormat is set.

HTML_CSS : The Definitive Guide v 1.5.0 : January 15, 2008