How do I log errors and warnings into a file?

前端 未结 7 1371
感动是毒
感动是毒 2020-11-22 04:43

How do I 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 fi

相关标签:
7条回答
  • 2020-11-22 04:54

    add this code in .htaccess (as an alternative of php.ini / ini_set function):

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

    * as commented: this is for Apache-type servers, and not for Nginx or others.

    0 讨论(0)
  • 2020-11-22 04:57

    Simply put these codes at top of your PHP/index file:

    error_reporting(E_ALL); // Error/Exception engine, always use E_ALL
    
    ini_set('ignore_repeated_errors', TRUE); // always use TRUE
    
    ini_set('display_errors', FALSE); // Error/Exception display, use FALSE only in production environment or real server. Use TRUE in development environment
    
    ini_set('log_errors', TRUE); // Error/Exception file logging engine.
    ini_set('error_log', 'your/path/to/errors.log'); // Logging file path
    
    0 讨论(0)
  • 2020-11-22 05:01

    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 directive is set to On, 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 note that display_errors should be disabled in php.ini if this option is enabled)

    0 讨论(0)
  • 2020-11-22 05:04

    In addition, you need the "AllowOverride Options" directive for this to work. (Apache 2.2.15)

    0 讨论(0)
  • 2020-11-22 05:07

    That's my personal short function

    # logging
    /*
    [2017-03-20 3:35:43] [INFO] [file.php] Here we are
    [2017-03-20 3:35:43] [ERROR] [file.php] Not good
    [2017-03-20 3:35:43] [DEBUG] [file.php] Regex empty
    
    mylog ('hallo') -> INFO
    mylog ('fail', 'e') -> ERROR
    mylog ('next', 'd') -> DEBUG
    mylog ('next', 'd', 'debug.log') -> DEBUG file debug.log
    */
    function mylog($text, $level='i', $file='logs') {
        switch (strtolower($level)) {
            case 'e':
            case 'error':
                $level='ERROR';
                break;
            case 'i':
            case 'info':
                $level='INFO';
                break;
            case 'd':
            case 'debug':
                $level='DEBUG';
                break;
            default:
                $level='INFO';
        }
        error_log(date("[Y-m-d H:i:s]")."\t[".$level."]\t[".basename(__FILE__)."]\t".$text."\n", 3, $file);
    }
    
    0 讨论(0)
  • 2020-11-22 05:12

    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.

    0 讨论(0)
提交回复
热议问题