Definition of 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.
Syntax and Parameters
Syntax and parameters of php log errors are given below:
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.
- Code 1 of the message_type1 parameter will send the message to the PHP sys logger(PHP system logger).
- Code 2 of the message_type1 parameter will send the message to the specified email address which is mentioned in the destination.
- Code 3 of the message_type1 parameter will work and send the message only through the specific PHP debugging connection but it will be done only if the remote debugging is enabled.
- Code 4 of the message_type1 parameter helps in appending the file which is actually specified in the destination.
- Message_type1 parameter is always optional here in the error_log() function. This parameter only accepts the integer type of values/codes as mentioned above.
- destination: Destination parameter helps in specifying the address/location where the error message should go. Location can be a file or an email address. It is the optional parameter. One can use it based on the requirement. It is the resource type value.
- extra_headers: Extra_headers parameter will only be used when the “message_type1” value/code is “1”. It is also the optional parameter. If you don’t want you can neglect it just like some of the other optional parameters. It is the string type value.
How PHP Log Errors Function Works?
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.
Examples to Implement Log Errors in PHP
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.
$error_message1 = "This is an error message!";
$log_file1 = "./my-errors.log";
error_log($error_message1, 3, $log_file1);
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.
$error_message1 = "Here it is an error message generated itself from the php code!!!";
$log_file2 = "./my-errors.log";
This is a guide to PHP Log Errors. Here we discuss the introduction and How PHP Log Errors Function Works along with different examples and code its implementation. You may also have a look at the following articles to learn more –