Home arrow PHP arrow Page 4 - Logging With PHP

Turning Up The Heat - PHP

Want to log script activity in your Web application, but haveno clue where to begin? Take our handy tour of PHP's logging functions,and find out how simple it really is.

  1. Logging With PHP
  2. The Bare Necessities
  3. Turning The Tables
  4. Turning Up The Heat
  5. Biting Into A PEAR
  6. Destination Unknown
  7. Artificial Intelligence
  8. Big Brother Is Watching
  9. Closing Time
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 53
November 27, 2002

print this article


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 4.x:

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-2002 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-2002 16:15:06] Failed opening 'common.php' for inclusion (include_path='.;') (error type 2)

>>> More PHP Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: