How to log errors and warnings into a file?

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.


Use the following code:

ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
error_log( "Hello, errors!" );

Then watch the file:

tail -f /tmp/php-error.log

Or update php.ini as described in this blog entry from 2008.


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


    add this code in .htaccess (if you dont have access to php.ini )

    <IfModule mod_php5.c>
    php_flag log_errors on 
    php_value error_log ./path_to_MY_PHP_ERRORS.log
    </IfModule>
    

    ps this is for Apache-type servers.

    链接地址: http://www.djcxy.com/p/26616.html

    上一篇: PHP isset返回true,但应该是false

    下一篇: 如何将错误和警告记录到文件中?