I want to write a message to an error log file when executing PHP code.
I am trying to use the PHP error_log()
function Docs.
But it's not working properly for me.
I want to write a message to an error log file when executing PHP code.
I am trying to use the PHP error_log()
function Docs.
But it's not working properly for me.
If you don't want to change anything in your php.ini file, according to PHP documentation, you can do this.
error_log("Error message\n", 3, "/mypath/php.log");
The first parameter is the string to be sent to the log. The second parameter 3
means expect a file destination. The third parameter is the log file path.
error_log("Error message\n")
but nothing appears in /var/log/httpd/error_log
. :( Any idea why?
php.ini
file check the error_log configuration setting. This setting will hold the file name that errors are written to. e.g. error_log "log_file_name"
. More details at php.net/manual/en/function.error-log.php
Jun 2, 2013 at 19:17
you can simply use :
error_log("your message");
By default, the message will be send to the php system logger.
error_log
directive in php.ini.
Apr 24, 2014 at 14:58
error_log
too...
Aug 10, 2016 at 4:33
We all know that PHP save errors in php_errors.log file.
But, that file contains a lot of data.
If we want to log our application data, we need to save it to a custom location.
We can use two parameters in the error_log function to achieve this.
http://php.net/manual/en/function.error-log.php
We can do it using:
error_log(print_r($v, TRUE), 3, '/var/tmp/errors.log');
Where,
print_r($v, TRUE) :
logs $v (array/string/object) to log file.
3
: Put log message to custom log file specified in the third parameter.
'/var/tmp/errors.log'
: Custom log file (This path is for Linux, we can specify other depending upon OS).
OR, you can use file_put_contents()
file_put_contents('/var/tmp/e.log', print_r($v, true), FILE_APPEND);
Where:
'/var/tmp/errors.log':
Custom log file (This path is for Linux, we can specify other depending upon OS).
print_r($v, TRUE) :
logs $v (array/string/object) to log file.
FILE_APPEND: Constant parameter specifying whether to append to the file if it exists, if file does not exist, new file will be created.
You can use normal file operation to create an error log. Just refer this and input this link: PHP File Handling
error_log()
function just works and you have not said what exactly did not work for you so your question is not clear.