![]() |
VOOZH | about |
In PHP, errors and warnings can be logged into a file by using a PHP script and changing the configuration of the php.ini file. Two such approaches are mentioned below:
The error_log() function can be used to send error messages to a given file. The first argument to the function is the error message to be sent. The second argument tells where to send/log the error message. In this case, the second argument is set to 3, used to redirect the error message to a file. The third argument is used to specify the file path of the error logging file.
Example: In this example, The PHP code logs an error message "This is an error message!" into a specified file "my-errors.log" using the error_log function with custom file path parameters.
Output:
[20-Dec-2018 17:32:00 UTC] This is an error message!Example: In this example, The PHP code log an error message "This is an error message!" into a specified file "my-errors.log" by setting error logging parameters using ini_set and then calling error_log.
Output:
[20-Dec-2018 17:30:35 UTC] This is an error message!Following lines can also be added directly to php.ini to make the configuration changes permanent for every php script that logs errors and warnings.
log_errors = on
error_log = ./errors.logNote: This approach is not highly reliable as compared to other approaches. Its better to use approach 1 as it gives flexibility of choosing different files for logging at same time without changing configuration of php.ini file.