PHP Log Function

Written by

Since developing several sites, I have found that the default error/notice output can be vague or not flag the error up where it is.
This is why I have developed a PHP function which logs what ever you want it to.

function logger($type,$action,$msg){
 // Three types of logging, debug(anything not User/Security/), user and Security (Sec)
 $buffer = "\r\n".date("d-m-y(H:i:s)")."; ".$action."; ".$msg."; ".$ip;
 $type = "security";
 $buffer = "\r\n".date("d-m-y(H:i:s)")."; ".$type."; ".$action."; ".$msg."; ".$ip;
 $type = "debug";
 $logfile = "logs/".strtolower($type).".log";
 $handle = fopen($logfile, "a") or $bool = FALSE;
 $write = fwrite($handle, $buffer);
 $bool = FALSE;
 $bool = TRUE;
return $bool;

Just call it up by doing

logger("MYSQL Error","MySQL_Connect","MySQL Error".mysql_error());

which for example will produce

01-01-11(22:31:27); MySQL Error; MySQL_Connect; Database Returned Error - You have an error in your SQL syntax; at line 1;

As you can see the logs are delimited with a semi-colon (;) which makes it easy to import that data into a spreadsheet.
The logs are saved in logs/ of the page that executes the function, but you can change this if required; there are 3 logs, ‘user‘,’security‘ and ‘debug‘.

The debug log catches any $type that is not ‘user‘ or ‘sec‘.

And finally it returns TRUE on log and FALSE on error.

Hope this code helps you as much as it’s helped me.