How to log errors and warnings into a file in php?

In PHP, errors and warnings can be logged into a file by using a php script and changing configuration of php.ini file. Two such approaches are mentioned below:

Approach 1: The error_log() function can be used to send error messages to a given file. First argument to the function is the error message to be sent. Second argument tells where to send/log the error message. In this case, second argument is set to 3, used to redirect error message to a file. Third argument is used to specify the file path of the error logging file.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php code for logging error into a given file
  
// error message to be logged
$error_message = "This is an error message!";
  
// path of the log file where errors need to be logged
$log_file = "./my-errors.log";
  
// logging error message to given log file
error_log($error_message, 3, $log_file);
  
?>

chevron_right


Output:

[20-Dec-2018 17:32:00 UTC] This is an error message!

Approach 2:

  • The init_set() function allows a user to programmatically update configuration of the php.ini file.
  • The ini_set(“log_errors”, TRUE) command can be added to the php script to enable error logging in php.
  • The ini_set(‘error_log’, $log_file) command can be added to the php script to set the error logging file.
  • Further error_log($error_message) function call can be used to log error message to the given file.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code for logging error into a given file
  
// error message to be logged
$error_message = "This is an error message!";
  
// path of the log file where errors need to be logged
$log_file = "./my-errors.log";
  
// setting error logging to be active
ini_set("log_errors", TRUE); 
  
// setting the logging file in php.ini
ini_set('error_log', $log_file);
  
// logging the error
error_log($error_message);
  
?>

chevron_right


Output:

[20-Dec-2018 17:30:35 UTC] This is an error message!

Similar Approach: 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.log

Note:: 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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.