7, September 2008

Custom error reporting script - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Custom error reporting script
« previous next »
Pages: [1] Print

Author Topic: Custom error reporting script  (Read 1296 times)
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« on: Sep 05, 2005, 03:06:27 PM »

Is there any custom script that can be used for logging the php errors?

I have setup my server so it wont show any error, but I would like to have the abillity to know when an error occures

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Novice Spammer
***
Posts: 100
103 credits
Members referred : 0



« Reply #1 on: Sep 08, 2005, 07:31:11 AM »

I have not seen a script like this, but I will let you know if I see one.
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #2 on: Sep 08, 2005, 11:32:01 AM »

Ok. I figured it out. Here is the solution :


Code:
<?php
 
function handler($errno$errstr$errfile$errline)
 {
        
$fp fopen($_SERVER["DOCUMENT_ROOT"] . "/error.log""w");
        
fwrite$fpdate("Y-m-d") . " Error $errno : $errstr in lile $errline of file $errfile\n" );
        
fclose($fp);
 }
 
set_error_handler("handler");
?>


Put this on every page of your site, and you'll have a simple error logging script.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Novice Spammer
***
Posts: 100
103 credits
Members referred : 0



« Reply #3 on: Sep 08, 2005, 11:42:02 PM »

Ah, very useful when you are developing scripts. Thanks
I love Pokemon
*
Posts: 14
0 credits
Members referred : 0


« Reply #4 on: Sep 08, 2005, 11:42:43 PM »

I generally dont use this sort of method but I will try it out.
Raped By Google
*
Gender: Female
Posts: 27
240 credits
Members referred : 0



« Reply #5 on: Sep 10, 2005, 05:17:42 PM »

Great stuff you shared there Nikolas!

By applying this script to the whole site you are sure that your site runs ok without problems.
Cyberpunk Wannabe
*
Gender: Male
Posts: 39
172 credits
Members referred : 0



« Reply #6 on: Sep 13, 2005, 06:12:43 PM »

Great script! and really simple.

I allways wanted something to tell me if there are problems in the server.

I've puted a line to email me the error, so I can sleep at night now... Smiley

Thanks for the information Nikolas
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #7 on: Sep 17, 2005, 05:31:17 PM »

I made a small fault in the code I submitted. The right code is :

Code:
<?php
function handler($errno$errstr$errfile$errline)
{
        
$fp fopen($_SERVER["DOCUMENT_ROOT"] . "/error.log""a");
        
fwrite$fpdate("Y-m-d") . " Error $errno : $errstr in lile $errline of file $errfile\n" );
        
fclose($fp);
}
set_error_handler("handler");
?>

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
He's The Man
Novice Spammer
***
Gender: Male
Posts: 100
0 credits
Members referred : 0



« Reply #8 on: Sep 17, 2005, 07:25:54 PM »

Thats pretty cool, I've never thought of logging my errors.
Novice Spammer
***
Gender: Male
Posts: 100
346 credits
Members referred : 0



« Reply #9 on: Sep 19, 2005, 11:44:21 PM »

That is a really neat script. I will probaby implement it on my new site.
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #10 on: Oct 04, 2005, 03:57:18 AM »

I had a script on our old server that scanned the error logs every hour and emailed the account owner / tech a list of the php errors that occured in that hour. I will see if I can find it...


Last blog : Site of the Month - August 2007
I wish I was an Oscar winner
**
Posts: 86
247 credits
Members referred : 0



« Reply #11 on: Oct 17, 2005, 03:04:20 AM »

Here is a mysql implementation of your script :


First create the table in the database by executing this query :

Code:
CREATE TABLE `error_log` (
  `date` datetime NOT NULL default '0000-00-00 00:00:00',
  `err_type` varchar(30) NOT NULL default '',
  `err_no` int(4) NOT NULL default '0',
  `err_str` text NOT NULL,
  `file` text NOT NULL,
  `line` int(8) NOT NULL default '0',
  KEY `date` (`date`,`err_no`),
  KEY ` err_type ` (`err_type`)
) TYPE=MyISAM;

Here is the php code :

Code:
<?php
function handler($errno$errstr$errfile$errline)
 {
   global $config;
   $errortype = array (
          E_ERROR          => "Error",
          E_WARNING        => "Warning",
          E_PARSE          => "Parsing Error",
          E_NOTICE          => "Notice",
          E_CORE_ERROR      => "Core Error",
          E_CORE_WARNING    => "Core Warning",
          E_COMPILE_ERROR  => "Compile Error",
          E_COMPILE_WARNING => "Compile Warning",
          E_USER_ERROR      => "User Error",
          E_USER_WARNING    => "User Warning",
          E_USER_NOTICE    => "User Notice",
          E_STRICT          => "Runtime Notice"
    );
   
   if 
$errno != && $errno != )//This line is to avoid simple errors
   {
    $db_link mysql_pconnect("host","user","pass");//Change this line and the next to your settings
    $db_name "name_of_my_database";

    $errstr str_replace("'""\\'"$errstr);
    $errfile str_replace("'""\\'"$errfile);
    mysql_db_query($db_name,"INSERT LOW_PRIORITY INTO error_log VALUES(NOW(), '"$errortype[$errno] ."', '$errno', '$errstr', '$errfile', '$errline')"$db_link);
   }
 }
 
set_error_handler("handler");
?>

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #12 on: Oct 17, 2005, 03:13:39 AM »

That's a very nice implementation wolf.

I use something simmilar to this in my sites.

The only thing you should check is the mysql_pconnec command, because I think that not every hosting account allow persistant connections, so if you have problem with that you can replace it with mysql_connect(.....

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
The mushroom boy
Hunky Junky Monky Man!
**
Posts: 65
418 credits
Members referred : 0



« Reply #13 on: Oct 26, 2005, 04:30:13 PM »

There is another way to log the errors.

If you have access to the virtual host configuration file, you can add there this line :
Code:
php_value error_log "/path/to/my/logs/php.site.err"

This way the php will log all the errors to this file
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #14 on: Oct 31, 2005, 06:05:27 PM »

OK I have found the script. Feel free to improve it!

Code:
<?php

/**
* PHP error logs are stored here: /usr/local/apache/logos/php_error_log
* The php.ini file that tells php where to store error logs (at the above location) is this one: /usr/local/lib/php.ini
* This script should have 755 permissions for the cPanel crontab to run it.
**/

/*
This script is designed to email report(s) of new errors written to the server-wide php error log. An error line might look like this:

[29-Oct-2004 12:28:22] PHP Warning: main(header.inc): failed to open stream: No such file or directory in /home/sample/public_html/enquiries.htm on line 7

The aim is to identify each error line by the account name (in this case 'sample'), group together all errors for that account name
*/

$developers = array (
"name@domain .com" => array("sample""sample2""sample3"),
"name2@domain2 .com" => array("sample4""sample5"),
);


define("PHP_ERR_DIR""/usr/local/apache/logs/");
define("PHP_ERR_SIZE_FILE"PHP_ERR_DIR "php_error_log.size"); // 644 root.host
define("PHP_ERR_FILE"PHP_ERR_DIR "php_error_log");
define("PHP_ERR_MAILTO","tech@techdomain .com");


$fp fopen(PHP_ERR_SIZE_FILE"r");
$lastsize = (int) fread($fpfilesize(PHP_ERR_SIZE_FILE));
fclose ($fp);

$size filesize(PHP_ERR_FILE);

$count 0// count of how many new errors since last time this script ran
if ($size != $lastsize// error log is bigger than the last time this script ran, so continue reporting.
{
$ferrp fopen(PHP_ERR_FILE"r");
fseek($ferrp$lastsize);
// Move all the error lines since the last report into $errors
$errors fread($ferrp$size-$lastsize);
fclose ($ferrp);

// Move all the lines into an array for easier processing based on detecting line breaks
$a_errors explode("\n",$errors);
$count count($a_errors);
foreach ($a_errors as $key => $line)
{
if ($line == "")
{
continue; // blank lines possible - reject these.
}
$before "";
$after "";
$accName "";
list($before,$after) = explode (" in /home/",$line);
list($accName,$theRest)= explode ("/",$after);
if ($accName == "")
{
$accName "unknown";
}
$a_accounts["$accName"][] = $line// groups all errors for the same account into a multi-dimensional array.
}

// Now for the reporting...
$count_total 0;
$message_summary "";
foreach ($a_accounts as $accName => $a_errorLines)
{
$message "New php errors on hosting account '$accName'

--------START PHP ERRORS----------------------------------------
"
;
$count 0;
foreach ($a_errorLines as $key => $errorLine)
{
$count++;
$message .= "Err#$count \t $errorLine\r\n\r\n"// aggregate all error lines for a single account into a single message
}
$count_total $count_total $count;
$message_summary .= "$count\t$accName\r\n";
$message .= "--------STOP PHP ERRORS-----------------------------------------

* This is a free, fully automated service. Any php errors detected and logged for your hosting account '$accName' will be forwarded to you.
* It's entirely up to you whether or not you choose to act on this information - but we recommend that you do.

Regards,
Tech Team

----------------------------------------------------------------
- this report generated by cron job 'check_php_errlog.php' and scheduled using cPanel"
;

$flag false;
if (is_array($developers))
{
foreach ($developers as $devEmail => $accounts)
{
if (is_array($accounts))
{
foreach ($accounts as $account)
{
if ($account == $accName)
{
if ($devEmail != "null-route")
{
mail($devEmail"$accName: new PHP errors",$message); // send one email report per account
}
$flag true;
}
}
}
}
}

if ($flag == false)
{
mail(PHP_ERR_MAILTO"$accName: new PHP errors",$message); // send one email report per account
}
}
}

$ssize sprintf("%u"$size);

$fp fopen(PHP_ERR_SIZE_FILE"w");
fwrite($fp$ssize);
fclose ($fp);

?>



Last blog : Site of the Month - August 2007
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #15 on: Oct 31, 2005, 07:47:17 PM »

Nice script Wineo. Thanks for sharing it!

Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #16 on: Nov 01, 2005, 01:46:33 AM »

NW... Sorry, I took so long to find it...


Last blog : Site of the Month - August 2007
Google dot what?
*
Posts: 2
12 credits
Members referred : 0



« Reply #17 on: Dec 14, 2005, 10:11:28 PM »

Wow, great script. I'll have to give that a try.
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #18 on: Dec 14, 2005, 11:07:48 PM »

some webhosts like website source provide you with WebAlizer, which tracks everything included all errors.

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=92
Tags : php mysql databases cpanel php.ini Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Custom error reporting script
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Sep 07, 2008, 06:15:45 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.301
Total Topics: 7.479
Total Members: 3.904
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: Brandon

26 Guests, 3 Users online :