How to print a debug log?

前端 未结 15 1691
北海茫月
北海茫月 2020-12-12 10:07

I\'d like to debug some PHP code, but I guess printing a log to screen or file is fine for me.

How should I print a log in PHP code?

The u

15条回答
  •  时光说笑
    2020-12-12 10:19

    You can also write to a file like this:

    $logFilePath = '../logs/debug.text';
    ob_start();
    
    // if you want to concatenate:
    if (file_exists($logFilePath)) {
        include($logFilePath);
    }
    // for timestamp
    $currentTime = date(DATE_RSS);
    
    // echo log statement(s) here
    echo "\n\n$currentTime - [log statement here]";
    
    $logFile = fopen($logFilePath, 'w');
    fwrite($logFile, ob_get_contents());
    fclose($logFile);
    ob_end_flush();
    

    Make sure the proper permissions are set so php can access and write to the file (775).

提交回复
热议问题