How to write an error log file in PHP

I want to write a message to the error log file when executing PHP code.

I am trying to use the PHP function error_log() Docs .

But it does not work properly for me.

+44
reference php
Mar 20 '13 at 17:01
source share
5 answers

If you do not want to change anything in the php.ini file, according to the 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 waiting for the destination of the file. The third parameter is the path to the log file.

+82
Mar 20 '13 at 17:13
source share

you can just use:

 error_log("your message"); 

By default, the message will be sent to the php system logger.

+29
Feb 21 '14 at 2:05
source share

We all know that PHP saves errors in the php_errors.log file.

But this file contains a lot of data.

If we want to register the data of our applications, we need to save them in an arbitrary place.

To achieve this, we can use two parameters in the error_log function.

http://php.net/manual/en/function.error-log.php

We can do this using:

 error_log(print_r($v, TRUE), 3, '/var/tmp/errors.log'); 

Where

print_r($v, TRUE) : writes the file $ v (array / string / object) to the log file. 3 : put the log message in the user log file specified in the third parameter.

'/var/tmp/errors.log' : Custom log file (this path is for Linux, we can specify others depending on the 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 another depending on the OS). print_r($v, TRUE) : writes the file $ v (array / string / object) to the log file. FILE_APPEND: a constant parameter indicating whether to add to the file, if it exists, if the file does not exist, a new file will be created.

+9
Oct 09 '15 at 9:58
source share

Just remove the code specified in php.ini

It is defined as follows:

 ;error_log = "c:/wamp/logs/php_error.log" 

Change it like this:

 error_log = "c:/wamp/logs/php_error.log" 

This is indicated for Wamp users.

Check your logs in a similar way, as I mention here:

 c:/wamp/logs/php_error.log 

Also make sure in php.ini:

 log_errors = On 
+6
Sep 01 '14 at 12:38 on
source share

You can use the usual file operation to create an error log. Just write this link and enter this link: PHP File Processing

+3
Mar 20 '13 at 17:40
source share



All Articles