Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Logging With PHP
Log script activity in your Web application with PHP's logging functions.

| Turning Up The Heat |

If the thought of rolling your own code doesn't appeal to you, you can also use PHP's built-in error_log() function, which logs data both to a file and elsewhere. The error_log() function needs a minimum of two arguments: the error message to be logged, and an integer indicating where the message should be sent. There are three possible integer values in PHP:

0 - send the message to the system logger ("syslogd" on *NIX, and the Event Log on Windows NT);

1 - send the message to the specified email address;

3 - send the message to the specified file;

Here's a trivial example which demonstrates how this works:


<?php
// set a variable
$temp = 101.6;

// test it and log an error
// this will only work if
// you have "log_errors" and "error_log" set in your php.ini file
if ($temp > 98.6)
{
   error_log("Body temperature above normal.", 0);
}

?>


Now, if you look at the system log file after running the script, you'll see something like this:


[28-Feb-2006 15:50:49] Body temperature above normal.


You can also write the error message to a file,


<?php

// set a variable
$temp = 101.6;

// test it and log an error
if ($temp > 98.6)
{
   error_log("Body temperature above normal.", 3, "a.out");
}

?>


or send it out as email.


<?php

// set a variable
$temp = 101.6;

// test it and log an error
if ($temp > 98.6)
{
   error_log("Body temperature above normal.", 1, "administrator@this.body.com");
}

?>


It's possible to combine this error logging facility with a custom error handler to ensure that all script errors get logged to a file. Here's an example which demonstrates this:


<?php

// custom handler
function eh($type, $msg, $file, $line)
{
   // log all errors
   error_log("$msg (error type $type)", 0);
  
   // if fatal error, die()
   if ($type == E_USER_ERROR)
   {
      die($msg);
   }
}


// report all errors
error_reporting(E_ALL);

// define custom handler
set_error_handler("eh");

// let's now write some bad code

// this will trigger a warning, since the file doesn't exist
include("common.php");
?>


And here's the output that gets logged to the system log file:


[28-Feb-2006 16:15:06] Failed opening 'common.php' for inclusion (include_path='.;') (error type 2)



How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
Caching Web Sites With PEAR Cache
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback