Home > Article > Backend Development > PHP Log Errors
PHP programming language provides a different kind of toggling the error logging in order to identify the error’s severity when the app/program is running and returns the error. PHP Log Errors will tell whether the programming script’s error messages are logged on to the server error log or not. This option is mostly the server-specific. It is better to use the error logging concept in place of the error display on most of the production websites.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax and parameters of php log errors are given below:
Syntax:
error_log(message1, message_type1, destination, extra_headers)
In the above syntax, there are 4 parameters mentioned. You will get detailed info of those parameters below:
messages: Message1 parameter is the variable which contains the error message that is to be logged. It is not optional. It is the main key component of the error_log() function. This parameter contains the string type value most of the time.
message_type1: Message_type1 parameter will specify where error/errors should go. Check out the codes of the parameter below so that you will understand better.
PHP Log Errors functions based on checking the program’s error messages which should be logged on to the server error log which is error_log(). PHP Logic Errors works in the server because it is server specific. It works without setting the max length of the log_errors in bytes. In the error_log(), the source’s information is added which is by default 1024 and 0 for not applying max length at all. This is the length that will be applied to the logged error/errors. Max length is also applicable to the displayed errors and also to the “$php_errormsg” but not explicitly called error_log() function/functions.
For better working, PHP Log Errors value should be changed to the 0 value as a security measure for the web-facing servers. It will also send errors to the sys log(system log) if it is set to the Syslog. Errors and warnings in PHP can be logged into the file by using the PHP program/script and also by changing the configuration of the php.ini file. This can be done by using two approaches.
To send the error message/messages to the wanted file you have to use “error_log()” function. The first argument which we pass to the error_log() function will be the error message which is to be sent. The second argument will tell us where to log/send the error message. Here 2nd argument will be set to the value 3 which is used in order to redirect the error message to the file. The third argument will be used to specify the path to file of the error logging file.
Coming to the 2nd approach, init_set() function will allow the user to update the configuration of PHP.INI file programmatically and systematically. To enable the error logging in php ini_set(“log_errors”,TRUE) command will be added. Likewise to set the error logging file “ini_set(‘error_log’,$log_file)” command will be added to the PHP programming script. In order to log the error message to the wanted file “error_log($error_message)” function call will be used.
Example of php log errors are given below:
This is the example of implementing the approach one which is mentioned above. This is a php code that is used to log the error into the wanted file/ the given file. In the below code, a variable called “$error_message1” is created with the string value. Then a variable “$log_file1” is created to name the file while it is to be created. Now the main function “error_log()” will come into existence. The first-term inside of the error_log() function is the error text and the last term of the error_log() is to create the specific file on the specific path with “.log” extension to the text file. In the PHP compiler, there will be no output because it is an error concept. The output which I am showing is in the self-created .log text file.
Code:
<?php $error_message1 = "This is an error message!"; $log_file1 = "./my-errors.log"; error_log($error_message1, 3, $log_file1); ?>
Output:
This is the example of implementing the approach two concepts. In the below PHP script, an error is logged into the wanted file. In the code, a string variable is created at first to show the text in the file. Then again a new variable is created to name the specific file which is to be created further. Then setting the error logging is done to be active by using the 1st ini_set(). Then the second time, ini_set() function is used to set the log file in the php.ini configuration. Then error_log() function is used with only one parameter which is nothing but the variable’s value (string text). The text will be displayed in the .log text file.
Code:
<?php $error_message1 = "Here it is an error message generated itself from the php code!!!"; $log_file2 = "./my-errors.log"; ini_set("log_errors", TRUE); ini_set('error_log', $log_file2); error_log($error_message1); ?>
Output:
The above is the detailed content of PHP Log Errors. For more information, please follow other related articles on the PHP Chinese website!