Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: aegersz on June 11, 2021, 01:39:53 AM

Title: Writing to the php-error-log with var_dump
Post by: aegersz on June 11, 2021, 01:39:53 AM
from: https://www.justinsilver.com/technology/writing-to-the-php-error_log-with-var_dump-and-print_r/
A little debugging tip, if not aforementioned:

function var_error_log( $object=null ){
    ob_start();                    // start buffer capture
    var_dump( $object );           // dump the values
    $contents = ob_get_contents(); // put the buffer into a variable
    ob_end_clean();                // end capture
    error_log( $contents );        // log contents of the result of var_dump( $object )
}

$object = $variableToLog;
var_error_log( $object );
Title: Re: Writing to the php-error-log with var_dump
Post by: live627 on June 13, 2021, 06:34:44 PM
This is quite neat. One pitfall, though, is that xdebug adds a html transform and trims large or deep arrays. I use var_export to get the entire array.

error_log(var_export($message, true));

To get it into SMF's log is very similar

log_error(var_export($message, true));https://support.simplemachines.org/function_db/index.php?action=view_function;id=42
Title: Re: Writing to the php-error-log with var_dump
Post by: aegersz on June 13, 2021, 09:01:10 PM
Thanks for the tips - writing to a file is preferable to writing to the screen, I discovered ... I am only a novice but looking into variables has helped me with debugging my huge amounts of mod interactions.