9

Is there a PHP function or some other way of obtaining the PHP error log as a string?

I need this because I cannot access the error log of a site I am running on someone else's server. - He offered to email me the error log, but that isn't exactly convenient.

Is there some way I could output the error log to a PHP page?


I realize that viewing the entire server's error log is not really going to happen for me. However, I know you can do something like this to email a manual error_log call to yourself:

error_log('A really bad error', 3, 'me@myemail.com');

Is it possible to configure a page to email errors to you instead of displaying them?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
  • Not a very good solution..., but when you know the path to the error log file you can get it's contents with `file_get_contents`. – evotopid Dec 29 '11 at 11:18
  • See [error_log](http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-log) php.ini setting. – mario Dec 29 '11 at 11:33
  • For locating the PHP error log, the canonical is *[Where does PHP store the error log? (PHP 5, Apache, FastCGI, and cPanel)](https://stackoverflow.com/questions/5127838/)* (despite the over-specific title). – Peter Mortensen Sep 26 '21 at 14:23

1 Answers1

9

On a badly secured server, yes. But on most servers there are two users: apache and [ you ]. You don't have access to the server logs, since they are owned by the apache user (or whichever server you're using).

However, you could probably try it:

echo file_get_contents('/var/log/httpd/error_log');

Note: that's the default location on a RedHat-based apache server. It may be different

Update To reflect the updated question
No, you cannot view the error log with error_log - it is a one-way process that gets handled by the webserver. It only writes the log, but you cannot read it.

You can probably display the errors with this:

ini_set('display_errors', 'On');
error_reporting(E_ALL);

You could even use set_error_handler to handle all warnings and notices (for example, to mail them). But that's pretty much all you can do.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • Hi, I'm on a debian server - just tried that but got "operation not permitted" – Alex Coplan Dec 29 '11 at 11:23
  • Then it's a properly secured server ;-) Just get the man to mail you the logs. – Tom van der Woerdt Dec 29 '11 at 11:25
  • OK, do you think you could take a look at my updated question? – Alex Coplan Dec 29 '11 at 11:29
  • excellent thanks, I think I'll use `set_error_hanlder` - the reason I want this is it's for a web service for an app I'm about to submit to Apple, and if the service goes down anytime between when I submit the app and Apple approves it I need to know straight away - cheers. – Alex Coplan Dec 29 '11 at 11:38
  • Another common location is `/var/log/apache2/error.log`. A canonical is not easy to find, but it may be *[Find out the error_log's path](https://stackoverflow.com/questions/8955411/find-out-the-error-logs-path)* (though not maintained / kept up-to-date). – Peter Mortensen Sep 26 '21 at 13:22