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