PHP Logging Errors Into a File
In this article, you will learn how to logging errors in a file using PHP. PHP provides functionality to log errors into a file. It is good practice to hide some specific errors from the user's system. If anyone using your system gets an error code, definitely your application has been determined as buggy. This causes a bad impression on your application. So you must be aware of every possible unanticipated event.
For this, turning off system errors is not only the perfect solution. The system analyst should also notify about the specific system problems that have occurred. PHP provides functionality to log messages in the default PHP error log file. But we can also specify the error log file location and change the log file name.
Set error log file name
To set the error log file name, we will have to edit the 'php.ini' file. So, open the 'php.ini' file in some editor and search for error_log. If there is semicolon in front of this, it means it is commented. Just remove the comment and specify the error log file location name, like -
error_log = "http://www.example.com/logs/php_error.log"
When you open this file, you will find the log messages in the following formats.
[14-Jun-2019 06:35:08 UTC] PHP Notice: Undefined index: source in http://www.example.com\demo.php on line 45
[14-Jun-2019 06:35:08 UTC] PHP Stack trace:
[14-Jun-2019 06:35:08 UTC] PHP 1. {main}() http://www.example.com\demo.php:0
[14-Jun-2019 06:35:29 UTC] PHP Notice: Undefined index: source in http://www.example.com\test.php on line 45
PHP has stored the error messages with the timestamp.
Check PHP Error Logging
To check the proper error message logging, create a PHP file and copy and paste the following code -
<?php
error_log("Error Message");
?>
Now, call this file in the browser and check the log file that you have specified. If your setting is correct, then you will show the above error message with timestamp similar to the following -
[14-Jun-2019 06:35:08 UTC] Error Message
Create Multiple Error Log Files
As we know, the errors are not always of the same type, as the errors can be authentication errors, validation errors, database connectivity errors etc. PHP allows us to set up multiple log files. This process is also easier for a developer to identify a specific type of message in a log file.
<?php
const AUTH_LOG = "http://www.example.com/logs/authentication.log";
const DB_LOG = "http://www.example.com/logs/database_error.log";
error_log("Authentication Error", 3, AUTH_LOG);
error_log("Database Connection Error", 3, DB_LOG);
?>
In the above example, we have used the constant to set the error messages to specific locations. Three parameters have been passed in the error_log() method. The first parameter is an error message, second parameter 3 specifies that this error is logged in a different location other than the specified in php.ini file, and the third parameter is the constant that contains the location file name. So it becomes easier for developers to store all authentication based errors in authentication.log log file and all database based errors in database_error.log log file.
Related Articles
PHP User Authentication by IP AddressHow to encrypt password in PHP
Different datatype comparison in PHP
PHP loop through an associative array
PHP CURL Cookie Jar
PHP remove last character from string
PHP calculate percentage of total
Insert image in database using PHP
PHP set a cookie to store login detail
Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
PHP pagination
Simple PHP File Cache
PHP Connection and File Handling on FTP Server
QR code generator PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL