Logging Errors With PHP
Posted on June 20th, 2010 John Casey
Difficulty: Easy
Estimated completion time: 5 Minutes
Language: PHP
This is just a quick little tip to show you how to log any errors in PHP to a txt file. This allows you to view any unexpected errors found in your code, even if a user does not tell you of the error.
To start off we’re going to make 2 new files, one called index.php, the other called log.txt.
In my index.php file I’m going to turn error reporting on, we do this like so…
<?php
error_reporting(E_ALL);
?>
This will show all errors including notices, warnings and errors.
Next we set some options using ini_set. These will override whatever options you have set in your php.ini file. And allow us to only log errors on page we want to use.
<?php
error_reporting(E_ALL);
//Make sure errors are displayed
ini_set('display_errors', 1);
//Make sure errors are logged
ini_set('log_errors', 1);
//define where our log is
ini_set('error_log', 'log.txt');
?>
So now you will see if we throw an error such as not putting a semi-colon at the end of an echo statement and using an undefined variable without a semi-colon at the end…
<?php
error_reporting(E_ALL);
//Make sure errors are displayed
ini_set('display_errors', 1);
//Make sure errors are logged
ini_set('log_errors', 1);
//define where our log is
ini_set('error_log', 'log.txt');
echo $foo
?>
If we run this we get the error
Notice: Undefined variable: foo in C:\xampp\htdocs\log\index.php on line 15
This will also appear in our log file as…
[19-Feb-2010 18:01:37] PHP Notice: Undefined variable: foo in C:\xampp\htdocs\log\index.php on line 15
That is all! Thanks for reading! If you have any questions please post a comment.
Filed under PHP |
Comments
-
terrance - February 13th, 2015 at 11:34 pm
.…
ñýíêñ çà èíôó….
-
victor - February 14th, 2015 at 4:54 am
.…
ñïàñèáî çà èíôó….
-
Matthew - February 14th, 2015 at 5:28 am
.…
good info!!…
-
Marshall - February 14th, 2015 at 6:00 am
.…
áëàãîäàðñòâóþ….
-
Glen - February 14th, 2015 at 9:49 am
.…
thank you!!…
-
eddie - February 14th, 2015 at 12:17 pm
.…
ñýíêñ çà èíôó….


