How do I log errors and warnings into a file?

前端 未结 7 1492
感动是毒
感动是毒 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 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);
    }
    

提交回复
热议问题