#native_company# #native_desc#
#native_cta#

File based, custom logging Page 2

By John Starkey
on September 30, 2002

How It’s Done

First we declare two variables containing the info for our log file and the date in our desired
format. This will make things easier if we need to change the location, the filename and/or the
date format at a later time. In this example I will use /tmp/article.log because I am using my
development machine. Please, do not use /tmp on a production machine. Instead, have your admin s
et up a directory outside of the DocumentRoot that the server can write to, and you can read.
/tmp is normally chmod 777, meaning that anyone can read, write or execute from that directory.
This can be a major security hazard.
With that said, we declare the variables:

<?php

$log_file 
'/tmp/article.log';

$date date('Y-m-d H:i:s (T)');

?>



Next, we create a function for handling the errors. To keep things simple, I will provide a
very basic example. Of course, you are free to expand on this as you wish:

<?php

function simplisticErrorHandler($errno$errmsg$filename$linenum$vars)

{

    global 
$log_file$date;

    if ( 
$errno == E_USER_NOTICE )

    {

        
$error "$date - ".$_SERVER['REMOTE_ADDR']." - NOTICE: $errmsg in $filename on line $linenumn";

        
error_log$error3$log_file );

    }

}

?>



This function will handle only the errors that you trigger and is pretty cut and dry, so I won’t
go into detail. If you are having a bit of an issue it’s probably best that you drop back a little,
until you are a little more familiar with PHP.
The key player in this function is error_log(), which takes 3 arguments: the message you want logged,
the way we want the error handled and finally the destination. The first argument is pretty self-explanatory,
as is the third. The second argument determines how PHP handles logging the file and, taken from
http://www.php.net/error_log, our current options are:
– 0 message is sent to PHP’s system logger, using the Operating System’s system logging mechanism or a file, depending on what the error_log configuration directive is set to.
– 1 message is sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used. This message type uses the same internal function as mail() does.
– 2 message is sent through the PHP debugging connection. This option is only available if remote debugging has been enabled. In this case, the destination parameter specifies the host name or IP address and optionally, port number, of the socket receiving the debug information.
– 3 message is appended to the file destination.
So, naturally, in our case #3 is our destination of choice.