How to print a debug log?

前端 未结 15 1680
北海茫月
北海茫月 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:14

    If you don't want to integrate a framework like Zend, then you can use the trigger_error method to log to the php error log.

    0 讨论(0)
  • 2020-12-12 10:16

    You can use error_log to send to your servers error log file (or an optional other file if you'd like)

    0 讨论(0)
  • 2020-12-12 10:16

    This a great tool for debugging & logging php: PHp Debugger & Logger

    It works right out of the box with just 3 lines of code. It can send messages to the js console for ajax debugging and can replace the error handler. It also dumps information about variables like var_dump() and print_r(), but in a more readable format. Very nice tool!

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-12 10:21

    Simply way is trigger_error:

     trigger_error("My error");
    

    but you can't put arrays or Objects therefore use

    var_dump
    
    0 讨论(0)
  • 2020-12-12 10:21

    You can use the php curl module to make calls to http://liveoutput.com/. This works great in an secure, corporate environment where certain restrictions in the php.ini exists that restrict usage of file_put_contents.

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