views:

43

answers:

5

How to turn on all error and warnings and log them to a file but to set up all of that within the script (not changing anything in php.ini). I want to define a file name and so that all errors and warnings get logged into it.

A: 

http://logging.apache.org/log4php/

Great utility

Zoidberg
+3  A: 

See

  • error_log — Send an error message somewhere

Example

error_log("You messed up!", 3, "/var/tmp/my-errors.log");

You can customize error handling with your own error handlers to call this function for you whenever an error or warning or whatever you need to log occurs. For additional information, please refer to the Chapter Error Handling in the PHP Manual

Gordon
+1  A: 

Take a look at set_error_handler.

Leon
+1  A: 

Take a look at the log_errors configuration option in php.ini. It seems to do just what you want to. I think you can use the error_log option to set your own logging file too.

When the log_errors option is enabled, any errors reported by PHP would be logged to the server log or the file specified with error_log. You can set these options with ini_set too, if you need to.

(Please not that display_errors should be disabled in php.ini if this option is enabled)

Frxstrem
Why should `display_errors` be disabled if you enable `log_errors`? Doesn't make sense in my opinion. :)
GuidoH
Because there's no need to output the content of the errors to the public on a production server, especially if the text of the error is being discreetly logged into a file.
Shabbyrobe
A: 

Put the following code on the top of your file:

ini_set("log_errors", 1);
ini_set("error_log", "/path/to/php-error.log");

OR do it via php.ini file settings as follows: http://perishablepress.com/press/2008/01/30/advanced-php-error-handling-via-php/

Aman Kumar Jain