Easy trick for troubleshooting SuiteCRM

Had a lot of problems in the past without this function so I think some of you may find it very useful

Here’s the video that explains it:

https://youtu.be/eMC_VWrM-BI

Here’s the function code


function write_to_log($arr, $str, $email = true) {

    date_default_timezone_set('America/New_York');
    $time_stamp = date("Y-m-d h:i:sa");

    // If the $arr is an object then convert it into an array first
    if(is_object($arr)) {
        $arr = serialize($arr);
        $arr = unserialize($arr);
        $txt = $str.PHP_EOL.PHP_EOL;
        $txt.= "<pre>".print_r($arr, true)."</pre>".PHP_EOL.PHP_EOL;
        file_put_contents('custom/log.txt', PHP_EOL.PHP_EOL, FILE_APPEND);
        file_put_contents('custom/log.txt', $txt, FILE_APPEND);
        file_put_contents('custom/log.txt', PHP_EOL.PHP_EOL, FILE_APPEND);
        return;
    }

    if(!empty($arr)) {

        $arr["Date_time"] = $time_stamp; /* This date goes into the txt file only */
        $txt = $str.":".PHP_EOL;
        $txt.= "<pre>".print_r($arr, true)."</pre>".PHP_EOL.PHP_EOL;

    } else {

        $txt = $str."       (".$time_stamp.")".PHP_EOL.PHP_EOL;

    }

    file_put_contents('custom/log.txt', $txt, FILE_APPEND);

    # Now email
    if($email == true)
        // email_update($txt, $str);

}
2 Likes

Nice!

I suggest including an optional stack trace

array_walk(debug_backtrace(),create_function('$a,$b','print "{$a[\'function\']}()(".basename($a[\'file\']).":{$a[\'line\']}); ";'));
1 Like

good idea thank you!

Just a quick update, here’s the function with stacktrace, I wrote it slightly differently to pgr’s example, so it only returns part of the stacktrace, for full trace uncomment where it says ‘// UNCOMMENT BELOW FOR FULL STACK TRACE’

Include it in your customutils.php file and then you can put it anywhere in your suitecrm code like this

write_to_log($arr), “Some stuff happened”);

^ $arr can be either an array or an object (it works with both)

and it will write it to custom/log.txt



function write_to_log($arr, $str, $email = true) {

    date_default_timezone_set('America/New_York');
    $time_stamp = date("Y-m-d h:i:sa");

    // If the $arr is an object then convert it into an array first
    if(is_object($arr)) {
        $arr = serialize($arr);
        $arr = unserialize($arr);
        $txt = $str.PHP_EOL.PHP_EOL;
        $txt.= "<pre>".print_r($arr, true)."</pre>".PHP_EOL.PHP_EOL;
        file_put_contents('custom/log.txt', PHP_EOL.PHP_EOL, FILE_APPEND);
        file_put_contents('custom/log.txt', $txt, FILE_APPEND);
        file_put_contents('custom/log.txt', PHP_EOL.PHP_EOL, FILE_APPEND);
        return;
    }

    if(!empty($arr)) {

        // Get readable stacktrace info that will go into the array
        $content = unserialize(serialize(debug_backtrace()));
        $content = parse_into_string($content);

        $arr["Date_time"]  = $time_stamp; /* This date goes into the txt file only */
        $arr["StackTrace"] = $content;
        $txt = $str.":".PHP_EOL;
        $txt.= "<pre>".print_r($arr, true)."</pre>".PHP_EOL.PHP_EOL;

    } else {

        $txt = $str."       (".$time_stamp.")".PHP_EOL.PHP_EOL;

    }

    file_put_contents('custom/log.txt', $txt, FILE_APPEND);

    # Now email
    if($email == true)
       // email_update($txt, $str);

}

// Will take the content of the stack trace and parse it into a more manageable array
function parse_into_string($stackTrace) {

    $ignore = array(
      "custom_utils.ext.php",
    );

    $report = "";

    foreach ($stackTrace as $s) {

        // Skip any system file we wish to ignore
        if(isset($s["file"]) AND in_array(basename($s["file"]), $ignore) !== false) {
            continue;
        }

        if(isset($s["class"]))
            $report .= "Class: ".$s["class"]." ";

        if(isset($s["function"]))
            $report .= "Function/method: ".$s["function"]."() ";

        if(isset($s["file"]))
            $report .= "File: ".basename($s["file"])." ";

        if(isset($s["line"]))
            $report .= "on line: ".$s["line"]." ";

        $report .= " | ";

        // UNCOMMENT BELOW FOR FULL STACK TRACE
        break;

    }

    return $report;

}

Thanks @robert_sinclair!

I can’t see any commented code below the line:

        // UNCOMMENT BELOW FOR FULL STACK TRACE

The only commented code I found is a few lines above, within the function write_to_log, at its end:

    if($email == true)
       // email_update($txt, $str);

I haven’t tested it but this commented line should isuue a fatal error because the if statement is followed by a close curly bracket.

Are these correct?

Hi

Sorry I meant “comment out the line” to get the full stack trace :slight_smile: Otherwise it will break; after first loop.

You’re right about missing curly bracket for email function. In my production code it’s not commented out so it does work, but if you just copy/paste directly it would throw an error.

Let me know if you have any more questions

Btw here’s the missing email function just in case


function email_update($msg, $title, $to = NULL) {

    if(!isset($to)) {
        $to = "email@email.com";
    }

    $from = "CRM <crm@crm.com>";
    $headers = "From: CRM <crm@crm.com>\r\n";
    $headers .= 'MIME-Version: 1.0' . "\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; #now mail

    try {
        mail($to, $title, $msg, $headers);
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }

}

Hi Robert!

I was looking through all the material you have on Youtube, which is amazing… thanks!

I was wondering if you would be willing to contribute something to the Docs site (docs.suitecrm.com). We have a Technical Blog there which ideally would gather contributions from many different Community members. And some of the things you documented in Video, would be good material to incorporate to the Docs themselves.

My favorite is the “fields in relationships” tutorials, we really should have that documented in text somewhere…

If you feel motivated for such a thing, even if it’s just one or two articles to try it, I could assist you in learning the mark up language and any other issues you might need to learn in order to write in our Docs.

What do you say?

1 Like

Hey!

Would love to get involved sure. Lets continue by email

Sorry just saw this message