tags:

views:

77

answers:

4

hi, in PHP.INI, I can set the variable "error_log" and it is currently set to "error_log", which manes one file of that name in each directory

Do you know ... if I set it to "public_html/error_log" - will I get only one single error log file? Any other way to do so? I really just want a single site-wide error file to check, rather than one on each sub-directory.

For bonus marks, can I send myself an email each time a new entry is added to the file?

Left as an exercise for the reader - can I ignore some "errors" which aren't really?

+2  A: 

you can use set_error_handler and set it to a function which logs it to a file (and ignores certain types of errors if you like), and sends the email

function myErrorHandler($errno, $errstr, $errfile, $errline) {
  // check if we want to log the error
  // log the error
  // send the mail
}

set_error_handler('myErrorHandler');
knittl
I always use a custom error handler, so this is a good suggestion. However, I wouldn't send email from within an error handler – I commented on this in my own answer.
fsb
A: 

Specifying an absolute path for the error_log, such as

/var/logs/php.err

in php.ini will make that the default system-wide php error logfile. You can then do per-server/per-directory logging by putting override error_log directives into the appropriate virtual host/directory settings (e.g. site.conf Apach files, .htaccess, etc...) if you need to log specific areas differently.

Marc B
+1  A: 

If you're running PHP under Apache, you can set php directives in .htaccess file, so per site or even per directory, see http://php.net/manual/en/configuration.changes.php

Michal Čihař
+1  A: 

I think the answer is yes, that should work.

As far as email is concerned, knittl gave a correct answer but there are risks. 1 – you could end up with an email flood, 2 – it may slow page delivery down because some email sending methods in PHP take a few seconds to return control to the calling script, which is an issue if it hasn't already sent its results to the client.

If you want email notifications, you could consider an alternative approach, e.g. a script run by cron that emails you if the log has been written to since it last ran. That resolves both issues and you can control the maximum rate of emails by the frequency at which the script runs.

fsb