Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Displaying errors to the screen during development is just the first step. The developer won't always run across every possible error and what about errors that happen in production? That’s where logging comes in.
Settings for Your PHP File
Open logging.php. At the top of the file we have some log options using the ini_set function:
ini_set("log_errors", 1);
ini_set("error_log", "php-error.txt");
Settings for an .htaccess File
# log errors
php_value log_errors 1
# log file for errors
php_value error_log php-error.txt
Settings for the php.ini File
Replace the following path with the full path your log file.
log_errors = On
error_log = "/Applications/MAMP/htdocs/php-error.txt"
-
0:00
Displaying errors to the screen during development is just the first step.
-
0:05
The developer won't always run across every possible error.
-
0:09
And what about errors that happen in production?
-
0:11
That's where logging comes in.
-
0:14
Like other error settings,
-
0:15
ideally your logging should be set in your php.ini file.
-
0:19
However, you can change these settings with the htaccess file or,
-
0:25
in a specific PHP file as well.
-
0:28
Although this is not ideal, you can use the INI set function
-
0:33
in your PHP file to set the configuration option for logging errors.
-
0:38
It's not an ideal solution, because it will not log any
-
0:42
errors if the script has parse errors and cannot be run at all.
-
0:47
Let's see this in action.
-
0:49
I've turned errors back on in the .htaccess file.
-
0:53
Open logging.php.
-
0:54
At the top of the file we have some log options using the ini_set functions.
-
1:00
Log_errors is set to 1.
-
1:03
And error_log is set to php-error.txt.
-
1:06
I'm using .txt for error_log so we can view this file in workspaces.
-
1:12
When we view this script in a browser, we can see our parse error, but
-
1:17
when we go back to work spaces we don't have any error logs.
-
1:22
That's because parse errors won't work.
-
1:25
PHP will never read this line that tells it where error log should be.
-
1:30
If we fix the parse error but leave a syntax error,
-
1:32
you'll see the error logs on the screen and also written to our log.
-
1:38
Let's fix the parse error but leave the syntax error.
-
1:44
Now when we preview it in the browser, we see an error on the screen.
-
1:49
It should also write to our error log.
-
1:56
Great.
-
1:57
The notice and the warning are in our error log.
-
2:00
If we don't have access to the php.ini file,
-
2:04
the next best option is usually the htaccess file.
-
2:07
This is a better option because it will log the errors,
-
2:10
even if there's a parse error in your script.
-
2:13
Let's put our parse error back in, then modify our htaccess file.
-
2:32
We want to turn on log errors.
-
2:34
We use php_value
-
2:39
log_errors 1.
-
2:44
We also need our log file for errors.
-
2:49
Php_value error_log
-
2:54
php-error.txt.
-
3:07
Let's rename our file again.
-
3:12
And now when we preview the browser, we see our parse error.
-
3:19
And if we go back to our PHP errors, we now see our parse error is logged as well.
-
3:26
This by no means covers error handling completely.
-
3:30
I trust that it's given you enough to get you started with this
-
3:33
important part of development.
-
3:35
You should now know how to display and log errors.
-
3:39
Proper error handling makes it easier to track down errors and
-
3:43
get your code up and running.
-
3:45
I've included more information on error and exception handling.
-
3:49
As well as debugging in the teacher's notes.
-
3:51
Have fun and keep learning.
You need to sign up for Treehouse in order to download course files.
Sign up