94

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.

5
  • 13
    What do you mean by "it's not working properly" Mar 20, 2013 at 17:02
  • 7
    Show us what have you tried. Tell use what you get and what you expect to happen. Mar 20, 2013 at 17:02
  • Please show your code, tell what you expected to happen, describe what happens instead. Normally the error_log() function just works and you have not said what exactly did not work for you so your question is not clear.
    – hakre
    Mar 20, 2013 at 17:06
  • Tutorial on this: code2real.blogspot.in/2015/06/…
    – Pupil
    Jul 16, 2015 at 4:00
  • This could stem from a permissions issue where the user you created the error log file for is different than the user being used by Apache to write to the file. Check out '/etc/apache2/envvars/' file for a possible fix. May 1, 2018 at 20:37

4 Answers 4

129

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.

5
  • 2
    I'm just doing error_log("Error message\n") but nothing appears in /var/log/httpd/error_log. :( Any idea why?
    – trusktr
    Jun 1, 2013 at 18:16
  • 3
    In 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
    – suspectus
    Jun 2, 2013 at 19:17
  • Tutorial on this: code2real.blogspot.in/2015/06/…
    – Pupil
    Jul 16, 2015 at 3:59
  • @suspectus, your answer is what I had been looking for long enough. I believe the third parameter could be any file that I like to log the Error message\n to. Am I correct? Jun 20, 2020 at 7:55
  • @Pupil, please tell me how long the data stays in the log file written to by error_log()? Jun 20, 2020 at 10:39
59

you can simply use :

error_log("your message");

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

6
  • 11
    If you use this method, be sure that error logging is turned on and that you uncomment the error_log directive in php.ini. Apr 24, 2014 at 14:58
  • 5
    -1 for suggesting, a year after the question was asked, what the OP already said he tried, but did not work for him. Also, there is already a BETTER answer by suspectus, created a year before this answer. Aug 17, 2014 at 18:42
  • @ToolmakerSteve, This is a pretty good alternative too.
    – Pacerier
    Mar 25, 2015 at 15:16
  • @Pacerier - This answer IS ALREADY WHAT THE OP SAID HE TRIED. So what does it add to the discussion? Aug 5, 2016 at 19:38
  • 2
    @ToolmakerSteve He didn't actually say how he was using it, just that he was trying to use it. The accepted (and very good) answer uses error_log too... Aug 10, 2016 at 4:33
25

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.

3

You can use normal file operation to create an error log. Just refer this and input this link: PHP File Handling

Not the answer you're looking for? Browse other questions tagged or ask your own question.